JavaScript JavaScript Arrays Multidimensional Arrays Improve the Quiz – One Solution

Kord Monaco
Kord Monaco
3,442 Points

I always get 0 answers correct even though I have followed code from video. What am I doing wrong?

// 1. Create a multidimensional array to hold quiz questions and answers
const questions = [
  ['When was javascript invented?', '1995'],
  ['How many planets are in the solar system?', '8'],
  ['How many days in a year?', '365'],
  ['How many continents are there?', '7']
];

// 2. Store the number of questions answered correctly
const correct = [];
const incorrect = [];
let correctAnswers = 0;

/* 
  3. Use a loop to cycle through each question
      - Present each question to the user
      - Compare the user's response to answer in the array
      - If the response matches the answer, the number of correctly
        answered questions increments by 1
*/

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

  if ( response === answer ) {
    correctAnswers++;
    correct.push(question);
  } else {
    incorrect.push(question);
  }
}

function createListItems(arr) {
  let items = '';
  for (let i = 0; i < arr.length; i++) {
    items += `<li>${arr[i]}</li>`;
  }
  return items;
}

// 4. Display the number of correct answers to the user
let html = `
  <h1>You got ${correctAnswers} question(s) correct.</h1>
  <h2>You got these questions right:</h2>
  <ol>${ createListItems(correct) }</ol>

  <h2>You got these questions wrong:</h2>
  <ol>${ createListItems(incorrect) }</ol>
`;


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

1 Answer

Cameron Childres
MOD
Cameron Childres
Treehouse Moderator 11,692 Points

Hey Kord~

This is one of those frustrating issues -- you left out a single 's' on questions on line 24. Past that your code is looking great :)