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

Martina Carrington
Martina Carrington
15,754 Points

my song won't show

i probably did something wrong on the music player . keep saying this on the JavaScript console a error Playlist.prototype.add = function(song) { this.songs.push(song); }; . now i have a blank music player

Can you post your code so that we can have a better understanding of what might be wrong?

5 Answers

In your playlist.js, add an (s) to this.song[i].toHTML(); so that your renderInElement will look like this:

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

list.innerHTML += this.songs[i].toHTML();

Martina Carrington
Martina Carrington
15,754 Points

Playlist.js music player

function Playlist() {
  this.song = [];
  this.nowPlayingIndex = 0;

}

Playlist.prototype.add = function(song) {
  this.songs.push(song);
};

Playlist.prototype.play = function() {
  var currentSong = this.songs[this.nowPlayingIndex];
};

Playlist.prototype.stop = function(){
var currentSong = this.songs[this.nowPlayingIndex];
  currentSong.stop();
};

Playlist.prototype.next = function() {
  this.stop();
  this.nowPlayingIndex++;
  if(this.nowPlayingIndex === this.songs.length) {
  this.nowPlayingIndex = 0;
  }
    this.play();
};

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

  }
};
Martina Carrington
Martina Carrington
15,754 Points

@Hayley Swimelar

function Playlist() {
  this.song = [];
  this.nowPlayingIndex = 0;

}

Playlist.prototype.add = function(song) {
  this.songs.push(song);
};

Playlist.prototype.play = function() {
  var currentSong = this.songs[this.nowPlayingIndex];
};

Playlist.prototype.stop = function(){
var currentSong = this.songs[this.nowPlayingIndex];
  currentSong.stop();
};

Playlist.prototype.next = function() {
  this.stop();
  this.nowPlayingIndex++;
  if(this.nowPlayingIndex === this.songs.length) {
  this.nowPlayingIndex = 0;
  }
    this.play();
};

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

  }
};

So, looking at your code, it's actually that the array in the Playlist() constructor function should have the s on the end:

function Playlist() {
  this.songs = [];
  this.nowPlayingIndex = 0;
}

You'll then need to update any time where you're accessing this.song and change it to this.songs.

Also, you're missing the currentSong.play(); from the play method:

Playlist.prototype.play = function() {
  var currentSong = this.songs[this.nowPlayingIndex];
  currentSong.play();
};