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
daniel zaragoza
1,843 PointsWhat is wrong with my code ??
I keep getting errors the most recent is that the variable guessAgain is undefined. Please help
var rightGuess = false;
var user = prompt( " What's the name of the UFC champion at the 155 ? ");
if(user.toUpperCase() === " RAFAEL DOS ANJOS"){
rightGuess = true;
} else if(user.toUpperCase() === "ANTHONY PETTIS" ){
var guessAgain = prompt(" Sorry that was the old champion. Try again! ");
}if(guessAgain.toUpperCase() === "RAFAEL DOS ANJOS"){
rightGuess = true;
} else if(user.toUpperCase() === "DONALD CERRONE"){
var guessOnceMore = prompt( " Sorry that was the guy who he last fought. Try again! ");
} if(guessOnceMore.toUpperCase() === " RAFAEL DOS ANJOS "){
rightGuess = true;
}
if(rightGuess){
document.write(" You guessed right ");
}else{
document.write(" Sorry. The champion is ");
}
2 Answers
patrickgardner
7,395 PointsHi Daniel,
When you're defining the guessAgain variable, it's inside a block that's checking if the user has entered Anthony Pettis as the champion. This has the effect of making the variable unavailable in other parts of the code, including where you're checking if the name is "Rafael Dos Anjos" the second time. This kind of situation is a good example of where you might prefer to use a switch statement with something like a while loop, instead of a set of if loops. This can help make it easier for you to reason about the logic for your code. As an example of this, I've tried to rewrite what I think you're looking for with the switch statement.
var rightGuess = false;
var attempts = 0;
var message = "What's the name of the UFC champion at the 155";
while(!rightGuess && attempts < 3) {
var user = prompt(message).toUpperCase();
switch(user) {
case "RAFAEL DOS ANJOS":
document.write("You guessed right");
rightGuess = true;
break;
case "ANTHONY PETTIS":
message = "Sorry that was the old champion. Try again!";
attempts++;
break;
case "DONALD CERRONE":
message = "Sorry that was the guy who he last fought. Try again!"
attempts++;
break;
default:
message = "Sorry, that isn't right either, try again!";
attempts++;
break;
}
}
if (!rightGuess && attempts == 3) {
document.write("Sorry the champion is Rafael Dos Anjos");
}
You'll notice that at the top of the code (outside of any blocks), we've got some variables that we're really using as defaults. These track whether the user has guessed right (as a boolean operator), a number of guesses (as an integer) and a message (which is a string, used for the prompt).
Once the user names 3 guesses incorrectly, they'll get the failure message at the end. There might be a neater way to handle that part of the program but maybe that can be an exercise for the reader :-)
Hope this helps!
daniel zaragoza
1,843 PointsUnfortunately, this is way ahead of my time, in that I am barely on the section where you start Working with conditions, such as the if and else if statements. I don't think i would be wise to get ahead of myself or would it??