Sum only items with different currency into separate total filed called foreign total

I did but using .js file with below code but I need it using a custom script

calculate_f_total: function() {
var me = this;
this.frm.doc.foreign_total = 0.0;

  $.each(this.frm.doc["items"] || [], function(i, item) {
  	if (item.currency =='USD') {
  	me.frm.doc.foreign_total += item.amount;
  	}
  	else if(item.is_alt_item =='ALT')
  	{
  	me.frm.doc.foreign_total += item.amount * 0;
  	}	
  	}); 

},