A maybe bug in Javascript Music Player exercise

I’m doing the exercises for certifications and I believe there’s a little bug in the code.

There’s a userData object with a userData.songCurrentTime field

let userData = {
  songs: [...allSongs],
  currentSong: null,
  songCurrentTime: 0,
};

In the pauseSong arrow function you have to store the audio.CurrentTime in the userData.songCurrentTime

const pauseSong = () => {
  userData.songCurrentTime = audio.currentTime;

And in the playSong arrow function you have an if statement:

  if (userData?.currentSong === null || userData?.currentSong.id !== song.id) {
    audio.currentTime = 0;
  } else {
    audio.currentTime = userData?.songCurrentTime;
  }

so if the id of the song is diferent you start by the currentTime = 0 , otherwise you continue from the songCurrentTime stored in userData when pausing.

The problem is that there’s other function renderSongs that creates a button with an onclick event calling playSong(${song.id}) without setting userData.songCurrentTime to 0 nor saving the userData.songCurrentTime again, so if you pause a song , then every time you click again the same song it starts over from the same songCurrentTime stored when it was originally paused, at least until you change the song and reset the time.

      <button class="playlist-song-info" onclick="playSong(${song.id})">

I think it’s not a desired behaviour, but not sure.

Thanks! I’m really enjoying freeCodeCamp :slight_smile:

In other words, to reproduce:

  • Press Start button
  • Press Pause button
  • Press song name on the list
  • Song will continue to play from the paused point, instead starting from the beginning

This is indeed behavior that might be slightly surprising. Would you be interested in creating issue for it on the github in the freeCodeCamp repository? Sign in to GitHub · GitHub

Hi! There are 2 ways to start playing a song: from the play icon and from the song’s info box.

To reproduce:

  • Start paying a song from anyway
  • Press Pause button , this saves the audio.currentTime in the userData object
  • Let the song advance
  • Now hit the song’s info box , the song will start from the stored currentTime not from 0 or any other behaviour (like pausing the song at least).
    (edit: the same happens if you hit the play button again)

Edit 2: Not only that, If you hit twice the same song, the first time you hit it it starts from 0, but the second, because it has the same ID, it starts from the stored paused time from a previous pause in maybe another song.

it’s not a bug I suppose, just an uncommon behaviour in Music Players.