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

HTML HTML Basics Going Further with HTML Linking to Sections of a Web Page

Why doesn't my app work?

This has nothing to do with Linking to sections of a webpage.

I was making my own simple program where the user guesses a number and the computer generates a number of the range the user chooses, but for some reason, my code is not working and the program is not running properly when the user's guess and the computers generated number are the same.

Here is a link to a snapshot of my code: https://w.trhou.se/bmfnwrkj3w

You can be sure it's not working by choosing the number the range to be to number 1, and also guessing number one, and the correct alert statement will not appear. Why is this?

2 Answers

Maxwell Newberry
Maxwell Newberry
7,693 Points

In your conditional statement, you have a triple equal sign. In other words, you're checking whether or not the guess is identical (strict equal) to the random number generated. What you want is two equal signs, or checking whether the random number is equal (loose equal) to the guess.

You can read the documentation on that here.

But essentially, loose equal (equal, ==) will:

perform a type conversion when comparing two things, and will handle NaN, -0, and +0 specially to conform to IEEE 754 (so NaN != NaN, and -0 == +0);

Whereas strict equal (identical, ===):

will do the same comparison as double equals (including the special handling for NaN, -0, and +0) but without type conversion; if the types differ, false is returned.

In order to update userGuess with parseFloat() you would need to reassign with: userGuess = parseFloat(userGuess)

Maxwell Newberry
Maxwell Newberry
7,693 Points

userGuess is init as a constant so you can’t reassign it.