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
Christian Brill
1,209 PointsMy solution
Here's how I solved this problem. Lots of different ways to do it:
/*
Here's a little quiz.
Answer correctly and "win" a prize.
*/
alert("The following will be a short quiz for you. You can win a special prize for answering correctly. Good luck and here goes.");
// quiz begins with zero correct answers
var numberQuestions = 4;
var answer = 0;
// question 1
var question1 = prompt("What is Kim Kardashian's husband's name?");
if (question1.toUpperCase() === 'KANYE WEST') {
alert("Congrats, you got that right. There are " + numberQuestions + " questions left.");
answer += 1;
} else {
alert("Sorry, that was wrong. Keep trying. There are " + numberQuestions + " questions left.");
}
numberQuestions--;
// question 2
var question2 = prompt("What is Earth's name in Latin?");
if (question2.toUpperCase() === 'TERRA') {
alert("Congrats, you got that right. There are " + numberQuestions + " questions left.");
answer += 1;
} else {
alert("Sorry, that was wrong. Keep trying. There are " + numberQuestions + " questions left.");
}
numberQuestions--;
// question 3
var question3 = prompt("Which is the best city on this planet?");
if (question3.toUpperCase() === 'NEW YORK' || question3.toUpperCase() === 'NEW YORK CITY' || question3.toUpperCase() === 'NYC') {
alert("Congrats, you got that right. There are " + numberQuestions + " questions left.");
answer += 1;
} else {
alert("Sorry, that was wrong. Keep trying. There are " + numberQuestions + " questions left.");
}
numberQuestions--;
// question 4
var question4 = prompt("How many suns are there in our solar system?");
if (parseInt(question4) === 1 || question4.toUpperCase() === "ONE") {
alert("Congrats, you got that right. There is " + numberQuestions + " question left. You can do it!");
answer += 1;
} else {
alert("Sorry, that was wrong. Keep trying. There is " + numberQuestions + " question left. You can do it!");
}
numberQuestions--;
// question 5
var question5 = prompt("How many legs does a spider have?");
if (parseInt(question5) === 8 || question5.toUpperCase() === 'EIGHT') {
alert("Congrats, you got that right. There are no more questions.");
answer += 1;
} else {
alert("Sorry, that was wrong. There are no more questions.");
}
// output result
if (answer === 5) {
alert("Here is your score: Congratulations, you got all questions right!");
document.write("Here's the GOLD CROWN for you!");
} else if (answer === 4) {
alert("Here is your score: Congrats, you got 4 out of 5 questions right!");
document.write("Here's the SILVER CROWN for you!");
} else if (answer === 3) {
alert("Here is your score: Congrats, you got 3 out of 5 questions right!");
document.write("Here's the SILVER CROWN for you!");
} else if (answer === 2) {
alert("Here is your score: Congrats, you got 2 out of 5 questions right!");
document.write("Here's the BRONZE CROWN for you!");
} else if (answer === 2) {
alert("Here is your score: Congrats, you got 1 out of 5 questions right!");
document.write("Here's the BRONZE CROWN for you!");
} else {
alert("Sorry, but you didn't get any answers right. No prize for you today.");
}