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

Abi Tyas Tunggal
Abi Tyas Tunggal
3,149 Points

Here is my attempt at the conditional statements quiz

Hey guys,

Wondering if there is anything I can do to improve this.

/* Asks User Questions
Determines if Answer is Right or Wrong
Prints to Screen How Many Answers Correct
Gives Crown Based on Performance */

var questionOne = prompt('Is this question one?');
var questionTwo = prompt('Is this question two?');
var questionThree = prompt('Is this question three?');
var questionFour = prompt('Is this question four?');
var questionFive = prompt('Is this question five?');
var score = 0;

// Question Check 
if (questionOne.toUpperCase() === 'YES') {
 score += 1;
}
else if (questionTwo.toUpperCase() === 'YES') {
  score += 1;
}
else if (questionThree.toUpperCase() === 'YES') {
  score += 1;
}
else if (questionFour.toUpperCase() === 'YES') {
  score += 1;
}
else if (questionFive.toUpperCase() === 'YES') {
  score += 1;
}

// end of quiz

document.write('<p>You got ' + score + ' questions correct!</p>'); 

// assigning of crown
if (score === 5) {
  document.write('Here is a gold crown');
}
else if (score === 4 || score === 3) {
  document.write('Here is a silver crown');
}  
else if (score === 2 || score === 1) {
  document.write('Here is a bronze crown');
}  
else {
  document.write('You get no crown');
}

2 Answers

TJ Egan
TJ Egan
14,420 Points

Looks like a simple and easy quiz generator. Nice Job. How about adding some trivia questions and having your friends try it out?

Nice work!!

Here is mine:

//5 Quesiton QUIZ

var correctAnswers = 0;
var goldCrown;
var silverCrown;
var bronzeCrown;
var questions = 5;

//Question 1

var answer = prompt("TRUE or FALSE , I am a computer? " + questions + " questions left.")
if (answer.toUpperCase() === "TRUE") {
   document.write("<h2> Question 1 CORRECT</h2>");
    correctAnswers = correctAnswers +1;
  alert("Correct!");
  questions = questions -1;
}
  else {
document.write("<h2> Question 1 WRONG </h2>");
alert("WRONG! Next Question");
   questions = questions -1;
  }



//Question 2

var answer = prompt("TRUE or FALSE , I am a computer " + questions + " questions left.")
if (answer.toUpperCase() === "TRUE") {
   document.write("<h2> Question 2 CORRECT</h2>");
    correctAnswers = correctAnswers +1;
  alert("Correct!");
  questions = questions -1;
}
  else {
document.write("<h2> Question 2 WRONG </h2>");
alert("WRONG! Next Question");
    questions = questions -1;
  }



//Question 3

var answer = prompt("TRUE or FALSE , I am a computer " + questions + " questions left.")
if (answer.toUpperCase() === "TRUE") {
   document.write("<h2> Question 3 CORRECT</h2>");
    correctAnswers = correctAnswers +1;
  questions = questions -1;
  alert("Correct!");
}
  else {
document.write("<h2> Question 3 WRONG </h2>");
alert("WRONG! Next Question");
    questions = questions -1;
  }




//Question 4

var answer = prompt("TRUE or FALSE , I am a computer " + questions + " questions left.")
if (answer.toUpperCase() === "TRUE") {
   document.write("<h2> Question 4 CORRECT</h2>");
    correctAnswers = correctAnswers +1;
  questions = questions -1;
  alert("Correct!");
}
  else {
document.write("<h2> Question 4 WRONG </h2>");
alert("WRONG! Next Question");
    questions = questions -1;
  }




//Question 5

var answer = prompt("TRUE or FALSE , I am a computer " + questions + " questions left.")
if (answer.toUpperCase() === "TRUE") {
   document.write("<h2> Question 5 CORRECT</h2>");
  correctAnswers = correctAnswers +1;
  questions = questions -1;
  alert("Correct!");
}
  else {
document.write("<h2> Question 5 WRONG </h2>");
alert("WRONG! Next Question");
    questions = questions -1;
  }

// Correct Message Output
document.write("<h2> You got " + correctAnswers + " right!</h2>");

//Reward "CROWN" Code

 if( correctAnswers === 5 ) {
 document.write("<h2> YOU EARNED A GOLD CROWN</h2>");
}
else if ( correctAnswers >= 3) {
   document.write("<h2> YOU EARNED A SILVER CROWN</h2>");
}
else {
   document.write("<h2> YOU EARNED A BRONZE CROWN</h2>");
}