I make a function(in javascript) to sum the elements of an array and return the result ... but there is a problem and the output of this function is "NaN" but when i checked the type of variable , that was "number" :| please please help me to solve this ...
function arrSum(a) {
var val = 0;
for (var i = 0; i <= a.length; i++) {
val += a[i];
}
return val;
}
var testArr = [1, 2, 3, 4];
var pass = arrSum(testArr);
document.write(pass);
// SHOW "NaN"
function arrSum(a) {
var val = 0;
for (var i = 0; i <= a.length; i++) {
val += a[i];
}
return val;
}
var testArr = [1, 2, 3, 4];
var pass = arrSum(testArr);
document.write(typeof pass);
// SHOW "number"
i am sorry about my english. i am not good at it :) and thank you for your helps ...
resultdefined?