Skip to content

Instantly share code, notes, and snippets.

@alex-hladun
Forked from kvirani/library.js
Last active May 29, 2020 01:56
Show Gist options
  • Save alex-hladun/beba2557adfbdb7d287e36306f6bf7d9 to your computer and use it in GitHub Desktop.
Save alex-hladun/beba2557adfbdb7d287e36306f6bf7d9 to your computer and use it in GitHub Desktop.
Music Library Exercise
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() {
for (const playlist in this.playlists) {
const pName = this.playlists[playlist].name;
const trackCount = this.playlists[playlist].tracks.length;
console.log(`${playlist}: ${pName} - ${trackCount} tracks`)
}
},
printTracks: function() {
for (let item in this['tracks']) {
let trackID = this['tracks'][item]['id'];
let trackName = this['tracks'][item]['name'];
let trackArtist = this['tracks'][item]['artist'];
let trackAlbum = this['tracks'][item]['album'];
console.log(`${trackID}: ${trackName} by ${trackArtist} (${trackAlbum})`);
}
},
printPlaylistTracks : function(trackID) {
for (let item in this['tracks']) {
let trackID = this['tracks'][item]['id'];
let trackName = this['tracks'][item]['name'];
let trackArtist = this['tracks'][item]['artist'];
let trackAlbum = this['tracks'][item]['album'];
console.log(`${trackID}: ${trackName} by ${trackArtist} (${trackAlbum})`);
}
},
printPlaylist : function(playlistId) {
for (let pList in this['playlists']) {
if (pList === playlistId) {
let pLName = this['playlists'][playlistId]['name'];
let pLLength = this['playlists'][playlistId]['tracks'].length;
console.log(`${playlistId}: ${pLName} - ${pLLength} tracks`);
for (let track of this['playlists'][playlistId]['tracks']) {
let trackName = this['tracks'][track]['name'];
let trackArtist = this['tracks'][track]['artist'];
let trackAlbum = this['tracks'][track]['album'];
console.log(`${track}: ${trackName} by ${trackArtist} (${trackAlbum})`);
}
}
}
},
addTrackToPlaylist : function(trackId, playlistId) {
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 = generateUid();
let trackID = "p09";
this['tracks'][trackID] = {id: trackID, name: name, artist: artist, album: album};
},
addPlaylist : function(name) {
// let playlistId = generateUid();
let playlistId = "p03";
this['playlists'][playlistId] = {id: playlistId, name: name, tracks: []};
},
printSearchResults : function(query) {
const searchResults = {};
for (let trackID in this['tracks']) {
const trackNameLower = this.tracks[trackID]['name'].toLowerCase();
const artistNameLower = this.tracks[trackID]['artist'].toLowerCase();
const albumNameLower = this.tracks[trackID]['album'].toLowerCase();
let trackName = this.tracks[trackID]['name'];
let trackArtist = this['tracks'][trackID]['artist'];
let trackAlbum = this['tracks'][trackID]['album'];
if (trackNameLower.search(query.toLowerCase()) !== -1) {
searchResults[trackID] = {id: trackID, name: trackName, artist: trackArtist, album: trackAlbum, "Search Hit" : "Track Name"};
}
if (artistNameLower.search(query.toLowerCase()) !== -1) {
searchResults[trackID] = {id: trackID, name: trackName, artist: trackArtist, album: trackAlbum, "Search Hit" : "Artist Name"};
}
if (albumNameLower.search(query.toLowerCase()) !== -1) {
searchResults[trackID] = {id: trackID, name: trackName, artist: trackArtist, album: trackAlbum, "Search Hit" : "Album Name"};
}
}
console.log("Search Results:");
console.log(searchResults);
return searchResults;
}
};
library.printTracks();
library.addTrack("Wandering Souls", "Tom Budin", "Wondering Souls - Single");
library.printSearchResults("Budin");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment