/
libraryWithMethods.js
94 lines (77 loc) · 2.66 KB
/
libraryWithMethods.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var 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 (var plistID in this.playlists) {
var index = this.playlists[plistID];
var notracks = this.playlists[plistID].tracks.length;
console.log(index.id + ": ", index.name + " - " + notracks + " tracks");
}
},
printSongDetails: function (tr_id) {
var index = this.tracks[tr_id];
console.log(index.id + ": ", index.name + " by " + index.artist + " (" + index.album + ")");
},
printTracks: function () {
for (var songId in this.tracks) {
var index = this.tracks[songId];
console.log(index.id + ": ", index.name + " by " + index.artist + " (" + index.album + ")");
}
},
addTrackToPlaylist: function (trackId, playlistId) {
var addTr = this.playlists[playlistId].tracks;
addTr.push(trackId);
},
uid: function() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
},
createTrack: function(name, artist, album) {
return { id: this.uid(),
name: name,
artist: artist,
album: album
}
},
addTrack: function(name, artist, album) {
var newTrack = this.createTrack(name, artist, album)
library.tracks[newTrack.id] = newTrack //this is how to "push" for object (push function is only for arrays)
},
createPlaylist: function(name) {
return { id: this.uid(),
name: name
}
},
addPlaylist: function(name) {
var newPlaylist = this.createPlaylist(name)
this.playlists[newPlaylist.id] = newPlaylist
this.playlists[newPlaylist.id].tracks = {}
},
printPlaylist: function(playlistId) {
var index = this.playlists[playlistId];
var notracks = index.tracks.length;
console.log(index.id + ": ", index.name + " - " + notracks + " tracks");
for (var songId of this.playlists[playlistId].tracks) {
this.printSongDetails(songId);
}
}
}