I dont want this code to show me NaN can someone help me? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I dont want this code to show me NaN can someone help me?

Const marks = [12, 13, 15, 17] Let sum =0 For (let i=0; i<=marks.length;i++){ sum += marks[i] } Console.log(sum)

19th Dec 2021, 3:37 PM
amir hosein kazazi
amir hosein kazazi - avatar
2 Answers
+ 5
amir hosein kazazi As array index start from 0 and you have 4 elements so your index would be 0 1 2 3 But you are trying to access 4th index which has nothing so you get NaN. So change i <= to i <
19th Dec 2021, 3:40 PM
A͢J
A͢J - avatar
+ 2
One tip, use console.log(i) inside loop, or in any code where have unespected output or error. So you can see what are your data
19th Dec 2021, 5:02 PM
PanicS
PanicS - avatar