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 Solution

Why return listHTML??

Hi,

i was wondering why we have to return the variable listHTML in thi code '''function buildList(arr) { var listHTML = '<ol>'; for (var i = 0; i < arr.length; i += 1) { listHTML += '<li>' + arr[i] + '</li>'; } listHTML += '</ol>'; return listHTML; }'''

if we don't use it anywhere. Does it make sense to return it?? I don't see the purpose of returning it! Can you explain me better? Thanks

1 Answer

andren
andren
28,558 Points

If you didn't return anything what point would the function serve? It builds a list and stores it in a variable called listHTML, if it didn't return anything then it would essentially build a list and then immediately throw the list away, making it entirely pointless.

Also you do use the returned value twice, in these two lines:

html += buildList(correct); 
html += buildList(wrong); 

In those two lines you call the function and add what it returns (the content of the listHTML variable) to the html variable.