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

Been stuck for a week and a half! Any ideas anyone?

var gameScore = 0;
var quizQuestions = [['What is the national sport in Japan', 'sumo wrestling'], ['What was the Olympic city in 1992', 'barcelona spain'], ['Who was the champion of the Tour de France from 1999 to 2005', 'lance armstrong'], ['Which cyclist was also called the cannibal', 'eddy merckx'], ['What do you call a Yoga posture', 'asana']];


for (i = 0; i < quizQuestions.length; i += 1) {
    prompt(quizQuestions[i][0]);
}

if (gameScore === [i][0]) {
      gameScore += 1;
}

function print(message) {
document.write(message); 
}

 print('Youve got ' + gameScore + ' right!');

[mod markup ~K]

1 Answer

Hey Jeremy,

So a couple changes need to be made in order for this to work.

First, you will need to create a variable that will hold the prompt's value

var questions = prompt(quizQuestions[i][0]);

Then, you need to move the gameScore conditional if statement and place it within your for loop. As the questions are asked, you need to verify if the answer given is correct before the user is prompted the next question.

for (i = 0; i < quizQuestions.length; i += 1) {
  var questions = prompt(quizQuestions[i][0]);

   if (gameScore === [i][0]) {
      gameScore += 1;
  }
}

Finally, you need to change your if statement. At this point your conditional statement is incorrectly structured. The variable gameScore carries an integer as its value and you are asking to check if the value of gameScore (gameScore = 0) is equal to what is suppose to be your questions answer (quizQuestions[i][0]) .

To do that we have to change the condition to check if the questions answered is equal to the questions answer (questions[i][1])

for (i = 0; i < quizQuestions.length; i += 1) {
  var questions = prompt(quizQuestions[i][0]);

  if (questions === quizQuestions[i][1]) {
    gameScore += 1;
  }
}

So your final code should look like this. jsFiddle Demo

var gameScore = 0;
var quizQuestions = [
  ['What is the national sport in Japan', 'sumo wrestling'],
  ['What was the Olympic city in 1992', 'barcelona spain'],
  ['Who was the champion of the Tour de France from 1999 to 2005', 'lance armstrong'],
  ['Which cyclist was also called the cannibal', 'eddy merckx'],
  ['What do you call a Yoga posture', 'asana']
];

for (i = 0; i < quizQuestions.length; i += 1) {
  var questions = prompt(quizQuestions[i][0]);

  if (questions === quizQuestions[i][1]) {
    gameScore += 1;
  }
}

function print(message) {
  document.write(message);
}

print('Youve got ' + gameScore + ' right!');

I hope this helps.

Thanks for the help!

jeremy