Exercism-JS/ozans-playlist
Andrew W 3083663a71 completed ozans-playlist,
started translation-service
2022-05-16 12:57:26 -05:00
..
.exercism completed factory-sensors, 2022-05-15 15:19:14 -05:00
.eslintrc completed factory-sensors, 2022-05-15 15:19:14 -05:00
.gitignore completed factory-sensors, 2022-05-15 15:19:14 -05:00
.npmrc completed factory-sensors, 2022-05-15 15:19:14 -05:00
babel.config.js completed factory-sensors, 2022-05-15 15:19:14 -05:00
HELP.md completed factory-sensors, 2022-05-15 15:19:14 -05:00
HINTS.md completed factory-sensors, 2022-05-15 15:19:14 -05:00
LICENSE completed factory-sensors, 2022-05-15 15:19:14 -05:00
ozans-playlist.js completed ozans-playlist, 2022-05-16 12:57:26 -05:00
ozans-playlist.spec.js completed factory-sensors, 2022-05-15 15:19:14 -05:00
package.json completed factory-sensors, 2022-05-15 15:19:14 -05:00
README.md completed factory-sensors, 2022-05-15 15:19:14 -05:00
yarn.lock completed factory-sensors, 2022-05-15 15:19:14 -05:00

Ozan's Playlist

Welcome to Ozan's Playlist on Exercism's JavaScript Track. If you need help running the tests or submitting your code, check out HELP.md. If you get stuck on the exercise, check out HINTS.md, but try and solve it without using those first :)

Introduction

In JavaScript, a set is a list-like structure containing unique values, which can be primitives and/or object references. Unlike an array, a set's elements cannot be accessed by index.

A value cannot be added to a set if it is strictly equal to any of the set's elements.

const set = new Set();
const object = { color: 'lime green' };
const functionallyIdenticalObject = { color: 'lime green' };

set.add(object);
set.add('wow');
set.add(77);

console.log(set.size);
//=> 3

set.add(functionallyIdenticalObject); // added because functionallyIdenticalObject is not strictly equal to object
console.log(set.size);
//=> 4

set.add(77); // not added because 77 is strictly equal to 77
console.log(set.size);
//=> 4

Instructions

Ozan is putting together a playlist for an upcoming roadtrip. He doesn't want to hear the same track more than once, but the playlist has gotten so long that he's having trouble remembering which tracks have already been added.

The API for Ozan's music player only knows how to work with arrays, so he attempts to write some code that uses Array.indexOf() to check for the presence of a track before adding it to the playlist. Unfortunately, his program takes much too long to execute. He needs your help!

Coming to Ozan's aid, you are astonished to find that his playlist contains half a million tracks. Perhaps you know of a different data structure that will allow you to manipulate the playlist more efficiently?

1. Remove duplicate tracks

Implement the removeDuplicates function, which takes a playlist as a parameter and returns a new playlist where all the tracks are unique.

const playlist = [
  'Court and Spark - Joni Mitchell',
  'Big Yellow Taxi - Joni Mitchell',
  'Court and Spark - Joni Mitchell',
];

removeDuplicates(playlist);
//=> ['Court and Spark - Joni Mitchell', 'Big Yellow Taxi - Joni Mitchell']

2. Check whether a track has already been added

Implement the hasTrack function, which takes a playlist and a track as parameters and returns a boolean that indicates whether the playlist contains the track.

const playlist = [
  'The Fashion Show - Grace Jones',
  'Dr. Funkenstein - Parliament',
];

hasTrack(playlist, 'Dr. Funkenstein - Parliament');
//=> true

hasTrack(playlist, 'Walking in the Rain - Grace Jones');
//=> false

3. Add a track

Implement the addTrack function, which takes a playlist and a track as parameters and returns a new playlist that includes the track.

const playlist = ['Selma - Bijelo Dugme'];

addTrack(playlist, 'Atomic Dog - George Clinton');
//=> ['Selma - Bijelo Dugme', 'Atomic Dog - George Clinton']

addTrack(playlist, 'Selma - Bijelo Dugme');
//=> ['Selma - Bijelo Dugme', 'Atomic Dog - George Clinton']

4. Delete a track

Implement the deleteTrack function, which takes a playlist and a track as parameters and returns a new playlist that does not include the track.

const playlist = [
  'The Treasure - Fra Lippo Lippi',
  'After the Fall - Klaus Nomi',
];

deleteTrack(playlist, 'The Treasure - Fra Lippo Lippi');
//=> ['After the Fall - Klaus Nomi']

deleteTrack(playlist, 'I Feel the Magic - Belinda Carlisle');
//=> ['After the Fall - Klaus Nomi']

5. List unique artists

Implement the listArtists function, which takes a playlist as a parameter and returns the list of unique artists in the playlist. Note that the names of the tracks are formatted like <SONG> - <ARTIST>.

const playlist = [
  'All Mine - Portishead',
  'Sight to Behold - Devendra Banhart',
  'Sour Times - Portishead',
];

listArtists(playlist);
//=> ['Portishead', 'Devendra Banhart']

Source

Created by

  • @kristinaborn

Contributed to by

  • @SleeplessByte