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 trialjason limmm
8,009 Pointsbackground doesn't turn green
// delcare varaibles to prepare for other code const body=document.body; const input=document.querySelector("#number"); const submit=document.querySelector("button[type=submit]"); const randnum=Math.floor(Math.random()*10)+1;
// cheatcode console.log(randnum);
//wait for user to submit submit.addEventListener( 'click', function correctornah(){ if(parseInt(input)!== randnum){ body.style.backgroundColor="#ed4337"; } else if(parseInt(input)===randnum){ body.style.backgroundColor="#37ed5e"; } });
this a code for a random number guesser website and when i put the correct number the background doesn't turn green.
1 Answer
Steven Parker
231,248 PointsSince "input" is assigned to an HTML element, parseInt(input)
will return NaN (not a number) which will never match a numeric value.
You probably meant to test the value entered instead (input.value
).
Also, it's not necessary to test for the opposite of a condition, a plain else
will do the same thing by itself.
In future postings containing code, please always use Markdown formatting to preserve the code's appearance. Also be sure to include the complete code, both the HTML and JavaScript parts (and perhaps CSS). An easy way to share everything at once is to make a snapshot of your workspace and post the link to it here. And take a look at this video about Posting a Question.