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 Objects Loop Through Objects Store Objects in Arrays

Micah Angelica Baguio
Micah Angelica Baguio
3,106 Points

Storing objects in an array

I don't know what's wrong? When I tested it in a console, it keeps saying there's an error.

// 1. Create an array to hold quiz questions and answers
const questions = [
  {
    question: 'How many planets are in the Solar System?', 
    answer: '8'
  },

  { 
    question: 'How many continents are there?',
    answer: '7'

  },

  {
    question: 'How many legs does an insect have?', 
    answer: '6'
  },

  {
    question: 'What year was JavaScript created?',
    answer: '1995'
  }
];

// 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].question;
  let answer = questions[i].answer;
  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

Steven Parker
Steven Parker
217,443 Points

This code seems to work fine. It does require that the HTML part contain a <main> section, do you have that (the HTML code isn't shown here)? If that was missing it would cause an error when running.

For future questions, a better way to share code and make your issue easy to replicate is to make a snapshot of your workspace and post the link to it here.

Micah Angelica Baguio
Micah Angelica Baguio
3,106 Points

I found what's the error was. Thank you!