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 trialhasinaakhter
8,540 PointsMy quiz code is not working!
var correct=0;
var answer1= prompt("Name a programming language that's also a gem"); if(answer1.toUpperCase()==='RUBY'){ correct +=1; } var answer2= prompt("Name a programming language that's also a snake"); if(answer2.toUpperCase()==='PYTHON'){ correct += 1; }
var answer3= prompt("What language do you use to style webpages?"); if(answer3.toUpperCase()==='CSS'){ correct += 1; } var answer4= prompt("What language do you use to make the structure of the webpages?"); if(answer4.toUpperCase()==='HTML'){ correct += 1; } var answer5= prompt("What language do you use to add interactivity to webpages?"); if(answer5.toUpperCase()==='JAVASCRIPT'){ correct += 1; }
document.write("<p>Well. You got " + correct + " out of 5 questions correct.</p>");
Until this my code is working. But when I write the if else clause my quiz is no longer showing dialogue boxes.
if(correct === 5 ){ document.write("<p>You have earned a gold crown.Congrats.</p>"); } else if(correct >= 3 ){ document.write("<p><strong>You have earned a silver crown.Congrats.</p></strong>"); } else if(correct >= 1 ){ document.write("<p><strong>You have earned a bronz crown.Good luck next time.</p></strong>"); } else (correct === 0){ document.write("<p><strong>Sorry you did not pass. Try again!</p></strong>"); }
Would you please help me here.
john larson
16,594 Pointsvar correct=0;
var answer1= prompt("Name a programming language that's also a gem");
if(answer1.toUpperCase()==='RUBY'){
correct +=1;
}
var answer2= prompt("Name a programming language that's also a snake");
if(answer2.toUpperCase()==='PYTHON'){
correct += 1;
}
var answer3= prompt("What language do you use to style webpages?");
if(answer3.toUpperCase()==='CSS'){
correct += 1;
}
var answer4= prompt("What language do you use to make the structure of the webpages?"); if(answer4.toUpperCase()==='HTML'){
correct += 1;
}
var answer5= prompt("What language do you use to add interactivity to webpages?"); if(answer5.toUpperCase()==='JAVASCRIPT'){
correct += 1;
}
document.write("<p>Well. You got " + correct + " out of 5 questions correct.</p>");
//Until this my code is working. But when I write the if else clause my quiz is no longer showing dialogue boxes.
if(correct === 5 ){
document.write("<p>You have earned a gold crown.Congrats.</p>");
} else if(correct >= 3 ){
document.write("<p><strong>You have earned a silver crown.Congrats.</p></strong>");
} else if(correct >= 1 ){
document.write("<p><strong>You have earned a bronz crown.Good luck next time.</p></strong>");
} else (correct === 0){ //remove this (correct === 0) conditions don't go after an else
document.write("<p><strong>Sorry you did not pass. Try again!</p></strong>");
}
john larson
16,594 Pointsalso to get your code to display nicely use:
three back tics (backtic is the charecter below ~ on the keyboard) JavaScript (skip down a line) //code goes here
three more back tics
then it will look like this
alert("I look much better");
2 Answers
john larson
16,594 PointsI had to take this line out
(correct === 0)
for it to work. conditions don't go after an else
hasinaakhter
8,540 PointsThanks Max and John. Its working now.
All the best.
Max Bulygin
5,601 PointsMax Bulygin
5,601 PointsIt seems it works fine in my browser, but you need to fix your else statement. You wrote else (correct ===0) and this condition is redundant since in previous else if statement you compared correct >=1, so 0 is the only option left. Hope this will help you!