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 trialAdam White
10,571 PointsWhy 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);
4 Answers
Andrews Gyamfi
Courses Plus Student 15,658 PointsThe 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
Treehouse Moderator 91,253 PointsOn 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>';
Andrews Gyamfi
Courses Plus Student 15,658 PointsThe 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!
Adam White
10,571 PointsThanks to both of you. The Typo Bandit strikes again!
Jonathan Grieve
Treehouse Moderator 91,253 Pointsyou're welcome. :)
Andrews Gyamfi
Courses Plus Student 15,658 PointsWelcome :)
Andrews Gyamfi
Courses Plus Student 15,658 PointsAndrews Gyamfi
Courses Plus Student 15,658 PointsThe 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!