For algorithm-heavy code that transforms arrays (multi-step pipelines, scheduling, deduping, cyclic traversal), require clear, testable function contracts and deterministic behavior:
[]).slot).nextIndex = (currentIndex + 1) % array.length.Example pattern (flatten → score → stable dedupe):
function flattenPlaylists(playlists) {
if (!Array.isArray(playlists)) return [];
const out = [];
playlists.forEach((pl, playlistIndex) => {
if (!Array.isArray(pl)) return;
pl.forEach((track, trackIndex) => {
out.push({
...track,
source: [playlistIndex, trackIndex],
});
});
});
return out;
}
function scoreTracks(tracks) {
return tracks.map(t => ({
...t,
score: t.votes * 10 - Math.abs(t.bpm - 120),
}));
}
function dedupeTracks(tracks) {
const seen = new Set();
const out = [];
for (const t of tracks) {
if (seen.has(t.trackId)) continue;
seen.add(t.trackId);
out.push(t);
}
return out; // keeps the earliest occurrence
}
Apply this standard anytime you see multi-step array algorithms or any logic depending on order/cycles—make those rules explicit in the functions’ behavior.
Enter the URL of a public GitHub repository