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 Two-Dimensional Arrays

For some reason this won't work

The first, fifth and sixth bracket in my array stays green. And when I look at it in it's form it doesn't print. What gives

var playList = [
  ['I Thnk We're Alone Now','The Rubinoos'],
  ['Easy Street', 'Eddie Howell'],
  ['Kaleidoscope','Kaleidoscope UK'],
  ['Come Back Jonee','Devo'],
  ['Peter's Birthday','World of Oz'],
  ['L'Education', 'Aline']
 ];

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

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

printSongs(playList);

Edited to format quote. The formatting helps highlight the problem.

How do I format it again?

Look at the Markdown Cheatsheet or you can edit your post to see what I did.

7 Answers

In your for loop you switch from songs to song. Add the s to song in your two variables and it works.

I found the error by copying and pasting the code into the JavaScript console and running it. It gave me errors that pointed to the solution.

Thanks for reminding me to use the console.

Samuel Webb
Samuel Webb
25,370 Points

You're using apostrophes in your strings while also using single quotes to contain them. Use double quotes to contain your strings and you'll bypass that issue.

Samuel Webb
Samuel Webb
25,370 Points
var playList = [
  ['I Thnk We\'re Alone Now','The Rubinoos'],
  ['Easy Street', 'Eddie Howell'],
  ['Kaleidoscope','Kaleidoscope UK'],
  ['Come Back Jonee','Devo'],
  ['Peter\'s Birthday','World of Oz'],
  ['L\'Education', 'Aline']
 ];

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

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

printSongs(playList);

There you go. That looks about right. I chose to escape the apostrophes instead of using double quotes to make it easier on myself.

Also, how do I put this code into proper html boxes again?

The ' in Peter's is causing you problems. Try changing to this:

'Peter\'s Birthday'

You also have the same problem in L'Education.

I see some other ' issues now. You either have to use " to surround the text when you use ' as part of the text or escape it with .

I replaced single quotes with double, and it still won't print.

Please post your new code.

'''html var playList = [ ['I Thnk We're Alone Now', 'The Rubinoos'], ['Easy Street', 'Eddie Howell'], ['Kaleidoscope','Kaleidoscope UK'], ['Come Back Jonee','Devo'], ['Peter\ 's Birthday','World of Oz'], ['L\ 'Education', 'Aline'], ];

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

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

printSongs(playList); '''

Use the back ticks on the upper left corner of your keyboard, then your quote should look right. Another thing is that you have to double space between any text prior to the quote and the line with the `.

Also, this is JavaScript, not HTML, so you would put JavaScript after the first set of ticks.

Whoops I was using single quotes.

It took me a long time to figure out how to do it right. I have suggested a mandatory video on forum posting. It doesn't exist yet, but I hope it does soon.

'''html var playList = [ ['I Thnk We're Alone Now', 'The Rubinoos'], ['Easy Street', 'Eddie Howell'], ['Kaleidoscope','Kaleidoscope UK'], ['Come Back Jonee','Devo'], ['Peter\ 's Birthday','World of Oz'], ['L\ 'Education', 'Aline'], ];

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

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

printSongs(playList); '''