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 Basics (Retired) Making Decisions with Conditional Statements Improving the Random Number Guessing Game

Why does this code not work ?

what is wrong with my code ? , the javascript console says something is wrong on line 12 but I can't seem to figure out what https://w.trhou.se/h3t07vbrdz

I've noticed that after removing the diff1 and diff2 variables from the entire code , it starts to work just fine but why does it not work when they are there ?

1 Answer

Jason Anders
Jason Anders
Treehouse Moderator 145,857 Points

Hi bavan .

You code is looking great, and the errors you are experiencing is because of how you have the conditionals set up and where you are assigning those 2 variables that are giving you the problem.

Right now, you have the assignments happening inside of the if block of your conditionals which is checking if the guess does EQUAL the random number. If it does, great the person won. However, if it doesn't, the interpreter does not even see the code inside that block, so it is not seeing the assignments for those two variables.
When the interpreter gets to the else if and tries to use the variable diff2 it can't find it, because it was never assigned.

To solve this, you will need to move the diff2 assignment to inside the first else if and the diff1 to inside the second else if. Once done, the code does run perfectly.

Hope this helps.

Nice work!! :)


Thanks a lot for the clear answer !