+ 3
What does NaN mean? And how do I remove it from my code ?
var result, i, arr; arr = [1,2,3,4,5]; flax = arr.length; for (i=0; i < flax; i++){ result+=arr[i]; } //Output: NaN
8 Antworten
+ 15
You have to initialise your variable:
var result = 0
Else, depending on how JS works, result may not even be interpreted as a number.
+ 18
NaN-> Not a Number....
+ 11
There is no print element in the code.....
+ 5
NaN stands for Not a Number.
In arr variable, you have stored 1,2,3,4 and 5. Now you want to add them by a for loop and store the result in result variable.
You have defined the result variable but you haven't stored any data in it. That means " result = undefined "
When you run the program
result = undefined + 1 + 2 + 3 + 4 +5
So, the result = NaN.
But if you store 0 in result variable, it will output 0 + 1 + 2 + 3 + 4 +5 = 15.
+ 4
// Fixed variant
var result = 0;
var arr = [1,2,3,4,5];
var flax = arr.length;
for (i=0; i < flax; i++){
result+=arr[i];
}
alert(result);
+ 3
@N/A, Hatsy's solution should work. Try to declare flax as well.
+ 2
@Hatsy Rei, I did what u said but now it returns 0 instead of the sum.
+ 2
@Jaydeep isnt that dom ?