Tell us what’s happening:
- When duplicate trackId values exist, dedupeTracks should keep only the first occurrence of the track.
tried all but no luck
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 [];
}
return playlists.flatMap((playlist, playlistIndex) =>
playlist.map((track, trackIndex) => (
{
...track,
source: [playlistIndex, trackIndex]
}
))
);
}
//console.log(flattenPlaylists(playlists));
let array=flattenPlaylists(playlists);
console.log(array);
function scoreTracks(array)
{
return array.map((track)=>({
...track,
score:(track.votes * 10) - Math.abs(track.bpm - 120) }));
}
let dupeTrack=scoreTracks(array);
console.log(dupeTrack);
//const seenTrackIds = new Set();
function dedupeTracks(dupeTrack)
{
let result=[];
const seen = new Set();
return dupeTrack.filter(track=>{
if (!seen.has(track.trackId)) {
seen.add(track.trackId);
result.push(track);
}
return result;
})
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36 Edg/147.0.0.0
Challenge Information:
Build a Playlist Remix Engine - Build a Playlist Remix Engine