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
brandonlind2
7,823 PointsCan someone tell me what's wrong with this code?
The code works until it gets to the a prompt telling you the number is either too big or small. Everything you type into that prompt comes back as correct. I cant figure out why, if you can help thanks!
console.log("start program");
var correctGuess= false;
var randomNumber = Math.floor(Math.random() * 6) +1;
var guess= prompt('I am thinking of a number between 1 and 6. What is it?');
if (parseInt(guess)===randomNumber) {correctGuess= true;}
else if(guess<randomNumber) {var guessMore=parseInt(prompt("guess again the number is bigger"));}
if (guessMore===randomNumber) {correctGuess=true}
else if(guess>randomNumber) {var guessLess=parseInt(prompt("guess again the number is smaller"));}
if (guessLess===randomNumber) {correctGuess=true}
if (correctGuess=true) {alert("you\'re correct!");}
else {alert=("sorry the number was " + randomNumber);}
console.log("end program");
2 Answers
Marcio Mello
7,861 PointsI did some modification to your code and it is working.
On your last IF CONDITION you are not testing if correctGuess is true, you are assigning the true value to correctGuess. Change "=" for "===".
I moved your 2nd and 3rd "IF" inside the respective ELSE IF above it. (nested ifs)
console.log("start program");
var correctGuess = false;
var randomNumber = Math.floor(Math.random() * 6) +1;
var guess = prompt('I am thinking of a number between 1 and 6. What is it?');
if (parseInt(guess) === randomNumber) {
correctGuess= true;
} else if (parseInt(guess) < randomNumber) {
var guessMore = parseInt(prompt("guess again the number is bigger"));
if (guessMore===randomNumber) {
correctGuess = true;
}
} else if (parseInt(guess) > randomNumber) {
var guessLess = parseInt(prompt("guess again the number is smaller"));
if (guessLess === randomNumber) {
correctGuess = true;
}
}
if (correctGuess === true) {
alert("you're correct!");
}
else {
alert("sorry the number was " + randomNumber);
}
console.log("end program");
lee F
1,564 Pointsput if (correctGuess)
var correctGuess= false;
var randomNumber = Math.floor(Math.random() * 6) +1;
var guess= prompt('I am thinking of a number between 1 and 6. What is it?');
if (parseInt(guess)===randomNumber) {correctGuess= true;}
else if(guess<randomNumber) {var guessMore=parseInt(prompt("guess again the number is bigger"));}
if (parseInt(guessMore)===randomNumber) {correctGuess=true}
else if(guess>randomNumber) {var guessLess=parseInt(prompt("guess again the number is smaller"));}
if (parseInt(guessLess)===randomNumber) {correctGuess=true}
if (correctGuess) {alert("you\'re correct!");}
else {alert("sorry the number was " + randomNumber);}