Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Daniel Stockham
Daniel Stockham
10,277 Points

Always coming back as NaN

/*Variables needed*/
var i = 0;
var prime = parseInt(prompt("Pick a number"));
var num;

/*The loop is suppose to sum all numbers from input value. 
i.e. input value is 5 so 1+2+3+4+5 = 15*/
while(i < prime){
    i += 1;
    parseInt(num);
    num = num + i;
    console.log(num);
  }  


console.log(num);

When I run this program, num is showing as being NaN. If I parseInt either the input value, wouldn't that be a number not a string?

2 Answers

Gavin Ralston
Gavin Ralston
28,770 Points

The first time through your loop, num is undefined. (See where you declare it?)

So "junk plus some integer" is going to equal NaN.

Try setting it to zero at the top of your script.

Daniel Stockham
Daniel Stockham
10,277 Points

I just realized that 10 minutes ago and it is working now. Coderbyte thinks otherwise though.

Daniel Stockham
Daniel Stockham
10,277 Points

Nevermind, I figured out the challenge on there.