Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
MOD
Jason Anders
Treehouse Moderator 145,624 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!! :)

:dizzy:

Thanks a lot for the clear answer !