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

Script not working

Can anyone tell me why this isn't working?

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

samiff
samiff
31,206 Points

That code works for me, how have you added it to your html document? What isn't working for you?

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Musical Playlist</title> <link rel="stylesheet" href="css/styles.css"> </head> <body> <h1>My Music Playlist</h1> <script src="js/playlist.js"></script> </body> </html>

HTML looks fine. When I hit preview out of Workspaces, I only get "My Music Playlist" on top with nothing else under it.

samiff
samiff
31,206 Points

Are you able to snapshot your workspace so it's easier to see what's going on? It's in the top right corner when you have the workspace open.

samiff
samiff
31,206 Points

I just forked your workspace and it works fine for me with all six items displayed. Have you tried opening the preview URL in a private browsing window or another web browser? Might just be something funky going on with cached data.

sigh... :-)

That worked! Thank you!

Yea the same thing was happening to me. My code seldom run from the workspace, but that same code always works when I download the project files and run them in my own editor and browser window.