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
Dean Pierrot
18,403 PointsNeed 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
Corina Meyer
9,990 Pointsyou 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
Dean Pierrot
18,403 Pointsvar 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?
Corina Meyer
9,990 Pointsyou forgot to parse i. :) surround the prompt with parseInt() and it should work
Corina Meyer
9,990 Pointsoh... 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>")
Dean Pierrot
18,403 Pointsvar i = prompt.parseInt("How long has Batman been around?"); Is that correct?
Thomas Nilsen
14,957 PointsNo - Like this:
var i = parseInt(prompt("How long has Batman been around?"));