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 trialStephen Clowes
4,512 PointsAnswer not defined?
Hi there, when I run this code in Chrome I receive the error message "answer not defined:"
prompt("Who is the only person Voldemort feared?"); if (answer.toUpperCase === "DUMBLEDORE") { document.write("<p>Nailed it.</p>"); }
What's the deal?
2 Answers
Dale Bailey
20,269 Pointshave you defined the variable somewhere in the document? try,
// Put the prompt in a variable
var answer = prompt("Who is the only person Voldemort feared?");
// Make the variable string uppercase (nicer this way)
answer = answer.toUpperCase();
// Console log the answer for dubugging
console.log(answer);
// Create the argument, remember that answer is already capitalised now
if (answer === "DUMBLEDORE") {
alert("You said " + answer + " you nailed it.");
} else {
alert('You are wrong! you suck!');
}
Hope this helps.
Stephen Clowes
4,512 PointsI got it! Forgot to assign the prompt to the variable answer.
Stephen Clowes
4,512 PointsStephen Clowes
4,512 PointsThanks, Dale! Appreciate the comments in your code.
Dale Bailey
20,269 PointsDale Bailey
20,269 Pointsno, worries! :)