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

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
215,954 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
215,954 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.