Basic JavaScript - Iterate Through an Array with a For Loop

Tell us what’s happening:

My answer is working, but at first I did total = undefined, after adding, it gave me NaN, from my understanding is we can give undefined to something which is not defined yet? so total is not defined yet, so why is NaN when every loop add to it?

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36

Challenge Information:

Basic JavaScript - Iterate Through an Array with a For Loop

Hello!

Please do not post the full working code to the forum?

The forum is attempting to lower the amount of spoilers for learners.

Here is a link found in fCC News category that I think can answer your question.

Taking as an example the first iteration (i == 0). The myArr[i] is added to the previous value of total. What would be then the result of adding undefined and myArr[0]?

console.log(undefined + 2);  // NaN

I have removed my code, sorry for posting the solution.

1 Like

Oh now I got it, if it was total = 2, it wouldn’t be NaN but as it is undefined, adding any value to undefined will be NaN coz of type coericin?

In case of numbers - correct.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.