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

Alex Hort-Francis
Alex Hort-Francis
17,074 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.

:)

Patrick Koch
Patrick Koch
40,498 Points

well nice work, now my minimal approach: ;-)

let questions = [
    ['Question 1', 'Answer1'],
    ['Question 2', 'Answer2'],
    ['Question 3', 'Answer3'],
]

let correct = 0;

for(let i = 0; i < questions.length; i++){
    if( prompt(questions[i][0]).toLowerCase() === questions[i][1].toLowerCase()) correct++;
}

alert(`Correct Answers ${correct}`);