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 Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1

Quinton Dobbs
Quinton Dobbs
5,149 Points

How to 2 questions have 1 answer and 1 question have 2 answers?

The only snag I am hitting is how to get Javascript to accept either of my two possible answers in my 3rd question. I figure I am trying to fit too many variables in a space that can only carry 1. But I am curious as to how to make this work.

alert("Get ready for a surprise quiz!");
var correctAnswers = 0;
var questions = [
  ['What British classic-rock band sang Bohemian Rhapsody?', 'queen'],
  ['Who was the leader of the Continental Army in the American Revolutionary war?' ,'george washington'],
  ['Who was the leader of Nazi Germany in World War 2?', 'hitler' || 'adolf hitler']
];
for (var i = 0; i < 3; i +=1) {
  document.write('<p> <strong> Question: </strong> ' + questions[i][0] + '</p>');
  var answers = prompt(questions[i][0])
  if(answers.toLowerCase() === questions[i][1]) {
    correctAnswers += 1
    document.write( answers + ' is correct!');
  } else {
    document.write( answers + ' is incorrect!');
  }
}

document.write('<p> <strong> Score: </strong> You got ' + correctAnswers + ' questions correct!</p>');

2 Answers

Steven Parker
Steven Parker
231,269 Points

You can't combine strings with a logical operator (like "||").

Even if you could, it would not cause the program to check both answers separately.

But one way you could make this work is instead of having a single string as the answer, have an array of strings. Then, when checking for the answer you could have another loop that would go through the inner array and compare each one to the response. If any one of them matched, the answer would be considered correct.

Duy Khanh
PLUS
Duy Khanh
Courses Plus Student 4,911 Points

You cannot use (||) logical for string ! if you have two answer , you should store it in an array

var questions = [
  ['What British classic-rock band sang Bohemian Rhapsody?', 'queen'],
  ['Who was the leader of the Continental Army in the American Revolutionary war?' ,'george washington'],
  ['Who was the leader of Nazi Germany in World War 2?', ['hitler','adolf hitler']]
];