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

julian arce
julian arce
3,181 Points

if I used 'else' and added prompts would it still work because I did that and it still worked

var correct = 0
var question1 = prompt('7-3');
if(question1 === '4') {
  alert('correct, next question');
  correct += 1;
}
else {
alert('sorry you got it wrong, lets go to the next question');
}
var question2 = prompt('10+5');
if(question2 === '15') {
  alert('correct, next question');
  correct += 1;
}
else {
alert('sorry you got it wrong, lets go to the next question');
}
var question3 = prompt('2-1');
if(question3 === '1') {
  alert('correct, next question');
  correct += 1;
}
else {
alert('sorry, you got it wrong, lets go to the next question');
}
var question4 = prompt('20+4');
if (question4 === '24') {
    alert('correct, next question');
  correct += 1;
}
else {
alert('sorry you got it wrong, lets go to the next question');
}
var question5 = prompt('10-4');
if (question5 === '6') {
   correct += 1;
    alert('correct, you got ' + correct + ' out of 5');
}
else {
alert('sorry you got it wrong, you got ' + correct + ' out of 5');
}
rydavim
rydavim
18,814 Points

Sorry, I'm not sure I understand what your question is. Could you elaborate?

I've gone ahead and edited in some markup to your original post so that your code gets formatted nicely.

If you add three backticks (```) on the lines before and after your code block, you get snazzy formatting. You can also add the language immediately following the first line of backticks to get syntax highlighting.

1 Answer

Hey Julian,

Off corse it still work! I like your code. You made your quiz app more fun and interactive by adding the else statement to your questions. I did write my code in a different way also. It doesn't matter how we write the code as long as it is correct, dry, understandable and off course it serve or purpose.

Here is my code:

/*
Ask at least five questions

Keep track of the number of questions the user answered correctly

Provide a final message after the quiz letting the user know the number of questions he or she got right.

Rank the player. If the player answered all five correctly, give that player the gold crown: 3-4 is a silver crown; 1-2 correct answers is a bronze crown and 0 correct is no crown at all.
*/

//5 questions of the our quiz
var question1 = prompt("Do you love JavaScript?");
var question2 = prompt("Do you love Computers?");
var question3 = prompt("Do you love Soccer?");
var question4 = prompt("Do you love Baghdad?");
var question5 = prompt("Do you love Starbucks?");

//Empty var for the score
var score = 0;

//Empty var for the prise
var prise = "";

//Count the answers and give score to the player
if (question1 === "yes" || question1 === "y") {
      score += 1;
}
if (question2 === "yes" || question2 === "y") {
      score += 1;
}
if (question3 === "yes" || question3 === "y") {
      score += 1;
}
if (question4 === "yes" || question4 === "y") {
      score += 1;
}
if (question5 === "yes" || question5 === "y") {
      score += 1;
}
//Check the Rank of the player!
if (score === 5) {
        prise = "Gold crown";
} else if (score === 4 || score === 3){
        prise = "Silver crown";
}else if (score === 2 || score === 1){
        prise = "Bronze crown";
} else {
    prise = "No crown at all";
}

// our final message telling the score and the price
document.write("Your score is: " + score + "<br>Your prise is: " + prise);

Keep your creativity up man.

Cheers!