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 Arrays Multidimensional Arrays Build a Quiz Challenge – One Solution

Austin Schneider
Austin Schneider
4,919 Points

My solution was working, then stopped working.

I thought I had mine working well, but when I went to upgrade it to produce lists for correct/incorrect questions it stopped working properly. I removed the new stuff and went back to my original, still not working anymore.

Then I mirrored Guil's solution in my workspace. Still not working- it says I get 0 questions correct even when I type all the right answers

I feel like I am missing something small here... or is there something bigger going on?

Help!

const questionsAnswers = [
  ['What color is the sky?', 'blue'],
  ['What color is snow?', 'white'],
  ['What color is ketchup?', 'red'],
  ['What color is the sun?', 'yellow'],
  ['What color is an orange?', 'orange']
];

let correctAnswers = 0;

for ( let i = 0; i < questionsAnswers.length; i++) {
  let question = questionsAnswers [i][0];
  let answer = [i][1];
  let response = prompt(question);

  if ( response === answer) {
    correctAnswers +=1;
  }
}

let html = `
  <h1>You got ${correctAnswers} question(s) correct!</h1>
`;

document.querySelector('main').innerHTML = html

3 Answers

Cameron Childres
MOD
Cameron Childres
Treehouse Moderator 11,714 Points

Hi Austin,

You're almost there. I only see one issue, and that's in your declaration of the variable "answer". Just like the variable "question" you need to provide the array for it to access. Otherwise all answers are "undefined"

Take loop with i=0 as an example:

let question = questionsAnswers[0][0]; // Accesses questionsAnswers array at [0][0]
let answer = [0][1]; // No array for brackets to relate to, returns undefined

You forgot to set your array of questionsAnswers[i][1] to variable answer.

It should be like this.

for ( let i = 0; i < questionsAnswers.length; i++) {
  let question = questionsAnswers[i][0];
  let answer = questionsAnswers[i][1];
  let response = prompt(question);

  if ( response === answer) {
    correctAnswers +=1;
  }
}
Austin Schneider
Austin Schneider
4,919 Points

Thanks! I knew I was staring right at it. Now I can't recall if I was making the same mistake before I switched to Guil's solution or not! Will just keep moving forward.