Sorry all! I'm super stupid Credit Checker Challenge

I’m doing the Credit Checker Challenge, and I keep getting stuck on stupid stuff. Brain not working tonight. Wondering why this would return NaN(not a number) instead of the sum of the array. Thank you! (sorry, I know it’s gotta be something obvious!)

let total;
let stupidArray = [1,3,7,12];
for (n=0; n < stupidArray.length; n++) {
total += stupidArray[n];
}
console.log(total);

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

Hello, @milkytime, and welcome to the forums!

Consider the following examples, and the difference between the two.

//this:
let a
console.log(a)
a += 5
console.log(a)

//versus this:
let b = 0
console.log(b)
b += 5
console.log(b)

Output:

undefined
NaN
0
5

When we try to add a numeric value to undefined we get NaN. When we initialize the variable with a numeric value, and then add another numeric value, we get the expected result.

2 Likes

Thank you! Much appreciated! :smiley:

2 Likes