Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Arrays Multidimensional Arrays Improve the Quiz – One Solution

Charlie Bird
Charlie Bird
4,504 Points

Any idea why I'm getting 'undefined' before my ordered list?

Instead of using the createListItems function I used a couple of for loops in order to create the list items. However for some reason I get 'undefined' at the start, does anybody know why?

for ( let i = 0; i < correctQuestions.length; i++ ) {
  correctQsList += `<li>${correctQuestions[i]}</li>`
}
for ( let i = 0; i < incorrectQuestions.length; i++ ) {
  incorrectQsList += `<li>${incorrectQuestions[i]}</li>`
}
Juan Luna Ramirez
Juan Luna Ramirez
8,988 Points

did you create variables for both correctQsList and incorrectQsList?

for example

let correctQsList = '';
let incorrectQsList = '';
Tomas Schaffer
Tomas Schaffer
11,569 Points

Hi Charlie,

You should post bigger picture of your code, because i dont see the topic.

Did you declare all variables? if yes are they not out of scope of this function?

Anyway i would create rather function for this case, it would be more correct solution.

1 Answer

Steven Parker
Steven Parker
210,989 Points

I'd bet you forgot to initialize your variables as empty strings. Since the += operator adds to an existing string, if it has not been initialized the first item added will have "undefined" in front of it.

Using just the first loop as an example:

let correctQsList = "";    // first, initialize as empty string before using the += operator
// then the remaining code should work as expected
for ( let i = 0; i < correctQuestions.length; i++ ) {
  correctQsList += `<li>${correctQuestions[i]}</li>`
}
Charlie Bird
Charlie Bird
4,504 Points

Thank you Steven that was it!

I had initialized the variables but not as empty strings, now it makes sense why there is an 'undefined' getting added in. Changing them to empty strings gets rid of this.

Tomas Schaffer
Tomas Schaffer
11,569 Points

There are already two answers with same solutions above, if this is issue then 4 variables are missing.