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 Combining Multiple Tests Into a Single Condition

Lorraine Wheat
Lorraine Wheat
6,083 Points

Using II with strings

So I am trying to do the multiple outcome conditions challenge with the option of allowing quiz takers to guess again if they get the answer wrong. So if I type in just the letter to answer the question, it runs correctly. It prompts me to guess again. However, if I type in the full name, I am not prompted to guess again. The program runs to the last else statement. Am I using the II operator wrong?

var correct = 0; 
var wrong = 0; 

var answerA = "Peter Parker";
var answerA1 = "A";
var answerB = "Bruce Wayne";
var answerB1 = "B";

var quiz = false; 

var question1 = prompt("What is Spiderman's Real Name A. Peter Parker or B. Bruce Wayne?");
if(question1.toUpperCase() === answerA.toUpperCase() || question1.toUpperCase() === answerA1) {

quiz = true; 

}
  else if (question1.toUpperCase() === answerB || question1.toUpperCase() === answerB1) {
    var again = prompt('Sorry! You\'re wrong. Guess  again!'); 
      if(again.toUpperCase() === answerA.toUpperCase() || again.toUpperCase() === answerA1) {
        quiz = true;  
      }
  }

if(quiz) {
correct += 1; 
alert('You have ' + correct + ' answers correct and ' + wrong + ' answers wrong'  ); 
}
else {
wrong += 1; 
alert('You have ' + correct + ' answers correct and ' + wrong + ' answers wrong' ); 
}

3 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Lorraine,

Try changing the else if to just an else. With the if statement, you are checking to see if the question has been answered correctly. Because you only have two choices in this example, you don't need an else if. The conditionals are right, and with this change, it worked in my console.

else {
    var again = prompt('Sorry! You\'re wrong. Guess  again!'); 
      if(again.toUpperCase() === answerA.toUpperCase() || again.toUpperCase() === answerA1) {
        quiz = true;  
}

Give that a try. :dizzy:

Lorraine Wheat
Lorraine Wheat
6,083 Points

Thank you very much for your help. I will try it.

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

I can not spot the problem, but I can see you're missing 1 thing:

again.toUpperCase() === answerA1) 

You're missing the starting ( after answerA1

I hope you get the problem solved very soon :-)

Lorraine Wheat
Lorraine Wheat
6,083 Points

After playing around some bit, I was able to find a solution. I had to add an extra set of parentheses around each condition.

var correct = 0; var wrong = 0;

var answerA = "Peter Parker"; var answerA1 = "A"; var answerB = "Bruce Wayne"; var answerB1 = "B";

var quiz = false;

var question1 = prompt("What is Spiderman's Real Name A. Peter Parker or B. Bruce Wayne?"); if((question1.toUpperCase() === answerA.toUpperCase()) || (question1.toUpperCase() === answerA1.toUpperCase())) {

quiz = true;

} else if ((question1.toUpperCase() === answerB.toUpperCase()) || (question1.toUpperCase() === answerB1.toUpperCase())) { var again = prompt('Sorry! You\'re wrong. Guess again!'); if(again.toUpperCase() === answerA.toUpperCase() || again.toUpperCase() === answerA1) { quiz = true;
} }

if(quiz) { correct += 1; alert('You have ' + correct + ' answers correct and ' + wrong + ' answers wrong' ); } else { wrong += 1; alert('You have ' + correct + ' answers correct and ' + wrong + ' answers wrong' ); }

var question2 = prompt('What colors are Spiderman\'s costume? A. Blue and Red or B. Green and Purple?'); var answerC = "Blue and Red"; var answerC1 = "A"; var answerD = "Green and Purple"; var answerD1 = "B";

if((question2.toUpperCase() === answerC.toUpperCase()) || (question2.toUpperCase === answerC1.toUpperCase())) { quiz = true;
} else if ((question2.toUpperCase() === answerD.toUpperCase()) || (question2.toUpperCase() === answerD1.toUpperCase())) { var again = prompt('Sorry! You\'re wrong. Guess again!'); if((again.toUpperCase() === answerA.toUpperCase()) || (again.toUpperCase = answerA1.toUpperCase())) {

   quiz = true;  
  }

}

if(quiz) { correct += 1; alert('You have ' + correct + ' answers correct and ' + wrong + ' answers wrong' ); } else { wrong += 1; alert('You have ' + correct + ' answers correct and ' + wrong + ' answers wrong' ); }