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

Roy Cma
Roy Cma
3,147 Points

displaying entire array instead of specific value.

in displaying the result summary, I was expecting only the question part of the array to be shown, but in the actual results, it shows the entire array.

e.g.: expected: q1 actual: q1, a1

// 1. Create a multidimensional array to hold quiz questions and answers
const answerSheet = [
  [ 'q1', 'a1'],
  [ 'q2', 'a2'],
  [ 'q3', 'a3'],
  [ 'q4', 'a4'],
  [ 'q5', 'a5']
];

// 2. Store the number of questions answered correctly
const quizLength = answerSheet.length;
let correctAnswers = 0;
let answer = '';
let randomQuestion = '';
let randomNumber;
let correctQuestions = [];
let wrongQuestions = [];
let resultSummary;
/* 
  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 = quizLength; i > 0; i-- ) {

  randomNumber = Math.floor(Math.random() * answerSheet.length);
  randomQuestion = answerSheet.splice( randomNumber, 1 );
  answer = prompt( randomQuestion[0][0] );
  if ( answer.toUpperCase() === randomQuestion[0][1].toUpperCase() ) {
    correctAnswers++;
    correctQuestions.push( randomQuestion );
  } else { wrongQuestions.push( randomQuestion );
    }
}

// 4. Display the number of correct answers to the user
let main = document.querySelector('main')
  main.innerHTML = `<h1>You have ${correctAnswers} correct answers</h1>`;

resultSummary = `<h2>You got these questions right:</h2><ol>`;

for (let i = 0; i < correctQuestions.length; i++) {
  resultSummary += `<li>${correctQuestions[i][0]}</li>`;
}


resultSummary += `</ol><h2>You got these questions wrong:</h2><ol>`;

for (let i = 0; i < wrongQuestions.length; i++) {
  resultSummary += `<li>${wrongQuestions[i][0]}</li>`;
}

main.innerHTML += resultSummary + '</ol>';

2 Answers

Steven Parker
Steven Parker
231,269 Points

You're seeing it all because the code is pushing the entire array. Just push the first item (current question):

  if (answer.toUpperCase() === randomQuestion[0][1].toUpperCase()) {
    correctAnswers++;
    correctQuestions.push(randomQuestion[0]);   // push only the current question
  } else {
    wrongQuestions.push(randomQuestion[0]);     // push only the current question
  }
Roy Cma
Roy Cma
3,147 Points

I see, in that line of code, I was expecting correctQuestions to take on the same structure of randomQuestion, but instead it seems to put the entire array of randomQuestion in the first element of correctAnswers. Maybe I should have used the spread operator to achieve the behavior I wanted.