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

My Solution!

Here is what I have done for the ultimate quiz challenge. I know the code is quite repetitive, but check the code out!

var correctAnswer = 0;
var rankCrown;
//Quiz1
var quiz1 = prompt("What equals 1 + 2?");
if (parseInt(quiz1) === 3) {
  alert("Correct! Move onto Quiz 2.");
  correctAnswer += 1;
}
else {
  alert("Sorry, you're wrong. Move onto Quiz 2.");
}
//Quiz2
var quiz2 = prompt("What is the captial of Netherlands?");
if (quiz2.toUpperCase() === "AMSTERDAM") {
  alert("Correct! Move onto Quiz 3.");
  correctAnswer += 1;
}
else {
  alert("Sorry, you're wrong. Move onto Quiz 3.");
}
//Quiz3
var quiz3 = prompt("Who invented the ligt bulb? Type the last name only.");
if (quiz3.toUpperCase() === "EDISON") {
  alert("Correct! Move onto Quiz 4.");
  correctAnswer += 1;
}
else {
  alert("Sorry, you're wrong. Move onto Quiz 4.");
}
//Quiz4
var quiz4 = prompt("When the world war 2 began?");
if (parseInt(quiz4) === 1938) {
  alert("Correct! Move onto Quiz 5.");
  correctAnswer += 1;
}
else {
  alert("Sorry, you're wrong. Move onto Quiz 5.");
}
//Quiz5
var quiz5 = prompt("What is the best programming language?");
if (quiz5.toUpperCase() === "JAVASCRIPT") {
  alert("Correct! You finished the quiz.");
  correctAnswer += 1;
}
else {
  alert("Sorry, you're wrong. You finished the quiz.");
}
//Assigning a crown level
if (correctAnswer === 5) {
  rankCrown = "Gold Crown";
}
else if (correctAnswer === 4 || correctAnswer === 3) {
  rankCrown = "Silver Crown";
}
else if (correctAnswer === 2 || correctAnswer === 1) {
  rankCrown = "Bronze Crown";
}
else {
  rankCrown = "No Crown";
}
//Printing a phrase
document.write("<p>Congraturation! You've answered " + correctAnswer + " questions correct. You achieved a " + rankCrown +" .</p>");

1 Answer

Steven Parker
Steven Parker
229,657 Points

Good solid code. :+1: Yet plenty of opportunities for optimization if you want to refine it further.