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 trialBrad Drinnon
3,565 PointsLine 17 confusion
I understand each code block for the most part, but a bit confused on line 17's purpose: if ( correctGuess ) { document.write('<p>You guessed the number!</p>');)
I actually removed this line of code from my program and it still works fine, further adding to my confusion.
Doesn't the code on line 4 and 5 already solve this clause? Thanks in advance for the help!
1 Answer
Ali Abbas
2,097 PointsHey Brad Drinnon,
When you say you removed line 17, do you mean to say that you removed the if statement? Did you also remove the (Document.write) code? In both cases the code will still run without error, but it'll cause a couple of issues.
If you just removed the if statement and not the (document.write) code, then this will cause the code to show on screen whether or not the conditions in the earlier statements are met. In other words, whether you guessed correctly or not the message will appear on screen.
If you removed everything below line 16, then your program will not produce a message confirming whether you guessed correctly or not.
The whole point of line 17 onwards is to declare the results in the form of a message. I hope this clears your confusion.
Ricardo Lee
8,455 PointsRicardo Lee
8,455 PointsIf you guessed the correct number in the first place:
in Line 4 and 5, after checking the conditions (in this case, if variable guess === randomNumber) are met, it changes the previous state of the variable correctGuess from false to true.
then it skips the following "else if" cases all the way down to line 17 whose purpose you were wondering.
Line 17's purpose is to physically show(document.write) you that you guessed correctly.
Line 4 and 5 does not produce any response on the screen.