Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,135 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.