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

Ed Farias
Ed Farias
4,476 Points

Another solution. Is it ok?

I used the already created for loop and just built the html text in 2 variables for right and wrong answers instead of creating 2 new arrays. I also didn't use the function. Any issues with this? Effeciency?

const quiz = [ 
  ['How many primary colors are there?', '6'], 
  ['What is the opposite of right', 'left'],
  ['If you are not up, you are?', 'down']          
];

let rightAnswers = '';
let wrongAnswers = '';
let correct = 0;

for (let i = 0; i < quiz.length; i++ ) {
  let answer = prompt(quiz[i][0]);
  if (answer.toLowerCase() === quiz[i][1].toLowerCase()) {
    correct += 1;
    rightAnswers += `<li>${quiz[i][0]}</li>`;
  } else {
    wrongAnswers += `<li>${quiz[i][0]}</li>`;
  }
}

let html = `<h1>Total correct answers is ${correct}</h1><p>You answered these questions correct:</p>
<ol>${rightAnswers}</ol> <p>You answered these questions wrong: </p><ol>${wrongAnswers}</ol>`;

document.querySelector('main').innerHTML = html
Filip Farag
Filip Farag
5,972 Points

I wrote the code exactly like you did, so hopefully it's ok, lol.

1 Answer

I did it same way as you did, found it to be less complex version. And I didn't declare the html variable, just placed it directly in document.querySelector.