Build a Music Player - Step 27

Tell us what’s happening:

Hello! The error message I’m getting is as follows…

“When the currentSong of userData is the last song of the playlist, the playNextSong function should set userData.currentSong to null.”

I’ve tested and re-read my code multiple times and it appears to do what the above is asking for. Does someone have any ideas on how to troubleshoot or resolve this?

Thanks! - Sam

const playNextSong = () => {
  if (userData.currentSong === null) {
    playSong(userData.songs[0].id);
    return;
  } else if (userData.currentSong != null) {
    playSong(getNextSong().id);
    return;
  } else if (userData.currentSong === userData.at(-1)) {
    userData.currentSong = null;
    userData.songCurrentTime = 0;
    pauseSong();
    return;
  }    
};  

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36

Challenge Information:

Build a Music Player - Step 27

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/workshop-music-player/674f534fa181f64a789ffcf9.md at main · freeCodeCamp/freeCodeCamp · GitHub

Hi @snaylspace

If userData.currentSong is not null you need to find the next song to play. Remember that you can use getNextSong for that.

Why not use getNextSong for the if condition?

Happy coding

Hi @Teller ! Thanks for your response. I tried that approach and received the exact same error message. :thinking:

const playNextSong = () => {
  if (userData.currentSong === null) {
    playSong(userData.songs[0].id);
    return;
  } else if (userData.currentSong != null) {
    playSong(getNextSong().id);
    return;
  } else if (getNextSong() === null) {
    userData.currentSong = null;
    userData.songCurrentTime = 0;
    pauseSong();
    return;
  }    
};  

Hi @snaylspace

Instead of this condition, could you use a getNextSong function call as a truthy value?

Happy coding

@Teller - I re-wrote the code as follows, and am now recieving this error:

“When the currentSong of userData is truthy and not the last song of the playlist, the playNextSong function should play the song next to the current song.”

const playNextSong = () => {
  if (userData.currentSong === false) {
    playSong(userData.songs[0].id);
    return;
  } else if (getNextSong() === false) {
    userData.currentSong = null;
    userData.songCurrentTime = 0;
    pauseSong();
    return;
  } else if (userData.currentSong === true) {
    userData.currentSong = getNextSong();
    playSong(userData.currentSong.id);
    return;
  };  
};

The instructions start “If …”, so change the else if statement to an if statement.

You don’t need to carry out a comparison, just call getNextSong as the condition.

getNextSong returns an index if there is a next song to play or throws an error.

const getNextSong = () => userData.songs[getCurrentSongIndex() + 1];

@Teller - I’m unsure why the “else if” statement wouldn’t work in this scenario. Are you suggesting that I create three different “if” statements for each of the conditions?

And what do you mean by this:

“You don’t need to carry out a comparison, just call getNextSong as the condition.”

Based on my best understanding, I’ve updated the code as follows to leverage getNextSong() as you had suggested…

const playNextSong = () => {
  if (userData.currentSong === false) {
    playSong(userData.songs[0].id);
    return;
  } else if (userData.currentSong === true) {
    userData.currentSong = getNextSong();
    playSong(userData.currentSong.id);
    return;
  } else if (getNextSong() === false) {
    userData.currentSong = null;
    userData.songCurrentTime = 0;
    pauseSong();
    return;
  };

So follow me, instead of the condition userData.currentSong != nulljust add getNextSong()

And instead of else ifstatement here, change it to an if statement.

If getNextSong()is truthy the next song is played.

Then over here:

instead of an else if statement, an else statement when there isn’t a next song to play.

Either there is a next song or there isn’t, no need to add a further conditional.

Happy coding