Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Object-Oriented JavaScript (2015) Prototypal Inheritance Building the Movie Object

Yohanan Braun-Feder
Yohanan Braun-Feder
6,101 Points

playlist.js returns "not a function" error

Hi, trying to complete the task on this course, but keep running into problems with the code:

in the playlist.js file the console returns

Uncaught TypeError: this.songs[i].toHTML is not a function
function Playlist() {
    this.songs = [];
    this.nowPlayingIndex = 0;
}

Playlist.prototype.add = function() {
    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.songs[i].toHTML(); /* error is here */
  }
};

of course the playlist fails to show or work because of this. Am I missing part of the code? I checked twice with the files.

1 Answer

Jimmy Saul
Jimmy Saul
9,851 Points

Hi Yohanan,

I think the problem could be coming from this line of code here...

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

The final code in the project files looks like this...

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

So it looks like song isn't defined as a parameter for this function, and subsequently the .push method won't add to the array this.songs