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

What's wrong with my "Build a Quiz" JavaScript Code?

Why won't the answers correct display. The questions prompt correctly but that's it. Help, please!

var pizzaQuiz = [ 
  ['What is the most popular pizza topping?', 'PEPPERONI'],
  ['What pizza style is also known as grandma?', 'SICILIAN'],
  ['What is a pizza maker called?', 'PIZZAIOLO']
];
var correctAnswers = 0;
var question;
var answer;
var response;
var html;

function print(message) {
  document.write(message);
}
for (var i = 0; 1 < pizzaQuiz.length; i += 1) {
  question = pizzaQuiz[i][0];
  answer = pizzaQuiz[i][1];
  answer = answer.toUpperCase();
  response = prompt(question);
  if (response === answer) {
   correctAnswers += 1; 
  }
}

html = "You answered " + correctAnswers + " pizza questions right.";
print(html);

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Ethan is correct. That should be an i instead of a 1 inside your for loop. But fixing that doesn't completely fix the problem. Your code contains something that isn't an error exactly, but will result in you not getting the desired result if the user enters the correct answer. Your code is changing the answer to all upper case. But your answer is already all upper case. It's the user's response that should be changed to upper case to determine if it matches the answer. I've reworked your code a bit to show you what I mean:

var pizzaQuiz = [ 
  ['What is the most popular pizza topping?', 'PEPPERONI'],
  ['What pizza style is also known as grandma?', 'SICILIAN'],
  ['What is a pizza maker called?', 'PIZZAIOLO']
];
var correctAnswers = 0;
var question;
var answer;
var response;
var html;

function print(message) {
  document.write(message);
}
for (var i = 0; i < pizzaQuiz.length; i += 1) {
  question = pizzaQuiz[i][0];
  answer = pizzaQuiz[i][1];  //removed toUpperCase from here as it's not needed
   response = prompt(question).toUpperCase();  //placed toUpperCase here as this is what needs to be converted
  if (response === answer) {
   correctAnswers += 1; 
  }
}

html = "You answered " + correctAnswers + " pizza questions right.";
print(html);

As you can see, I moved the toUpperCase() method to the appropriate place and commented it in the code. Hope this helps! :sparkles:

I think a one slipped into your conditional statement in your for loop 1 < pizzaQuiz.length should be variable i instead

Thanks, y'all! Much appreciated. Both fixes worked.