Build a Playlist Remix Engine - Build a Playlist Remix Engine

Tell us what’s happening:

I have all tests passing except for test 14 (the last test). I’m not sure what I’m doing wrong in the remixPlaylists function or if I even am doing anything wrong and this is just some weird bug. Anyone one know which it is and what I should do?

Your code so far

const playlists = [
  [
    {
      trackId: "trk101",
      artist: "Velvet Comet",
      title: "Crimson Afterglow",
      votes: 5,
      bpm: 122
    },
    {
      trackId: "trk102",
      artist: "Neon Harbor",
      title: "Static Horizon",
      votes: 2,
      bpm: 108
    },
    {
      trackId: "trk103",
      artist: "Lunar Arcade",
      title: "Midnight Frequency",
      votes: 4,
      bpm: 128
    }
  ],
  [
    {
      trackId: "trk201",
      artist: "Solar Echo",
      title: "Glass Skyline",
      votes: 3,
      bpm: 115
    },
    {
      trackId: "trk202",
      artist: "Velvet Comet",
      title: "Satellite Hearts",
      votes: 6,
      bpm: 124
    }
  ]
];

function flattenPlaylists(playlists){
  if(!Array.isArray(playlists)) return [];

  for(let playlist of playlists){
    for(let track of playlist){
      track.source = [playlists.indexOf(playlist), playlist.indexOf(track)];
    }
  }

  return playlists.flat();
}

function scoreTracks(playlist){
  for(let track of playlist){
    track.score = track.votes * 10 - Math.abs(track.bpm - 120);
  }

  return playlist;
}

function dedupeTracks(playlist){
  for(let i = 0; i < playlist.length; i++){
    for(let j = i + 1; j < playlist.length; j){
      if(playlist[j].trackId === playlist[i].trackId){
        playlist.splice(j,1);
        continue;
      }
      j++;
    }
  }
  return playlist;
}

function enforceArtistQuota(playlist, maxPerArtist){
  for(let i = 0; i < playlist.length; i++){
    let occur = 1;
    for(let j = i + 1; j < playlist.length; j){
      if(playlist[j].artist === playlist[i].artist){
        occur++;
      }
      if(occur > maxPerArtist){
        playlist.splice(j,1);
        continue;
      }
      j++;
    }
  }
  return playlist;
}

function buildSchedule(playlist){
  for (let track of playlist){
    let trackId = track.trackId;
    track.slot = playlist.indexOf(track) + 1;
    delete track.trackId;
    delete track.artist;
    delete track.title;
    delete track.votes;
    delete track.bpm;
    delete track.source;
    delete track.score;
    track.trackId = trackId;
  }
  return playlist;
}

function remixPlaylist(playlists, maxPerArtist) {
  return buildSchedule(enforceArtistQuota(dedupeTracks(scoreTracks(flattenPlaylists(playlists))), maxPerArtist));
}

Your browser information:

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

Challenge Information:

Build a Playlist Remix Engine - Build a Playlist Remix Engine

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-playlist-remix-engine/6976db7ecfa770bf21307b20.md at main · freeCodeCamp/freeCodeCamp · GitHub

hello @aca101 welcome to the forum!

here you are mutating (changing) the original playlist array received by the function. but when the tests run they expect the tracks in the original array to be as they were. you can verify this by doing this –

console.log(playlists[0]);
console.log(buildSchedule(playlists[0]))
console.log(playlists[0]);

it is generally a good practice to not mutate the original arrays or objects.

Thank you, I edited my flattenPlaylists function (since it also mutated the original array and is the first to do so) and instead created a copy of the playlists array to work on. Every test passed after that.