Skip to content

Instantly share code, notes, and snippets.

@IrhaAli
Last active November 30, 2022 21:17
Show Gist options
  • Save IrhaAli/5e70b68181dd0f1d58d54b88db0085e2 to your computer and use it in GitHub Desktop.
Save IrhaAli/5e70b68181dd0f1d58d54b88db0085e2 to your computer and use it in GitHub Desktop.
Put all the functions for the music library in one object
const library = {
tracks: {
t01: {
id: "t01",
name: "Code Monkey",
artist: "Jonathan Coulton",
album: "Thing a Week Three"
},
t02: {
id: "t02",
name: "Model View Controller",
artist: "James Dempsey",
album: "WWDC 2003"
},
t03: {
id: "t03",
name: "Four Thirty-Three",
artist: "John Cage",
album: "Woodstock 1952"
}
},
playlists: {
p01: {
id: "p01",
name: "Coding Music",
tracks: ["t01", "t02"]
},
p02: {
id: "p02",
name: "Other Playlist",
tracks: ["t03"]
}
},
printPlaylists: function() {
// Loop through the playlists
for (const playlist in this.playlists) {
// Console the playlist
console.log(`${this.playlists[playlist]['id']}: ${this.playlists[playlist]['name']} - ${this.playlists[playlist].tracks.length}`);
}
},
printTracks: function() {
// Loop through the tracks
for (const track in this.tracks) {
// Console the tracks
console.log(`${this.tracks[track][`id`]}: ${this.tracks[track][`name`]} by ${this.tracks[track].artist} (${this.tracks[track][`album`]})`);
}
},
printPlaylist: function(playlistId) {
// Console the playlist
console.log(`${this.playlists[playlistId][`id`]}: ${this.playlists[playlistId][`name`]} - ${this.playlists[playlistId].tracks.length}`);
// Loop through the playlist
for (const track of this.playlists[playlistId].tracks) {
// Console the tracks
console.log(`${this.tracks[track][`id`]}: ${this.tracks[track][`name`]} by ${this.tracks[track].artist} (${this.tracks[track][`album`]})`);
}
},
addTrackToPlaylist: function(trackId, playlistId) {
if (!this.playlists[playlistId].tracks.includes(trackId)) {
this.playlists[playlistId].tracks.push(trackId);
}
},
generateUid: function() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
},
addTrack: function(name, artist, album) {
let trackId = this.generateUid();
this.tracks[trackId] = {
id: trackId,
name,
artist,
album,
};
},
addPlaylist: function(name) {
const id = this.generateUid();
console.log(id);
this.playlists[id] = { id, name, tracks: [] };
},
printSearchResults: function(query) {
// lowercase the query
let noCapQuery = query.toLowerCase();
// iterate over every track
for (const track in this.tracks) {
// lowercase the artist, album and name
let noCapTrack = [this.tracks[track].name.toLowerCase(), this.tracks[track].artist.toLowerCase(), this.tracks[track].album.toLowerCase()];
// for each track check if name, album or artist has the query
if ((noCapTrack[0].search(noCapQuery) !== -1) || (noCapTrack[1].search(noCapQuery) !== -1) || (noCapTrack[2].search(noCapQuery) !== -1)) {
// if it does then you console.log the track
console.log(this.tracks[track]);
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment