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 Solution

My prompt() won't prompt... am I missing something here?

let correct = 0;
let answeredCorrectly = [];
let answeredIncorrectly = [];

let quiz = [
  ["What is the meaning of life?", 42],
  ["What color is Donald Trump?", "Orange"],
  ["Who is the richest man in the world?", "Jeff Bezos"]
];

for (let item = 0; item === quiz.length; item++) {
  let question = quiz[item][0];
  let answer = quiz[item][1];
  let userResponse = parseInt( prompt(question) );

  if (userResponse === answer) {
    correct++;
    answeredCorrectly.push(question);
  } else {
    answeredIncorrectly.push(question);
  }
}

document.querySelector("p").innerHTML = `You got ${correct} out of 3 correct! <br>You got these right: ${answeredCorrectly.join(', ')}. <br> And these wrong: ${answeredIncorrectly.join(', ')}.`;

It seems you stored your prompt in a variable thats a part of the local scope of your for loop. Which should be fine, but in order for the prompt to run, that variable needs to be called somewhere in your code, which did not happen. In order for your quiz app to run the way I think you intend it to, i'd recommend you declare the userResponse, answer, and question variables in global scope outside of your for loop and then call on them in the loop. Hope this helps.

2 Answers

Blake Larson
Blake Larson
12,933 Points

item === quiz.length <-- your for loop never runs. You want the for loop to run while item is less than quiz.length.

Thanks Blake. Guess I should have spotted that one! Hm.