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 Improve the Quiz – One Solution

My solution

Hey!

Wanted to share my own solution as I see I took a different approach than the teacher and other people's solution here, might not be the DRYIEST / best way to make it function but it gets the job done.

Just wanted to add my grain of salt. Good luck to everyone attempting this!! Not an easy task.

// 1. Create a multidimensional array to hold quiz questions and answers
let quizQuestions = [
  ['How much is 2 by 2', 'How much is 4 by 2', 'How much is 5 by 2'],
  ['4', '8', '10'],
];
let correctAnswerDisplay = []; 
let incorrectAnswerDisplay = [];

// 2. Store the number of questions answered correctly
let answers = 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 < 3; i++) {
  let answerQuiz = prompt(quizQuestions[0][i]);

  if (answerQuiz === quizQuestions[1][i]) {
      answers++;
      correctAnswerDisplay.push(quizQuestions[0][i]);
  } else {
      incorrectAnswerDisplay.push(quizQuestions[0][i]);
    }
}
let  rdyCorrectAnswerDisplay = ` `;
// 4. Display the number of correct answers to the user
  for (let l = 0;l < correctAnswerDisplay.length;l++) {
     rdyCorrectAnswerDisplay += `<li>${correctAnswerDisplay[l]}</li>`;
  }
let rdyIncorrectAnswerDisplay = ` `;
  for (let x = 0;x < incorrectAnswerDisplay.length;x++) {
    rdyIncorrectAnswerDisplay += `<li>${incorrectAnswerDisplay[x]}</li>`;
  }

let html = `<h2>Amount of correct answers: ${answers}</h2>

<h3> Your correct answers: </h3>

    <ol>
      ${rdyCorrectAnswerDisplay}
    </ol>

<h3> Your incorrect answers: </h3>

    <ol>
      ${rdyIncorrectAnswerDisplay}
    </ol>`;

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