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

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Introducing Conditional Statements

Answer 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

have 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.

Thanks, Dale! Appreciate the comments in your code.

no, worries! :)

I got it! Forgot to assign the prompt to the variable answer.