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 trialYohanan Braun-Feder
6,101 Pointsplaylist.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
9,851 PointsHi 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