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 Using For Loops with Arrays

Why is my "Using For Loops with Arrays" code not working?

Hi. I've checked and checked my code to see if it matches the instructors, but I can't find any difference. I keep getting "NaNMaybellene" when I preview. Note: the code I added begins at "function printList( list ). Thanks.

var playList = [
  'I Did It My Way',
  'Respect',
  'Imagine',
  'Born to Run',
  'Louie Louie',
  'Maybellene'
];

function print(message) {
  document.write(message);
}

function printList( list ) {
    var listHTML ='<ol>';
    for ( var i = 0; i < list.length; i += 1) {
    listHTML =+ '<li>' + list[i] + '</li>';
    }   
    listHTML += '</ol>';    
    print(listHTML);
}

printList (playList);

The error is in the for loop construct where you have "listHTML =+" instead of "listHTML +=" i.e the plus symbol should come before the equal to symbol. I believe it was just a typo as you have it written correctly in other lines.

Cheers!

4 Answers

The error is in the for loop construct where you have "listHTML =+" instead of "listHTML +=" i.e the plus symbol should come before the equal to symbol. I believe it was just a typo as you have it written correctly in other lines.

Cheers!

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

On line 17 try changing your operator from =+ to += so it matches the rest of them. What you're doing at the moment is assigning a new straight value rather than adding to the variable. :-)

listHTML += '<li>' + list[i] + '</li>';

The error is in the for loop construct where you have "listHTML =+" instead of "listHTML +=" i.e the plus symbol should come before the equal to symbol. I believe it was just a typo as you have it written correctly in other lines.

Cheers!

Thanks to both of you. The Typo Bandit strikes again!

Welcome :)