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 The Conditional Challenge Solution

Bei Zhang
PLUS
Bei Zhang
Courses Plus Student 7,113 Points

I followed the steps, but conditional statement didn't work

var correct = 0;

var question1 = prompt('1+1=?'); if(question1 === 2){ correct += 1;}

var question2 = prompt('1+2=?'); if(question2 === 3){ correct += 1;}

var question3 = prompt('1+3=?'); if(question3 === 4){ correct += 1;}

var question4 = prompt('1+4=?'); if(question4 === 5){ correct += 1;}

var question5 = prompt('1+5=?'); if(question5 === 6){ correct += 1;}

if (correct === 5) { document.write('<p>You earned a gold crown!</p>'); }

else if (correct >= 3 ){ document.write('<p>You earned a silver crown!</p>'); }

else if (correct >= 1){ document.write('<p>You earned a brown crown!</p>'); }

else { document.write('<p>You earned 0 crown!</p>'); }

1 Answer

Christopher Debove
PLUS
Christopher Debove
Courses Plus Student 18,373 Points

The prompt function will return a string. You need to parse this result. Ex:

var question1 =  parseInt(prompt('1+1=?'));

Or compare the result to a string in the condition :

if (question1 === "4") {}

Or use a non type-strict comparison :

if (question1 == 4) {}