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 JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops `do ... while` Loops

I can't see why my counter variable is returning NaN

function GRN(upper){
    var number = Math.floor(Math.random() * 10) + 1;
    return number;
}

var RN = GRN(10);
console.log(RN)
var guess;
var counter;

do {
    guess = prompt("Pick a number 1-10");
    guess= parseInt(guess);
    counter += 1;
} while (guess !== RN)
print(guess + " is the number <br> " + counter + " tries.");

The code seems to work fine except for the counter .

1 Answer

David Bath
David Bath
25,940 Points

You aren't initializing the value of counter, so it is undefined. You can't increment "undefined", so you end up with NaN. When you declare the counter variable set it to 0.

oh yea... silly me.