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

Manoj Gurung
Manoj Gurung
5,318 Points

why is this code bringing out undefined?

// 1. Create a multidimensional array to hold quiz questions and answers

const QA= [ ["what coutry is the billa villain from?", "indian"], [" Totti is a football......?","player"], ["what is rambo considered as?", "hero"] ]

// 2. Store the number of questions answered correctly let score=0; const correct =[]; const incorrect = [];

/*

  1. 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<QA.length; i++) { let question= QA[i][0]; let answer= prompt(question);

if (answer.toLowerCase()===QA[i][1]) {score++; correct.push[question]; } else {incorrect.push(question); } }

// 4. Display the number of correct answers to the user

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

let html = ` <h1> You got ${score} correct </h1> <h2> The following are ones you got it right </h2> <ol> ${createlist(correct)} </ol>

   <h2>The following are ones you got it wrong </h2>
  <ol> ${createlist(incorrect)} </ol>

` ;

document.querySelector('main').innerHTML=html;

1 Answer

Daniel L.
Daniel L.
10,837 Points

Your code is technically works fine. It was a bit hard to read so I cleaned it up a bit (please consider using Markdown in the future)

// 1. Create a multidimensional array to hold quiz questions and answers

const QA= [ 
  ["what coutry is the billa villain from?", "indian"], 
  ["Totti is a football......?","player"], 
  ["what is rambo considered as?", "hero"] ]

// 2. Store the number of questions answered correctly 
let score=0; 
const correct =[]; 
const incorrect = [];

/*

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<QA.length; i++) { 
  let question= QA[i][0]; 
  let answer= prompt(question);
    if (answer.toLowerCase()===QA[i][1]) {
      score++; 
      correct.push(question); 
      } else {
        incorrect.push(question); 
        } 
  }

// 4. Display the number of correct answers to the user

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

let html = ` 
    <h1> You got ${score} correct </h1> 
    <h2> The following are ones you got it right </h2> 
    <ol> ${createlist(correct)} </ol>
    <h2>The following are ones you got it wrong </h2>
    <ol> ${createlist(incorrect)} </ol>
    `;

document.querySelector('main').innerHTML=html;

The "problem" is in this part:

// 4. Display the number of correct answers to the user

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

You are essentially creating a list that is the length of QA array so you will always have 3 spaces, whatever spaces are left blank, because you got the question right or wrong, will be undefined. This is the solution I came up with:

///not QA.length, use arr.length which is the length of the parameter you are passing to createlist
function createlist(arr) { 
  let items=''; 
  for (let i=0; i<arr.length; i++) { 
    items +=`<li> ${arr[i]}</li>`; 
    } 
    return items; 
    }