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

Need help with this code please!

var answer = 77;

var answer = prompt("How long has Batman been around?"); if ( answer === 77 ) { document.write(<p>"You are right!"<p>) } else if (answer =< 77) { document.write(<p>"Sorry your wrong"<p>) }

document.write(<p>Batman is " + answer + " years old.<p>);

3 Answers

you initialized the same variable (answer) twice. try to use different variables for the right answer and the prompt's return value. you will have to parse the input to be an integer to compare it with the === operator, otherwise it will always fail, because the prompt's return value is of type string. (use parseInt() for that) then you can compare the user's input with the right answer variable instead of writing out the number multiple times (makes it easier to change, after batman's next birthday ;) ) also in your second comparison (in else if) you could check for unequality (!==) or simply skip the if, else alone would do the trick, too

var answer = 77;

var i = prompt("How long has Batman been around?"); if ( answer === 77) { document.write(<p>"You are right!<p>) } else { document.write(<p>"Sorry your wrong<p>") }

document.write(<p>"Batman is + answer +" years old.<p>);

So is this a better look?

you forgot to parse i. :) surround the prompt with parseInt() and it should work

oh... and i just noticed (but i think that might be an error made in haste) you should pay attention where to put the quotes in your document.write calls and to use the right closing tags, for example: document.write("<p>Batman is " + answer + " years old.</p>")

var i = prompt.parseInt("How long has Batman been around?"); Is that correct?

No - Like this:

var i = parseInt(prompt("How long has Batman been around?"));