JavaScript JavaScript Arrays Multidimensional Arrays Improve the Quiz – One Solution

Nick Huemmer
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Nick Huemmer
Front End Web Development Techdegree Graduate 19,651 Points

My code won't run: `quiz.js:24 Uncaught TypeError: Cannot read property '0' of undefined at quiz.js:24`

I'm following the lesson along with Guil, doing my own variation with my own questions and answers.

When the code gets the for loop that evaluates the users' answers to see if they match up with the correct answer, the code stops running with the following TypeError:

quiz.js:24 Uncaught TypeError: Cannot read property '0' of undefined at quiz.js:24

I understand that something is happening at line 24 of my code, but can't figure out what it is...it's virtually identical to Guil's code in this respect.

The TypeError happens here, line 24 is the second line that begins with let question = questions[i][0];

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

Here's the rest of the code:

// 1. Create a multidimensional array to hold quiz questions and answers
const questions = [
  ['Are JavaScript and Java related?', 'No'],
  ['What type of programming is Sass?', 'Preprocessor'],
  ['What in JavaScript allows you to use code over and over again?', 'Functions']
  ['Where in the world is Carmen SanDiego?', 'Netflix']
];

// 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 = questions[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!
  <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

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Nick Huemmer! It looks like you are off by a single comma. The answer lies in this line:

  ['What in JavaScript allows you to use code over and over again?', 'Functions']

up in your questions definition. Note that it's missing a single comma at the end. That means that the array isn't properly defined which leads to this error.

Try:

  ['What in JavaScript allows you to use code over and over again?', 'Functions'],

And see how it goes! Let me know :smiley: Hope this helps! :sparkles:

Nick Huemmer
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Nick Huemmer
Front End Web Development Techdegree Graduate 19,651 Points

That did it! I should have known better, but figured if something was off it would be lower down in the code. BUT! I now understand why it didn't work.

Thanks for checking and for your answer, Jennifer!