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

Alisa Trunczik
Alisa Trunczik
3,659 Points

My quiz (feedback)

Hi everyone! Here is what I've done so far. For sure there are many ways to make this code shorter/more beautiful, but here I've tried to use all what we learned.

I really enjoyed this challenge and now encouraged to play in CSS to make this quiz look more beautiful.

Would be happy for some feedback!

Thanks in advance!

var crown;
var correctAnswer = false;
var score = 0;

 //Questions for user

var userAnswer1 = prompt("How many weeks in the year?");
  if ( userAnswer1 === '52' || userAnswer1.toUpperCase() === 'FIFTYTWO' ) {
      correctAnswer = true;
      alert("It's correct!");
      score += 1; 
  } else {
      alert("Sorry, you are wrong! The correct answer was '52'");
      score += 0;
  }
var userAnswer2 = prompt(" Which has the highest mountain: Earth or Mars?");
  if ( userAnswer2.toUpperCase() === 'MARS' ) {
      correctAnswer === true;
      alert("It's correct!");
      score += 1; 
  }  else {
      alert("Sorry, you are wrong! The correct answer was 'Mars'");
      score += 0;
  }
var userAnswer3 = prompt("How many men have walked on the moon: 4, 8, or 12?");
  if ( userAnswer3 === '12' || userAnswer3.toUpperCase() === 'TWELVE' ) {
      correctAnswer === true;
      alert("It's correct!");
      score += 1; 
  }  else {
      alert("Sorry, you are wrong! The correct answer was '12'");
      score += 0;
  }
var userAnswer4 = prompt("Which word does the 'e' in 'e-mail' stand for?");
  if ( userAnswer4.toUpperCase() === 'ELECTRONIC' ) {
    correctAnswer === true;
      alert("It's correct!");
      score += 1;  
  }  else {
      alert("Sorry, you are wrong! The correct answer was 'electronic'");
      score += 0;
  }
var userAnswer5 = prompt("In which country is the world's largest McDonalds Restaurant?");
  if ( userAnswer5.toUpperCase() === 'CHINA' ) {
      correctAnswer === true;
      alert("It's correct!");
      score += 1; 
  }  else {
      alert("Sorry, you are wrong! The correct answer was 'China'");  
      score += 0;
  }

// Gives user gold, silver, bronze or no crown, depends on user's score

if ( score === 5 ) {
    crown = 'GOLD crown';
    alert("Wow! Congratulations, you've answered ALL the questions correctly!");
} else if ( score === 4 || score === 3) {
    crown = 'SILVER crown';       
} else if ( score === 2 || score === 1) {
    crown = 'BRONZE crown';
} else  {
    crown = 'NO crown';
    alert("Oops! Don't be sad and try again!");
}

// Prints results of the quiz

document.write("<h2> Here are your results! You have " + score + " correct answers.</h2>");
document.write("<h2> You've got a " + crown + "!</h2>");

1 Answer

OOps, watch your use of === !

In several places, you have correctAnswer === true; where you likely meant correctAnswer = true;

=== is a comparison operator.. = is the assignment operator.

Also, uses of score += 0 can be removed entirely. If you add 0 to the score, you do not alter the score!

Hope this helps.. Take care!

-- Cal

Alisa Trunczik
Alisa Trunczik
3,659 Points

Calvin, thanks so much for your review, that helped a lot. I wasn't sure about using === , so now it's clear:) And about +=0 actually makes sense!