How can I sum the numbers in an array? This is what I tried to do
var y = new Array(4)
y[0] = 3;
y[2] = 7 ;
y[1] = 5 ;
var x = this.getField("detail.0") ;
x.value = y[0]+y[1]+y[2]+y[3] ;
// result is NaN
// solved the problem - no empty elements allowed in array
var y = new Array(4)
y[0] = 3;
y[2] = 7 ;
y[1] = 5 ;
y[3] = 0 ;
var x = this.getField("detail.0") ;
x.value = y[0]+y[1]+y[2]+y[3] ;
// result 15
function SumArray(aItem) {
var sum = 0;
for (i = 0; i < aItem.length; i++) {
if ( !isNaN(aItem[i]) ) { //drop NaN items - Not isNaN()
sum += Number(aItem[i]); // add as number
}
return sum; // return the computed sum
}
var y = new Array(4);
y[0] = 3;
y[2] = 7 ;
y[1] = 5 ;
var x = this.getField("detail.0") ;
x.value = SumArray(y);
George Kaiser