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

Axel Lancieri
seal-mask
.a{fill-rule:evenodd;}techdegree
Axel Lancieri
Front End Web Development Techdegree Student 9,416 Points

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;