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 trialMaja Divkovic
Front End Web Development Techdegree Graduate 13,920 PointsTrue or False
Could someone please help me, my outcome is always showing "Sorry, the number was 6."
What am I doing wrong?
let correctGuess = false; const number = 6; const guess = prompt("Guess a number between 1 and 10.");
if ( guess === number) { currectGuess = true; }
if (correctGuess) {
console.log('You guessed the number!');
} else {
console.log(Sorry. The number was ${number}.
);
}
3 Answers
Steven Parker
231,236 PointsIf the answer matches, a new global variable "currectGuess" (with 2 "u"s) is created and set to true.
But the variable used to control the output is "correctGuess" (with an "o").
Also, a comparison with the type-sensitive operator "===" will never consider a string (as prompt gives) to match with a number. You could use the standard comparison "==" to allow the system to perform a conversion, or you could do it explicitly.
CarteA Sharrock
4,860 PointsAs mentioned in the video, another way to write this code would be to simply add a + in front of the variable guess in the conditional statement because you need to convert the string value returned by prompt to a number value in order to achieve your desired results. Ex: if ( +guess === number ) { correctGuess = true; }
Also, your last line of code should include a template literal
Ex:
else {
console.log(Sorry. The number was ${number}.
);
}