Build a Playlist Remix Engine - Build a Playlist Remix Engine

Tell us what’s happening:

I completed this lab (build a playlist remix engine) with all test passing.

However, it looks like that the test case #9 and #10 (responsible for enforceArtistQuota) is not correctly checking the return value. i.e if that function returns the less tracks (say 1) than the allowedOccurrences (say 2), the testcases looks passing.

Not sure, if I am missing anything.

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
    },
    {
      trackId: "trk202",
      artist: "Velvet Comet",
      title: "Satellite Hearts",
      votes: 6,
      bpm: 124
    }
  ]
];

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

  let flatPlaylist = [];
  for (let i = 0; i < playlists.length; i++ ) {
    for (let j = 0; j < playlists[i].length; j++) {
      const flattenedTrack = playlists[i][j];
      flattenedTrack.source = [];
      flattenedTrack.source[0] = i;
      flattenedTrack.source[1] = j;
      flatPlaylist.push(flattenedTrack);
    }
  }

  return flatPlaylist;
}

const flattenedPlaylist = flattenPlaylists(playlists);
// console.log(flattenedPlaylist);

function scoreTracks(tracks) {
  let tracksWithScore = [];
  for (let track of tracks) {
    let trackWithScore = structuredClone(track);
    trackWithScore.score = trackWithScore.votes * 10 - Math.abs(trackWithScore.bpm - 120);
    tracksWithScore.push(trackWithScore);
  }
  return tracksWithScore;

}

const tracksWithScore = scoreTracks(flattenedPlaylist);
// console.log(tracksWithScore);

function dedupeTracks(tracks) {
  let uniqueTracks = [];
  const allTracksId = [];

  for (let track of tracks) {
    allTracksId.push(track.trackId);
  }
  
  const uniqueTracksId = new Set(allTracksId);
  for (let trackId of uniqueTracksId) {
    for (let track of tracks) {
      if (trackId === track.trackId) {
        uniqueTracks.push(track);
        break;
      }
    }
  }

  return uniqueTracks;
}

const uniqueTracks = dedupeTracks(tracksWithScore);
console.log(uniqueTracks.length);



function enforceArtistQuota(tracks, quota) {
  let quotalTracks = [];
  const nonUniqueArtists = [];

  for (let track of tracks) {
    nonUniqueArtists.push(track.artist);
  }
  
  const uniqueArtists = new Set(nonUniqueArtists);

  for (let artist of uniqueArtists) {
    let trackCount = 0;
    for (let track of tracks) {
      if (track.artist === artist && trackCount < quota) {
        quotalTracks.push(track);
        trackCount++;
        // if (trackCount >= quota) {
        //   break;
        // }
        break; //test case #9 and #10 passing despite the possibly incorrect break logic here.
      }
    }
  }

  return quotalTracks;  
}

const quotalTracks = enforceArtistQuota(uniqueTracks,1);
console.log(quotalTracks.length);
console.log(quotalTracks);

function buildSchedule(tracks) {
  const trackSchedule = [];

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

  return trackSchedule;

}

const trackSchedule = buildSchedule(quotalTracks);
// console.log(trackSchedule);

function remixPlaylist(playlists, allowedOccurrences) {

  // return buildSchedule(enforceArtistQuota(dedupeTracks(scoreTracks(flattenPlaylists(playlists))), allowedOccurrences))

  const flattenedPlaylist = flattenPlaylists(playlists);
  const tracksWithScore = scoreTracks(flattenedPlaylist);
  const uniqueTracks = dedupeTracks(tracksWithScore);
  const quotalTracks = enforceArtistQuota(uniqueTracks, allowedOccurrences);
  const trackSchedule = buildSchedule(quotalTracks);
  return trackSchedule;

}


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:150.0) Gecko/20100101 Firefox/150.0

Challenge Information:

Build a Playlist Remix Engine - Build a Playlist Remix Engine

can you explain how you confirmed this please? (for eg if you used specific testing/logging, please share that with us in full)

Hi @hbar1st

Thanks for reaching out.

I tested with the same test sample provided with the lab (the array with 2 playlists; first one containing 3 tracks and 2nd one 2 tracks). I believe, when the enforceArtistQuota is called with allowedOccurrences = 2 then both tracks of “Velvet Comet” should be returned but when even 1 track is returned, test #9 and #10 seems passing.

user@localhost playlistRemixEngine % #With current submission
user@localhost playlistRemixEngine % #allowedOccurrences = 1, expected length of track = 4
user@localhost playlistRemixEngine % node script.js
4
[
{ slot: 1, trackId: ‘trk101’ },
{ slot: 2, trackId: ‘trk102’ },
{ slot: 3, trackId: ‘trk103’ },
{ slot: 4, trackId: ‘trk201’ }
]
user@localhost playlistRemixEngine % #allowedOccurrences = 2, expected length of track = 5
user@localhost playlistRemixEngine % node script.js
4
[
{ slot: 1, trackId: ‘trk101’ },
{ slot: 2, trackId: ‘trk102’ },
{ slot: 3, trackId: ‘trk103’ },
{ slot: 4, trackId: ‘trk201’ }
]
user@localhost playlistRemixEngine % #after correcting the enforceArtistQuota function
user@localhost playlistRemixEngine % #allowedOccurrences = 1, expected length of track = 4
user@localhost playlistRemixEngine % node script.js
4
[
{ slot: 1, trackId: ‘trk101’ },
{ slot: 2, trackId: ‘trk102’ },
{ slot: 3, trackId: ‘trk103’ },
{ slot: 4, trackId: ‘trk201’ }
]
user@localhost playlistRemixEngine % #allowedOccurrences = 2, expected length of track = 5
user@localhost playlistRemixEngine % node script.js
5
[
{ slot: 1, trackId: ‘trk101’ },
{ slot: 2, trackId: ‘trk202’ },
{ slot: 3, trackId: ‘trk102’ },
{ slot: 4, trackId: ‘trk103’ },
{ slot: 5, trackId: ‘trk201’ }
]

yes this is indeed a bug. Thanks for reporting.

Please go ahead and report it officially by opening a github issue as described below:

Thank you for helping make FCC better. Bugs can be reported as GitHub Issues. Whenever reporting a bug, please check first that there isn’t already an issue for it and provide as much detail as possible.

please include a link to the issue after you open though here so i can make a quick comment about where the test has gone wrong. thanks

@hbar1st

Thanks, I will do it.