Build a Playlist Remix Engine - Build a Playlist Remix Engine

Tell us what’s happening:

So I do not understand what the scoreTracks actually wants.
I am pretty sure, that my method is the correct one (or so I thought), but when I used another one from another user, it was actually the correct one?

But the other user didn’t use the flattenedPlaylist, instead it used the original and it calculated the score that way.

It would also solve the fact, that step 5, 6, 8 and 12 would be solved (haven’t tried 14 yet).

So I am pretty confused about this lab and need help yeah

Your code so far

const playlists = [
  [
    {
      trackId: "trk101",
      artist: "Velvet Comet",
      title: "Crimson Afterglow",
      votes: 5,
      bpm: 122
    },
    {
      trackId: "trk101",
      artist: "Rawr",
      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){
  const flatPlaylist = [];
  let trackIndex = 0;

  if (!Array.isArray(playlists)){
    return flatPlaylist;
  }
  
  for (let i = 0; i < playlists.length; i++){
    for (let j = 0; j < playlists[i].length; j++){
      playlists[i][j].source = [i, j];
      flatPlaylist.push(playlists[i][j]);
    }
  }

  return flatPlaylist;
}

function scoreTracks(tracks){
  const flatPlaylist = flattenPlaylists(tracks);

  for (const songs of flatPlaylist){
    songs.score = songs.votes * 10 - Math.abs(songs.bpm - 120);
  }

  return flatPlaylist;
}

function dedupeTracks(tracks){
  const scorePlaylist = scoreTracks(tracks);
  const dedupedPlaylist = [];
  const trackIdHistory = [];

  for (const songs of scorePlaylist){
    if (trackIdHistory.includes(songs.trackId)){
      continue;
    }
    trackIdHistory.push(songs.trackId);
    dedupedPlaylist.push(songs);
  }

  return dedupedPlaylist;
}

function enforceArtistQuota(tracks, maxOcc){
  const dedupedPlaylist = dedupeTracks(tracks);
  const limitedPlaylist = [];
  const completedArtist = [];

  function countOccurences(artist){
    let counter = 0;

    for (const songs of dedupedPlaylist){
      if (songs.artist !== artist){
        continue;
      } else if (completedArtist.includes(artist)){
        return;
      }

      limitedPlaylist.push(songs);
      counter++;

      if (counter === maxOcc){
        completedArtist.push(artist);
        return;
      }
    }
  }

  for (const songs of dedupedPlaylist){
    countOccurences(songs.artist);
  }

  return limitedPlaylist;
}

function buildSchedule(tracks, maxOcc){
  const limitedPlaylist = enforceArtistQuota(tracks, maxOcc);
  const schedulePlaylist = [];

  for (let i = 0; i < limitedPlaylist.length; i++){
    schedulePlaylist.push({
      slot: i + 1,
      trackId: limitedPlaylist[i].trackId
    });
  }

  return schedulePlaylist;
}

function remixPlaylist(playlists, maxOcc){
  const schedulePlaylist = buildSchedule(playlists, maxOcc);
  return schedulePlaylist;
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:150.0) Gecko/20100101 Firefox/150.0

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

Hi @Ninge3007,

I think the best way to explain how this should work is to read again User Story #7 where is says the final function, remixPlaylist should

return the final broadcast schedule as an array of { slot, trackId } objects, by calling flattenPlaylists , scoreTracks , dedupeTracks , enforceArtistQuota , and buildSchedule in order.

Keeping that in mind, you shouldn’t need to call the previous function in each of your functions:

Happy coding!

got it, the words “accepts an array of track objects as returned by functionname” confused me xD