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

Buttons not working -giving error - please help!

1 Answer

Hey Sunny,

Thanks for posting the snapshot! You have a few syntax errors that are breaking the application.

First in your app.js, you've added the same song object twice. This will give us some weird problems.

playlist.add(hereComeTheSun);
playlist.add(hereComeTheSun);

changed to -

playlist.add(hereComeTheSun);
playlist.add(walkingOnSunshine);

Judging by when the errors appear, it looks like the playlist array eventually gets pushed into undefined elements. I took a look at your next playlist prototype and noticed a few issues.

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

};

This function is intended to reset the nowPlayingIndex to 0 once it reaches the end. The first syntax error is this.nowPlayingIndex === 0;. Remember that the three equal sign operator is comparing two values, where in this case we want to use a singular equal sign operator, as it will assign the value to the variable.

The second syntax error is a simple one. this.play should be this.play();.

Here is the fixed version of the next prototype -

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

};

Hope this helps!

The explanation helped. Thanks Justin.