Learn Basic String and Array Methods by Building a Music Player - Step 96

Tell us what’s happening:

Hello,

I am having trouble with the nextSongExist constant. Here is error I receive:

You should check if a next song exists comparing userData.songs.length and currentSongIndex and set it to a nextSongExists constant. If the last index of the songs array (userData.songs.length - 1 ) is bigger than the currentSongIndex that means there is a next song.

Your code so far

/* file: script.js */
// User Editable Region

audio.addEventListener("ended", () => {const currentSongIndex=getCurrentSongIndex()});
const nextSongExists = () => {
    if (userData.songs.length == currentSongIndex) {return false;} 
    else if (userData.songs.length - 1 > currentSongIndex) {return true;}
    }
 

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36

Challenge Information:

Learn Basic String and Array Methods by Building a Music Player - Step 96

Pay attention to the instructions. You missed this:
Next, you need to check if there is a next song to play. Retrieve the current song index by calling the getCurrentSongIndex() function, and save it in a currentSongIndex constant.
And Do not create aero function and use ternary operator for statement.

Thanks for replying. Based on the error message I just posted the 2nd part of my code. Here it is in full:

audio.addEventListener("ended", () => {});

const currentSongIndex=getCurrentSongIndex();

const nextSongExists = () => {
    if (userData.songs.length == currentSongIndex) {return false;} 
    else if (userData.songs.length - 1 > currentSongIndex) {return true;}
    }

I originally had the currentSongIndex portion in the addEventListener, and received the error about nextSongExists. So I’ve now move it out and now receive:

You should create a currentSongIndex constant and set it to the calling of the getCurrentSongIndex function.

Reset your challenge step. You don’t need to add a function. Create a variable currentSongIdex and assign it the function getCurrentSongIdex .
Then create a variable nextSongExists and assign it the userData.songs.length equal to currentSongIndex. Then check if the last song userData.songs.length - 1 is bigger than currentSongIndex. If it is, return true otherwise return false.

[quote=“hasanzaib1389, post:5, topic:694218”]

Following your instruction:

  1. Reset challenge:
audio.addEventListener("ended", () => {
  
});
  1. Create a variable currentSongIdex and assign it the function getCurrentSongIdex .
audio.addEventListener("ended", () => {
  
});

const currentSongIndex = getCurrentSongIndex();
  1. Then create a variable nextSongExists and assign it the userData.songs.length equal to currentSongIndex.
audio.addEventListener("ended", () => {
  
});

const currentSongIndex = getCurrentSongIndex();
const nextSongExists = userData.songs.length == currentSongIndex;
  1. Then check if the last song userData.songs.length - 1 is bigger than currentSongIndex. If it is, return true otherwise return false.
audio.addEventListener("ended", () => {
  
});

const currentSongIndex = getCurrentSongIndex();
const nextSongExists = userData.songs.length == currentSongIndex;
if (userData.songs.length - 1 > currentSongIndex){return true}else{return false}

I receive these messages when checking code:
You should create a currentSongIndex constant and set it to the calling of the getCurrentSongIndex function.

  1. Your code has to be inside the "ended" handler function.

  2. Remove the if statement you added and read the hint.

  3. Your nextSongExists variable should be assigned a boolean value of true or false, because it is a const it must happen directly through an expression.

const isBiggerThan = 10 >= 5;

That helped. Thank you. I was making it more complicated than it needed to be.