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

Raul Cisneros
Raul Cisneros
7,319 Points

Havent been able to figure out how to incorporate the "for" loop, but its still somewhat working.

var quizQuestions = [
  ["How many eggs in one dozen?", 12],
  ["How many numbers on a telephone key pad?", 10],
  ["How many stars in our solar system?", 1]
];

var incorrect = 0;
var correct = 0;

var ques1 = prompt(quizQuestions[0][0]);
if (parseInt(ques1) === 12) {
  correct++;
} else {
  incorrect++;
}

var ques2 = prompt(quizQuestions[1][0]);
if (parseInt(ques2) === 10) {
  correct++;
} else {
  incorrect++;
}

var ques3 = prompt(quizQuestions[2][0]);
if (parseInt(ques3) === 1) {
  correct++;
} else {
  incorrect++;
}

document.getElementById("link").innerHTML = ("You had a total of " + correct + " correct answers.");
document.getElementById("link2").innerHTML = ("You had a total of " + incorrect + " incorrect answers.");
Raul Cisneros
Raul Cisneros
7,319 Points

Been burning too much time on this. Figured I better throw in the towel.

1 Answer

Steven Parker
Steven Parker
230,274 Points

Here's a few hints:

  • where you have 3 very similar sections of code you can use one loop instead
  • the body of the loop would be similar to just one of the three sections
  • you could use the loop index to select which question to show
  • instead of a fixed value, you could also use the loop index to pick the value to compare with

If you still can't get it, proceed to the next video and see the teacher's example.