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

Gustavo Winter
PLUS
Gustavo Winter
Courses Plus Student 27,382 Points

I can't understand what is wrong with my "previusButton"

Hi, i try to improove the playlist of the course "Object-Oriented JavaScript" but i make a mistake and i can't see what it is.

My code is here, on my snapshot.

https://w.trhou.se/cgqab5178h

Please, if you can explain me where and why is wrong, i'll appreciate.

Thanks.

2 Answers

Steven Parker
Steven Parker
229,644 Points

You didn't describe the problem.

But I noticed that when you try to use "previus" on the first item, it tries to wrap around but goes to an undefined index. Here's a corrected snippet from Playlist.prototype.previus;

playlist.js
  if(this.nowPlayingIndex < 0) {
    this.nowPlayingIndex = this.songs.length - 1;  // the last index is one LESS than the length
  }
Gustavo Winter
Gustavo Winter
Courses Plus Student 27,382 Points

Nice, thanks.

But let me see if i really understand. "this.songs.length" is equal to 3 ( three songs). But in the array Playlist the index are 0, 1, 2. So for i acess the last song in the array e need to " - 1" because the length dont count the first item like 0 ?

Steven Parker
Steven Parker
229,644 Points

You are correct that "this.songs.length" is equal to 3, and the Playlist index can be 0, 1, or 2.

But that means to access the last song the index must be 2. An index of -1 would not be valid.

That's why when the index goes below 0, you need to reset it to "this.songs.length - 1" which in this case is 2. Your problem was setting it to 3, which is also not a valid index.