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 Build a Quiz Challenge – One Solution

Alex Francis
Alex Francis
11,515 Points

My solution

const questions = [
  [
    'Which method adds an element to the end of an array?', 
    'push()'
  ],
  [
    'Which method adds an element to the start of an array?',
    'unshift()'
  ],
  [
    'Which method determines whether an array includes a certain value among its entries, returning true or false?',
    'indexOf()'
  ]
];

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

let score = 0;
const wrongAnswers = [];
const correctAnswers = [];

for (let i = 0; i < questions.length; i++) {
  if (prompt(questions[i][0]) === questions[i][1]) {
    score++;
    correctAnswers.push(questions[i]); 
  } else {
    wrongAnswers.push(questions[i]);
  }
}

document.querySelector('main').innerHTML = `<h1>You got ${score} correct anwsers!</h1>
                                            <h2>You got these questions right:</h2>
                                            ${makeOL(correctAnswers)}
                                            <h2>You got these questions wrong:</h2>
                                            ${makeOL(wrongAnswers)}
                                            `;

Seems to work, and I made a cheeky function, too.

:)