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 only returning one result on boolean based script

Hi Everyone,

I created a small test script that generates a random number, however it only returns the result as if the boolean is false (else statement). Please could someone have a look over it for me and let me know what's wrong with it?

Thanks so much in advance!

var decision = false; var randomNumber = Math.floor(Math.random() * 10) + 1; var guess = prompt("I'm thinking of a number between 1 & 10, what do you think it is?"); if (parseInt(guess) === randomNumber) { decision = true; } if (decision) { alert("That's correct! " + randomNumber + " is the right number!"); } else { alert("That's not right " + randomNumber + " was the number I was thinking of"); }

3 Answers

When i have the right answer it does return "That's correct! " + randomNumber + " is the right number!" for me so I dont see what your problem is here :)

Oh really!? I'm using codeanywhere's compiler... Maybe it's a problem there... hmm.

Thanks so much Stanley!!

Is it possible you were never guessing correctly?

Here's a few things that you could do to make it easier to test both of your code paths.

You could log the randomNumber to the console. That way you know which number to put in the prompt if you want to get it right.

Or you could display the randomNumber in the prompt to achieve the same thing.

These are only temporary things you would put in for easier testing. They should be removed from the final script because they would enable cheating.

Haha, of course I was :). Actually the code works everywhere but not the codeanwhere compiler for some reason!?

Thank you for your suggestion though! Makes so much sense.