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 Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 2

Getting undefined list. Something's wrong with my answers function or array to capture answers?

I'm seeing 'undefined' where I should be seeing a numbered list of both correct and incorrectly answered questions.

I added .push to my new empty arrays inside the condition of right or wrong and created a separate function to build a list of the answers to display to the page.

Probably way more long-winded than necessary, but would like to get it working before I look at efficiency.

Any help would be great.


Snapshot of workspace: https://w.trhou.se/4yb15p6t65

1 Answer

The clue is the the undefined. A function that doesn't return anything will result in undefined. In other words, Dave sent his assistant to buy a coffee, but never told the assistant to return with the coffee.

You gotta add the return keyword at the end of the function.

function printAnswers(answers) {
  var listHTML = "<ol>";
  for (var i = 0; i < answers.length; i += 1) {
    listHTML += "<li>" + answers[i] + "</li>";
  listHTML += "</ol>";
  return listHTML;