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

Kathy Lanier
Kathy Lanier
4,426 Points

My solution using for loops

I used the workspace to test this code and it was very frustrating because it doesn't throw errors. So, I had to use another editor for the challenge.

var questions = ["What is the state capitol of California?", 
                 "Are earthquakes or hurricanes more common in Florida?", 
                 "What is state bird of Georgia?", 
                 "What is the last name of the president?", 
                 "What is the state capitol of Florida?"];
var answers = ["SACREMENTO",
               "HURRICANES", 
               "THRASHER", 
               "TRUMP", 
               "TALLAHASSEE"];
var correctAnswers = 0;
var finalMessage = "You answered "+correctAnswers+"questions correctly";

for(i = 0; i < questions.length; i++){
  var x = prompt(questions[i]);  

  if( x.toUpperCase() === answers[i]){
    correctAnswers++;
    alert("You are correct");
  }
  else{
    alert("You are incorrect");
  }
} 

if ( correctAnswers === 5 ) {
  document.write("<p><strong>You earned a gold crown!</strong></p>");  
} else if (correctAnswers >= 3) {
  document.write("<p><strong>You earned a silver crown.</strong></p>");  
} else if (correctAnswers >= 2) {
  document.write("<p><strong>You earned a bronze crown.</strong></p>");  
} else {
  document.write("<p><strong>No crown for you. You need to study.</strong></p>");
}```