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

Justin Bigos
Justin Bigos
3,828 Points

Using For Loop with Array

When I open the workspace and click on the index.html there is no <ol> of songs in file code. Also, after following along I cannot get my code to print the songlist. I even went and added code to the html file(when teacher inspects element you can see code in html). Nothing doing either ways. here is my code from my workspace

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);

If anyone can see a misake, please go ahead and point it out. I can't for the life of me see why this won't work. As far as I can tell its identical to teachers.

Thanks, JB

Justin Bigos
Justin Bigos
3,828 Points

The code did not look like that when I put it into the discussion. It got all jumbled. Here it is as formatted.

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);

//Fixed Code Presentation

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

You need to elaborate more what are you trying to accomplish and what does your print function do?

3 Answers

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

Assuming that you still have the playlist array in your code it should work as you have it.

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);

If not, share a snapshot of your workspace. It's the icon at the top of the workspace that looks like a camera. Post the snapshot link here so we can see your code

Justin Bigos
Justin Bigos
3,828 Points

w.trhou.se/5xlizbwedj

Here is a snapshot. Everything looks exactly the same unless i am missing the most obvious error.

Thank for checking it out, JB

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Q. When I open the workspace and click on the index.html there is no of songs in file code

A. Your index.html shouldn't have song list because your goal is injecting it using JavaScript.

Q. After following along I cannot get my code to print the song list

A. Your code prints out through song list through web page, where else would you want to print out song list?

So what do you mean by nothing is working?

Justin Bigos
Justin Bigos
3,828 Points

I didn't think the items needed to be in the index.html file but thought maybe their absence was why code wouldn't run. After clicking preview workspace the songs do not show up on the web page ie: print the song list, assumed 'to the webpage' was a given. So I mean when I hit preview workspace no song list prints out to the webpage or anywhere else for that matter. When you previewed my snapshot and if it worked as intended then I guess its an error based on my browser or laptop.

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

That's the weird part because I forked your project and previewed it without editing code.

You should check the developer tool to inspect what's going on.

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

I didn't edit any of the code from your snapshot. It seemed to work: http://screencast.com/t/uFPJMKobJ9ff