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 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
Cameron Childres
11,818 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.