JavaScript JavaScript Arrays Multidimensional Arrays Improve the Quiz – One Solution

Tatjana Burdett
Tatjana Burdett
2,759 Points

No showing <li>'s correctly. Questions right and wrong show: "Undefined"

I'm not sure what I'm missing. I went back and recreated it matching the video, but still it does not show correctly. No errors shown in the console, but the screen displays:

"You got however many question(s) correct

You got these questions right: undefined

You got these answers wrong: undefined"

____ My code is below____

['Question one?', '5'], ['Question two?', '3'], ['Question three?', '92'], ['Question four?', '54'] ];

const correct = []; const incorrect = []; let correctAnswers = 0;

for (let i = 0; i < questions.length; i++) { let question = questions[i][0]; let answer = questions[i][1]; let response = prompt(question);

if ( response === answer ) { correctAnswers++; correct.push(question); } else { incorrect.push(question); } }

function createListItems(arr) { let items = ''; for (let i = 0; i < arr.lenth; i++) { items += <li>${arr[i]}</li>; } }

let html = ` <h1>You got ${correctAnswers} question(s) correct</h1> <h2>You got these questions right:</h2> <ol>${ createListItems(correct) }</ol>

<h2>You got these questions wrong:</h2> <ol>${ createListItems(incorrect) }</ol> `;

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

1 Answer

Steven Parker
Steven Parker
207,973 Points

For future questions, always use Markdown formatting when posting code.

But I was able to spot two issues:

  • the "createListItems" function doesn't return anything (you probably meant to "return items;")
  • there's a misspelling of "lenth" in the loop (should be "length")
Tatjana Burdett
Tatjana Burdett
2,759 Points

Thank you!!! I was looking at it for awhile and I couldn't find anything wrong! It's like when you say a word over and over again until it loses all meaning.

Also, thank you for the link to markdown formatting! That's really helpful!

Steven Parker
Steven Parker
207,973 Points

Tatjana Burdett — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!