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

Pratik Rai
Pratik Rai
6,856 Points

My code is not working. please help!

I am loosing too much of my hair scratching my head. I followed the tutorial but I am able to get the result. here is my code: var playList = [ 'I Did It My Way', 'Respect', 'Imagine', 'Born to Run', 'Louie Louie', 'Maybellene' ];

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

Function printPlaylist (list) { var topList = '<ol>'; for(var i = 0; i < list.length; i++){ var topList += '<li>' + list[i] + '</li>'; } var topList += '</ol>'; print(topList); }

printPlaylist(playList);

I don't know why is this not working.

Pratik Rai
Pratik Rai
6,856 Points

I have added the html ol opening and closing tag but its not showing in this discussion preview.

3 Answers

Hey Pratik,

I found your problems in your code. Your quotes are fine as you are using single quotes, not double quotes. The problem was that you keep using the "var" keyword every time you are adding value to the "topList" variable. Only use the "var" keyword once when initializing the variable and after that do not use it.

Every time you use the "var" keyword before a variable, you are initializing it. That means that when you used it again in the for loop (and in the line after the for loop), you were telling the JS compiler that you wish for "topList" to be initialized again. You can't use "+=" with an initialization, so you have a double error going on here. 1) You overwrite the value of "topList" that was set beforehand to a now undefined value and 2) you cause a syntax error because you're attempting to use an illegal operator on a variable initialization.

I also added in the HTML that was likely stripped from your code. Be sure to wrap your code in 3 backticks when pasting code to the forums with a blank line above and below the code. Also, no other text on the line with backticks except for a language on the first set of backticks. There is a picture below of what I mean.

var playList = [ 'I Did It My Way', 'Respect', 'Imagine', 'Born to Run', 'Louie Louie', 'Maybellene' ];
function print(message) { 
  document.write(message); 
}

function printPlaylist (list) { 
  var topList = '<ol>'; 
  for(var i = 0; i < list.length; i++){ 
    //Removed var keyword from topList which causes an error when used with += operator
    topList += '<li>' + list[i] + '</li>'; 
  } 
  //Removed var keyword from topList which causes an error when used with += operator
  topList += '</ol>'; 
  print(topList); 
}

printPlaylist(playList);

code

Looks like not enough quotes.

var topList="";

Pratik Rai
Pratik Rai
6,856 Points

i have added that. it is not showing in the preview

Three ` at the beginning and end designates code in the forum. Check out the markdown cheatsheet below the comment field.

Pratik Rai
Pratik Rai
6,856 Points

thanks marcus. that helped me out. I should not repeat the var. hmmm nice.

You're welcome, Pratik. Just remember that anytime you use the "var" keyword, you're creating a new variable so anything you've done before would get overwritten. And you can't use certain operators with an initialization such as "X=" where X is any kind of valid operand.