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

ops

My solution to this task was way off the mark then!

Here you go, my 1st effort :)

var correctAnswers = 0;
var wrongAnswers = 0;
var questionsAsked = 0;



 if (questionsAsked < 5) {
  var question1 = prompt("is A a letter yes or no?");
   if (question1.toLowerCase() === "yes") {
      alert("question 1 correct!");
      correctAnswers += 1;

   }

   else {
    alert("question 1 WRONG!");
     wrongAnswers += 1;
   }

 }

 if (questionsAsked < 5) {
  var question2 = prompt("is B a letter yes or no?");
   if (question2.toLowerCase() === "yes") {
      alert("question 2 correct!");
      correctAnswers += 1;

   }

   else {
    alert("question 2 WRONG!");
     wrongAnswers += 1;
   }

 }

 if (questionsAsked < 5) {
  var question3 = prompt("is C a letter yes or no?");
   if (question3.toLowerCase() === "yes") {
      alert("question 3 correct!");
      correctAnswers += 1;

   }

   else {
    alert("question 3 WRONG!");
     wrongAnswers += 1;
   }

 }

 if (questionsAsked < 5) {
  var question4 = prompt("is D a letter yes or no?");
   if (question4.toLowerCase() === "yes") {
      alert("question 4 correct!");
      correctAnswers += 1;

   }

   else {
    alert("question 4 WRONG!");
     wrongAnswers += 1;
   }

 }

 if (questionsAsked < 5) {
  var question5 = prompt("is E a letter yes or no?");
   if (question5.toLowerCase() === "yes") {
      alert("question 5 correct!");
      correctAnswers += 1;

   }

   else {
    alert("question 5 WRONG!");
     wrongAnswers += 1;
   }

 }



else {

}


   //badges


if (correctAnswers === 5) {
  alert("you got " + correctAnswers + " right so the gold badge!");
}


else if (correctAnswers === 3 || correctAnswers === 3) {
  alert("you got " + correctAnswers + " right so the silver badge!");
}

else if (correctAnswers === 1 || correctAnswers === 2) {
  alert("you got " + correctAnswers + " right so the bronze badge!");
}

 else {
 alert("you got " + correctAnswers + " right so you suck");
 }

didn't need the if statement at the very start really..

1 Answer

well, here was my answer, in case you need something else to compare it to ā€”

// quiz begins, nothing correct thus far

var correctAnswers = 0

alert("We\'re about to start our Quiz! Click the \"OK\" button to get started with the first question.");

// start the quiz...
var firstAnswer = prompt("What programming language is also the name of a gem?");

if (firstAnswer.toUpperCase() === 'RUBY') {
    correctAnswers += 1;
    alert("Correct! Very good! Click the \"OK\" button to go on to the next question...");
} else {
    alert("Oh, Sorry! Not the right answer. Click the \"OK\" button to try on the next question...");
}

var secondAnswer = prompt("What programming language is like a snake in the grass?");

if (secondAnswer.toUpperCase() === 'PYTHON') {
    correctAnswers += 1;
    alert("Correct! Very good! Click the \"OK\" button to go on to the next question...");
} else {
    alert("Oh, Sorry! Not the right answer. Click the \"OK\" button to try on the next question...");
}

var thirdAnswer = prompt("What programming language is probably the best there is?");

if (thirdAnswer.toUpperCase() === 'JAVASCRIPT') {
    correctAnswers += 1;
    alert("Correct! Very good! Click the \"OK\" button to go on to the next question...");
} else {
    alert("Oh, Sorry! Not the right answer. Click the \"OK\" button to try on the next question...");
}

var fourthAnswer = prompt("What web programming language gives the outline structure and basic content to all web pages?");

if (fourthAnswer.toUpperCase() === 'HTML') {
    correctAnswers += 1;
    alert("Correct! Very good! Click the \"OK\" button to go on to ... THE FINAL QUESTION!");
} else {
    alert("Oh, Sorry! Not the right answer. Click the \"OK\" button to try on... THE FINAL QUESTION!");
}

var fifthAnswer = prompt("What web programming language gives wonderful style to web pages and gives the image of a waterfall \'cascading\' down from file to file?");

if (fifthAnswer.toUpperCase() === 'CSS') {
    correctAnswers += 1;
    alert("Correct! Very good! Click the \"OK\" button to go on to find out how your did!");
} else {
    alert("Oh, Sorry! Not the right answer. Click the \"OK\" button to try on... THE FINAL QUESTION!");
}

if (correctAnswers === 5) {
    alert("Congrats! You got ALL FIVE QUIZ QUESTIONS CORRECT! You get THE GOLD CROWN!");
} else if (correctAnswers >= 3) {
    alert("Congrats! You got " + correctAnswers + " correct! Very good! You get THE SILVER CROWN!");
} else if (correctAnswers >= 1) {
    alert("Congrats! You got " + correctAnswers + " correct! Very good! You get THE BRONZE CROWN! Maybe brush up a bit and try again?");
} else {
    alert("Oh dear! You got none correct! So sorry, maybe brush up a bit and try again?");
}

of course, there are other things gone over later in the course that would make all this code far simpler and less repetitive: putting all the prompt/queries & if/else of the quiz questions in a function that takes String arguments of the question/answer and returns the incremented correctAnswers, and maybe a loop that goes through the prompt/question/if/else five times. but above is the basic idea. hope this helps you.

best,

ā€” faddah portland, oregon, u.s.a.