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 Object-Oriented JavaScript (2015) Constructor Functions and Prototypes Making the UI Work

Matt Warren
Matt Warren
11,087 Points

Song titles, artist, and duration not listing to HTML.

Before adding and binding buttons, I loaded the page to see that the li tags have not printed. Instead I receive text in the HTML where the li tags are supposed to be:

"function () { var htmlString = 'function () { var htmlString = '"

I figured there was something wrong with my for loop or Song.prototype.toHTML. The console doesn't print any errors. I quadruple checked to make sure all strings are the same as Andrew's. Would greatly appreciate assistance.

//playlist.js
Playlist.prototype.renderInElement = function(list) {
  list.innerHTML = "";
  for ( var i = 0; i < this.songs.length; i++) {
    list.innerHTML += this.songs[i].toHTML;
  }
};

//song.js
Song.prototype.toHTML = function() {
  var htmlString = '<li';
  if (this.isPlaying) {
    htmlString += ' class="current"';
  }
  htmlString += '>';
  htmlString += this.title;
  htmlString += ' - '
  htmlString += this.artist;
  htmlString += '<span class="duration">'
  htmlString += this.duration;
  htmlString += '</span></li>';
  return htmlString;
};

//app.js
var playlist = new Playlist();

var hereComesTheSun = new Song("Here Comes The Sun", "The Beatles", "2:54");
var loveIsWar = new Song("Love Is War", "Hillsong United", "6:50");

playlist.add(hereComesTheSun);
playlist.add(loveIsWar);

var playlistElement = document.getElementById("playlist");

playlist.renderInElement(playlistElement);

3 Answers

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

You have a couple of small problems. Listed below

//song.js
Song.prototype.toHTML = function() {
  var htmlString = '<li';
  if (this.isPlaying) {
    htmlString += ' class="current"';
  }
  htmlString += '>';
  htmlString += this.title;
  htmlString += ' - ';//missing semicolon
  htmlString += this.artist;
  htmlString += '<span class="duration">';//missing semicolon
  htmlString += this.duration;
  htmlString += '</span></li>';
  return htmlString;
};


//playlist.js
Playlist.prototype.renderInElement = function(list) {
  list.innerHTML = "";
  for ( var i = 0; i < this.songs.length; i++) {
    list.innerHTML += this.songs[i].toHTML();//missing ()
  }
};

Without the parenthesis, instead of running the function you were printing the function.

Seth Kroger
Seth Kroger
56,413 Points

In your for loop in playlist.js you're not using parentheses to call the toHTML function:

  for ( var i = 0; i < this.songs.length; i++) {
    list.innerHTML += this.songs[i].toHTML; // Grabs the property toHTML
  }

versus:

  for ( var i = 0; i < this.songs.length; i++) {
    list.innerHTML += this.songs[i].toHTML();  // This should call it as a function
  }