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