From 134a1cadd2f37891ec23f080cd4277fae6f8716d Mon Sep 17 00:00:00 2001 From: DanielPhilosoph Date: Mon, 6 Sep 2021 16:35:22 +0300 Subject: [PATCH 01/18] Play Song Sends the song playing with all of him info in a specific format --- index.js | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 10f4784..5288c23 100644 --- a/index.js +++ b/index.js @@ -47,21 +47,46 @@ const player = { { id: 1, name: 'Metal', songs: [1, 7, 4] }, { id: 5, name: 'Israeli', songs: [4, 5] }, ], + playSong(song) { - console.log(/* your code here */) + const songObj = this.fingObjectByID(song); + // If not exists + if(songObj === undefined){ + throw ("non existent ID") + } + else{ + console.log("Playing " + songObj.title + " from " + songObj.album + " by " + songObj.artist + " | " + player.calcPlayTime(songObj.duration) + "."); + } + }, + + // ===> Reformat from seconds to MM:SS <=== + calcPlayTime(durationTime) { + const min = Math.floor(durationTime / 60); + const sec = durationTime - min * 60; + // Should add 0 before the number? numberS = number as string + const numberS1 = (min < 10) ? "0" : ""; + const numberS2 = (sec < 10) ? "0" : ""; + return numberS1 + min + ":" + numberS2 + sec; }, + + // ===> Returns the song by the ID given <=== + fingObjectByID(id){ + return player.songs.find(songObj => songObj.id === id); + } } function playSong(id) { - // your code here + player.playSong(id); } function removeSong(id) { - // your code here + } + +// Params: String String String MM:SS Optional function addSong(title, album, artist, duration, id) { - // your code here + } function removePlaylist(id) { From 7fe760af9f4ed7be25de5cbcc73ec5ed45364c53 Mon Sep 17 00:00:00 2001 From: DanielPhilosoph Date: Mon, 6 Sep 2021 16:37:42 +0300 Subject: [PATCH 02/18] Remove Song Removes the song with an identical ID from songs and playlists --- index.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 5288c23..8ede956 100644 --- a/index.js +++ b/index.js @@ -80,7 +80,27 @@ function playSong(id) { } function removeSong(id) { - + const songObj = player.fingObjectByID(id); + // If ID does not exists + if(songObj === undefined){ + throw ("non existent ID"); + } + // If ID does exists - remove + else{ + // Remove from songs + player.songs.forEach((songObj, index) => { + if(songObj.id === id) + player.songs.splice(index, 1); + }); + + // Remove from playlist + player.playlists.forEach(playlistObj => { + const index = playlistObj.songs.indexOf(id) + if(index > -1){ + playlistObj.songs.splice(index, 1); + } + }); + } } From 3a4138a79da73d703e656e8080fe74ba1329d2f9 Mon Sep 17 00:00:00 2001 From: DanielPhilosoph Date: Mon, 6 Sep 2021 21:48:05 +0300 Subject: [PATCH 03/18] Add Song Adding a song to player.songs. 1 of the test didnt work, will go over it later on. --- index.js | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 8ede956..d192d02 100644 --- a/index.js +++ b/index.js @@ -104,9 +104,51 @@ function removeSong(id) { } +// ===> Convert MM:SS to Seconds <=== +function from_Time_String_To_Seconds(duration){ + const newDuration = duration.split(":") + return parseInt(newDuration[0]) * 60 + parseInt(newDuration[1]); +} + +// ===> Generate a new ID <=== +function generate_ID(){ + let i = 1 + while(true){ + const songObj = player.fingObjectByID(i); + // If ID does not exists + if(songObj === undefined){ + return i; + } + i++; + } +} + + // Params: String String String MM:SS Optional function addSong(title, album, artist, duration, id) { - + const newDuration = from_Time_String_To_Seconds(duration); + //Check if ID is already exits + if(player.fingObjectByID(id) !== undefined){ + throw "That ID has been taken"; + } + // If ID doesnt omitted - generate ID + if(id === undefined){ + id = generate_ID() + } + else{ + // Checks If ID is a string + if(isNaN(id)){ + throw "ID must be a number"; + } + } + + player.songs.push({ + "title": title, + "album": album, + "duration": newDuration, + "artist": artist, + "id": id + }); } function removePlaylist(id) { From 03eba475c6efd11335b8aff7959ff3ee613dbf04 Mon Sep 17 00:00:00 2001 From: DanielPhilosoph Date: Mon, 6 Sep 2021 22:53:33 +0300 Subject: [PATCH 04/18] Update Add Song Fixed test #7 return id --- index.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index d192d02..206184d 100644 --- a/index.js +++ b/index.js @@ -72,6 +72,11 @@ const player = { // ===> Returns the song by the ID given <=== fingObjectByID(id){ return player.songs.find(songObj => songObj.id === id); + }, + + // ===> Returns the playlist by the ID given <=== + fingObjectPlaylistByID(id){ + return player.playlists.find(songObj => songObj.id === id); } } @@ -143,16 +148,20 @@ function addSong(title, album, artist, duration, id) { } player.songs.push({ - "title": title, - "album": album, - "duration": newDuration, - "artist": artist, - "id": id + title: title, + album: album, + duration: newDuration, + artist: artist, + id: id }); + + // Return ID + return id; } + function removePlaylist(id) { - // your code here + } function createPlaylist(name, id) { From ba6d54e8671890b8038ef7b10854a6193aba4c8c Mon Sep 17 00:00:00 2001 From: DanielPhilosoph Date: Mon, 6 Sep 2021 23:29:17 +0300 Subject: [PATCH 05/18] Remove Playlist removes a playlist if exists by ID --- index.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 206184d..80b8dae 100644 --- a/index.js +++ b/index.js @@ -90,6 +90,10 @@ function removeSong(id) { if(songObj === undefined){ throw ("non existent ID"); } + // If ID is not a number + else if(isNaN(id)){ + throw "ID must be a number"; + } // If ID does exists - remove else{ // Remove from songs @@ -132,10 +136,12 @@ function generate_ID(){ // Params: String String String MM:SS Optional function addSong(title, album, artist, duration, id) { const newDuration = from_Time_String_To_Seconds(duration); + //Check if ID is already exits if(player.fingObjectByID(id) !== undefined){ throw "That ID has been taken"; } + // If ID doesnt omitted - generate ID if(id === undefined){ id = generate_ID() @@ -154,14 +160,27 @@ function addSong(title, album, artist, duration, id) { artist: artist, id: id }); - + // Return ID return id; } function removePlaylist(id) { - + if(player.fingObjectPlaylistByID(id) === undefined){ + throw "non existent ID"; + } + // If ID is not a number + else if(isNaN(id)){ + throw "ID must be a number"; + } + else{ + // Removes from playlist + player.playlists.forEach((playlistObj, index) => { + if(playlistObj.id === id) + player.playlists.splice(index, 1); + }); + } } function createPlaylist(name, id) { From 52337e294c4d0adc6882d40b629bbe2c6226c7dd Mon Sep 17 00:00:00 2001 From: DanielPhilosoph Date: Mon, 6 Sep 2021 23:45:31 +0300 Subject: [PATCH 06/18] Create playlist Creating playlist - test prove. added a part to generate id which can also generate ID for playlist as well --- index.js | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 80b8dae..44b4b6c 100644 --- a/index.js +++ b/index.js @@ -119,10 +119,10 @@ function from_Time_String_To_Seconds(duration){ return parseInt(newDuration[0]) * 60 + parseInt(newDuration[1]); } -// ===> Generate a new ID <=== -function generate_ID(){ +// ===> Generate a new ID to songs <=== +function generate_ID(songOrPlaylist){ let i = 1 - while(true){ + while(songOrPlaylist === "song"){ const songObj = player.fingObjectByID(i); // If ID does not exists if(songObj === undefined){ @@ -130,6 +130,16 @@ function generate_ID(){ } i++; } + + // ===> Generate a new ID to playlist <=== + while(songOrPlaylist === "playlist"){ + const playlistObj = player.fingObjectPlaylistByID(i); + // If ID does not exists + if(playlistObj === undefined){ + return i; + } + i++; + } } @@ -144,7 +154,7 @@ function addSong(title, album, artist, duration, id) { // If ID doesnt omitted - generate ID if(id === undefined){ - id = generate_ID() + id = generate_ID("song") } else{ // Checks If ID is a string @@ -184,7 +194,30 @@ function removePlaylist(id) { } function createPlaylist(name, id) { - // your code here + //Check if ID is already exits + if(player.fingObjectPlaylistByID(id) !== undefined){ + throw "That ID has been taken"; + }; + + // If ID doesnt omitted - generate ID + if(id === undefined){ + id = generate_ID("playlist"); + } + else{ + // Checks If ID is a string + if(isNaN(id)){ + throw "ID must be a number"; + } + } + + player.playlists.push({ + id :id, + name: name, + songs: [] + }); + + //Return ID + return id; } function playPlaylist(id) { From 3d1036af7e7c191d5d2ff0088eae0bc3a35ac341 Mon Sep 17 00:00:00 2001 From: DanielPhilosoph Date: Tue, 7 Sep 2021 11:02:04 +0300 Subject: [PATCH 07/18] Fix bugs and create play playlist Fixing the ID check (if not a number throw ERR). Creating play playlist by given ID - uses the playSong function --- index.js | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 44b4b6c..958e694 100644 --- a/index.js +++ b/index.js @@ -50,8 +50,11 @@ const player = { playSong(song) { const songObj = this.fingObjectByID(song); + if(isNaN(song)){ + throw "ID must be a number"; + } // If not exists - if(songObj === undefined){ + else if(songObj === undefined){ throw ("non existent ID") } else{ @@ -86,14 +89,14 @@ function playSong(id) { function removeSong(id) { const songObj = player.fingObjectByID(id); - // If ID does not exists - if(songObj === undefined){ - throw ("non existent ID"); - } // If ID is not a number - else if(isNaN(id)){ + if(isNaN(id)){ throw "ID must be a number"; } + // If ID does not exists + else if(songObj === undefined){ + throw ("non existent ID"); + } // If ID does exists - remove else{ // Remove from songs @@ -176,14 +179,14 @@ function addSong(title, album, artist, duration, id) { } -function removePlaylist(id) { - if(player.fingObjectPlaylistByID(id) === undefined){ - throw "non existent ID"; - } +function removePlaylist(id) { // If ID is not a number - else if(isNaN(id)){ + if(isNaN(id)){ throw "ID must be a number"; } + else if(player.fingObjectPlaylistByID(id) === undefined){ + throw "non existent ID"; + } else{ // Removes from playlist player.playlists.forEach((playlistObj, index) => { @@ -221,9 +224,21 @@ function createPlaylist(name, id) { } function playPlaylist(id) { - // your code here + const playlistObj = player.fingObjectPlaylistByID(id); + // Checks If ID is a string + if(isNaN(id)){ + throw "ID must be a number"; + } + // Check if ID is exists in playlists + else if(playlistObj === undefined){ + throw "non existent ID"; + } + else{ + playlistObj.songs.forEach(song => playSong(song)); + } } + function editPlaylist(playlistId, songId) { // your code here } From 4179ee656e6d9e69f0b0d18aa1bb7678a1602fdc Mon Sep 17 00:00:00 2001 From: DanielPhilosoph Date: Tue, 7 Sep 2021 12:19:00 +0300 Subject: [PATCH 08/18] Edit Playlist added the function - pass all the test runs. Will work on side functions later on --- index.js | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 958e694..8476121 100644 --- a/index.js +++ b/index.js @@ -240,9 +240,41 @@ function playPlaylist(id) { function editPlaylist(playlistId, songId) { - // your code here + const myPlaylist = player.fingObjectPlaylistByID(playlistId); + const song = player.fingObjectByID(songId); + if(isNaN(playlistId)){ + throw "playlist ID must be a number"; + } + else if(isNaN(songId)){ + throw "song ID must be a number"; + } + else if(myPlaylist === undefined){ + throw "playlist dosent exists"; + } + else if(song === undefined){ + throw "song dosent exists"; + } + else{ + //If Song ID exists - remove + if(myPlaylist.songs.find(song => song === songId) === songId){ + const index = myPlaylist.songs.indexOf(songId); + if(index > -1){ + myPlaylist.songs.splice(index, 1); + //If it was the last song in the playlist - delete playlist + if(myPlaylist.songs.length === 0){ + removePlaylist(playlistId); + } + } + } + //If Song ID doesnt exists - add + else{ + myPlaylist.songs.push(songId); + } + } } + + function playlistDuration(id) { // your code here } From 00c7eb09aa2d146144a9b37160020b5531c4c911 Mon Sep 17 00:00:00 2001 From: DanielPhilosoph Date: Tue, 7 Sep 2021 22:30:13 +0300 Subject: [PATCH 09/18] playlist Duration time The function calculate the number of seconds in a playlist. Also changed some notes --- index.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 8476121..5701e61 100644 --- a/index.js +++ b/index.js @@ -122,9 +122,10 @@ function from_Time_String_To_Seconds(duration){ return parseInt(newDuration[0]) * 60 + parseInt(newDuration[1]); } -// ===> Generate a new ID to songs <=== +// ===> Generate a new ID <=== function generate_ID(songOrPlaylist){ let i = 1 + // To Songs while(songOrPlaylist === "song"){ const songObj = player.fingObjectByID(i); // If ID does not exists @@ -134,7 +135,7 @@ function generate_ID(songOrPlaylist){ i++; } - // ===> Generate a new ID to playlist <=== + // To Playlist while(songOrPlaylist === "playlist"){ const playlistObj = player.fingObjectPlaylistByID(i); // If ID does not exists @@ -272,11 +273,27 @@ function editPlaylist(playlistId, songId) { } } } +//editPlaylist(1,1) +//console.log(player.playlists); function playlistDuration(id) { - // your code here + const myPlaylist = player.fingObjectPlaylistByID(id); + if(isNaN(id)){ + throw "playlist ID must be a number"; + } + else if(myPlaylist === undefined){ + throw "playlist dosent exists"; + } + else{ + let durationInSeconds = 0; + myPlaylist.songs.forEach(song => { + const songObj = player.fingObjectByID(song); + durationInSeconds += songObj.duration; + }); + return durationInSeconds; + } } function searchByQuery(query) { From 2f338544a8d75b3a2ee86a25b80de0bffc25bcd1 Mon Sep 17 00:00:00 2001 From: DanielPhilosoph Date: Wed, 8 Sep 2021 14:31:12 +0300 Subject: [PATCH 10/18] Search by Query added the function, searching through songs and playlists and returns them back as an Object --- index.js | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 5701e61..4c08a9a 100644 --- a/index.js +++ b/index.js @@ -273,19 +273,16 @@ function editPlaylist(playlistId, songId) { } } } -//editPlaylist(1,1) -//console.log(player.playlists); - - function playlistDuration(id) { const myPlaylist = player.fingObjectPlaylistByID(id); + if(isNaN(id)){ throw "playlist ID must be a number"; } else if(myPlaylist === undefined){ throw "playlist dosent exists"; - } + } else{ let durationInSeconds = 0; myPlaylist.songs.forEach(song => { @@ -297,9 +294,43 @@ function playlistDuration(id) { } function searchByQuery(query) { - // your code here + const newQuery = query.toLowerCase(); + const objectReturned = { + "songs": [], + "playlists": [] + } + + player.songs.forEach(song => { + if(song.title.toLowerCase().includes(newQuery) || song.album.toLowerCase().includes(newQuery) || song.artist.toLowerCase().includes(newQuery)){ + objectReturned["songs"].push(song); + } + }); + // Sorting Songs by title + objectReturned.songs.sort(function(name1, name2) { + // Ignore uppercase and lowercase chars + var nameA = name1.title.toUpperCase(); + var nameB = name2.title.toUpperCase(); + return (nameA < nameB) ? -1 : (nameA > nameB) ? 1 : 0; + }) + + player.playlists.forEach(playlist => { + if(playlist.name.toLowerCase().includes(newQuery)){ + objectReturned["playlists"].push(playlist); + } + }); + + // Sorting Playlists by names + objectReturned.playlists.sort(function(name1, name2) { + // Ignore uppercase and lowercase chars + var nameA = name1.name.toUpperCase(); + var nameB = name2.name.toUpperCase(); + return (nameA < nameB) ? -1 : (nameA > nameB) ? 1 : 0; + }) + + return objectReturned; } + function searchByDuration(duration) { // your code here } From 2275e080500badeea5a1ba20508d1e6b99c2bb52 Mon Sep 17 00:00:00 2001 From: DanielPhilosoph Date: Wed, 8 Sep 2021 19:56:54 +0300 Subject: [PATCH 11/18] search Duration + move a function search duration pass 1 test - the second should go too, working on it. moved calaPlayTime to be a function of its own --- index.js | 82 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 27 deletions(-) diff --git a/index.js b/index.js index 4c08a9a..4330a33 100644 --- a/index.js +++ b/index.js @@ -49,7 +49,7 @@ const player = { ], playSong(song) { - const songObj = this.fingObjectByID(song); + const songObj = this.findSongByID(song); if(isNaN(song)){ throw "ID must be a number"; } @@ -58,27 +58,17 @@ const player = { throw ("non existent ID") } else{ - console.log("Playing " + songObj.title + " from " + songObj.album + " by " + songObj.artist + " | " + player.calcPlayTime(songObj.duration) + "."); + console.log("Playing " + songObj.title + " from " + songObj.album + " by " + songObj.artist + " | " + calcPlayTime(songObj.duration) + "."); } }, - // ===> Reformat from seconds to MM:SS <=== - calcPlayTime(durationTime) { - const min = Math.floor(durationTime / 60); - const sec = durationTime - min * 60; - // Should add 0 before the number? numberS = number as string - const numberS1 = (min < 10) ? "0" : ""; - const numberS2 = (sec < 10) ? "0" : ""; - return numberS1 + min + ":" + numberS2 + sec; - }, - // ===> Returns the song by the ID given <=== - fingObjectByID(id){ + findSongByID(id){ return player.songs.find(songObj => songObj.id === id); }, // ===> Returns the playlist by the ID given <=== - fingObjectPlaylistByID(id){ + findPlaylistByID(id){ return player.playlists.find(songObj => songObj.id === id); } } @@ -88,7 +78,7 @@ function playSong(id) { } function removeSong(id) { - const songObj = player.fingObjectByID(id); + const songObj = player.findSongByID(id); // If ID is not a number if(isNaN(id)){ throw "ID must be a number"; @@ -122,12 +112,22 @@ function from_Time_String_To_Seconds(duration){ return parseInt(newDuration[0]) * 60 + parseInt(newDuration[1]); } +// ===> Reformat from seconds to MM:SS <=== +function calcPlayTime(durationTime) { + const min = Math.floor(durationTime / 60); + const sec = durationTime - min * 60; + // Should add 0 before the number? numberS = number as string + const numberS1 = (min < 10) ? "0" : ""; + const numberS2 = (sec < 10) ? "0" : ""; + return numberS1 + min + ":" + numberS2 + sec; +} + // ===> Generate a new ID <=== function generate_ID(songOrPlaylist){ let i = 1 // To Songs while(songOrPlaylist === "song"){ - const songObj = player.fingObjectByID(i); + const songObj = player.findSongByID(i); // If ID does not exists if(songObj === undefined){ return i; @@ -137,7 +137,7 @@ function generate_ID(songOrPlaylist){ // To Playlist while(songOrPlaylist === "playlist"){ - const playlistObj = player.fingObjectPlaylistByID(i); + const playlistObj = player.findPlaylistByID(i); // If ID does not exists if(playlistObj === undefined){ return i; @@ -152,7 +152,7 @@ function addSong(title, album, artist, duration, id) { const newDuration = from_Time_String_To_Seconds(duration); //Check if ID is already exits - if(player.fingObjectByID(id) !== undefined){ + if(player.findSongByID(id) !== undefined){ throw "That ID has been taken"; } @@ -185,7 +185,7 @@ function removePlaylist(id) { if(isNaN(id)){ throw "ID must be a number"; } - else if(player.fingObjectPlaylistByID(id) === undefined){ + else if(player.findPlaylistByID(id) === undefined){ throw "non existent ID"; } else{ @@ -199,7 +199,7 @@ function removePlaylist(id) { function createPlaylist(name, id) { //Check if ID is already exits - if(player.fingObjectPlaylistByID(id) !== undefined){ + if(player.findPlaylistByID(id) !== undefined){ throw "That ID has been taken"; }; @@ -225,7 +225,7 @@ function createPlaylist(name, id) { } function playPlaylist(id) { - const playlistObj = player.fingObjectPlaylistByID(id); + const playlistObj = player.findPlaylistByID(id); // Checks If ID is a string if(isNaN(id)){ throw "ID must be a number"; @@ -241,8 +241,8 @@ function playPlaylist(id) { function editPlaylist(playlistId, songId) { - const myPlaylist = player.fingObjectPlaylistByID(playlistId); - const song = player.fingObjectByID(songId); + const myPlaylist = player.findPlaylistByID(playlistId); + const song = player.findSongByID(songId); if(isNaN(playlistId)){ throw "playlist ID must be a number"; } @@ -275,7 +275,7 @@ function editPlaylist(playlistId, songId) { } function playlistDuration(id) { - const myPlaylist = player.fingObjectPlaylistByID(id); + const myPlaylist = player.findPlaylistByID(id); if(isNaN(id)){ throw "playlist ID must be a number"; @@ -286,7 +286,7 @@ function playlistDuration(id) { else{ let durationInSeconds = 0; myPlaylist.songs.forEach(song => { - const songObj = player.fingObjectByID(song); + const songObj = player.findSongByID(song); durationInSeconds += songObj.duration; }); return durationInSeconds; @@ -325,16 +325,44 @@ function searchByQuery(query) { var nameA = name1.name.toUpperCase(); var nameB = name2.name.toUpperCase(); return (nameA < nameB) ? -1 : (nameA > nameB) ? 1 : 0; - }) + }); return objectReturned; } function searchByDuration(duration) { - // your code here + + const secondsDuration = from_Time_String_To_Seconds(duration); + let lowestReduceNumber = Math.abs(player.songs[0].duration - secondsDuration); + let selected_ID = 0; + let returnSongOrPlaylist = ""; + player.songs.forEach(song => { + if(lowestReduceNumber > Math.abs(song.duration - secondsDuration)){ + lowestReduceNumber = Math.abs(song.duration - secondsDuration); + selected_ID = song.id; + returnSongOrPlaylist = "song"; + } + }); + + player.playlists.forEach(playlist => { + if(lowestReduceNumber > Math.abs(playlistDuration(playlist.id) - secondsDuration)){ + lowestReduceNumber = Math.abs(playlistDuration(playlist.id) - secondsDuration); + selected_ID = playlist.id; + returnSongOrPlaylist = "playlist"; + } + }); + + if(returnSongOrPlaylist === "song"){ + return player.findSongByID(selected_ID); + } + else if (returnSongOrPlaylist === "playlist"){ + return player.findPlaylistByID(selected_ID); + } } + + module.exports = { player, playSong, From 50f9180ca14af5474b0b3ac31f872004e8bd9233 Mon Sep 17 00:00:00 2001 From: DanielPhilosoph Date: Thu, 9 Sep 2021 09:59:47 +0300 Subject: [PATCH 12/18] Removing comments and fix edge event at "createPlaylist" moved isNan() to the top of the function, must check validation before everything else --- index.js | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 4330a33..5efe5a0 100644 --- a/index.js +++ b/index.js @@ -197,40 +197,33 @@ function removePlaylist(id) { } } -function createPlaylist(name, id) { - //Check if ID is already exits +function createPlaylist(name, id) { + if(isNaN(id)){ + throw "ID must be a number"; + } if(player.findPlaylistByID(id) !== undefined){ throw "That ID has been taken"; }; - // If ID doesnt omitted - generate ID if(id === undefined){ id = generate_ID("playlist"); } - else{ - // Checks If ID is a string - if(isNaN(id)){ - throw "ID must be a number"; - } - } + player.playlists.push({ id :id, name: name, songs: [] }); - - //Return ID + return id; } function playPlaylist(id) { const playlistObj = player.findPlaylistByID(id); - // Checks If ID is a string if(isNaN(id)){ throw "ID must be a number"; - } - // Check if ID is exists in playlists + } else if(playlistObj === undefined){ throw "non existent ID"; } @@ -332,7 +325,9 @@ function searchByQuery(query) { function searchByDuration(duration) { - + if(player.songs.length === 0){ + throw "No songs exists"; + } const secondsDuration = from_Time_String_To_Seconds(duration); let lowestReduceNumber = Math.abs(player.songs[0].duration - secondsDuration); let selected_ID = 0; @@ -363,6 +358,8 @@ function searchByDuration(duration) { + + module.exports = { player, playSong, From 06e9c28919cb7ca9ed6b6d54251ed38e882b0d2b Mon Sep 17 00:00:00 2001 From: DanielPhilosoph Date: Thu, 9 Sep 2021 11:13:59 +0300 Subject: [PATCH 13/18] fix errors in serchByDuration Could not find if the first song was closest. Fixed --- index.js | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index 5efe5a0..5510bb7 100644 --- a/index.js +++ b/index.js @@ -79,11 +79,10 @@ function playSong(id) { function removeSong(id) { const songObj = player.findSongByID(id); - // If ID is not a number + if(isNaN(id)){ throw "ID must be a number"; - } - // If ID does not exists + } else if(songObj === undefined){ throw ("non existent ID"); } @@ -151,7 +150,7 @@ function generate_ID(songOrPlaylist){ function addSong(title, album, artist, duration, id) { const newDuration = from_Time_String_To_Seconds(duration); - //Check if ID is already exits + if(player.findSongByID(id) !== undefined){ throw "That ID has been taken"; } @@ -161,7 +160,6 @@ function addSong(title, album, artist, duration, id) { id = generate_ID("song") } else{ - // Checks If ID is a string if(isNaN(id)){ throw "ID must be a number"; } @@ -175,13 +173,11 @@ function addSong(title, album, artist, duration, id) { id: id }); - // Return ID return id; } -function removePlaylist(id) { - // If ID is not a number +function removePlaylist(id) { if(isNaN(id)){ throw "ID must be a number"; } @@ -198,9 +194,7 @@ function removePlaylist(id) { } function createPlaylist(name, id) { - if(isNaN(id)){ - throw "ID must be a number"; - } + if(player.findPlaylistByID(id) !== undefined){ throw "That ID has been taken"; }; @@ -208,13 +202,12 @@ function createPlaylist(name, id) { if(id === undefined){ id = generate_ID("playlist"); } - - - player.playlists.push({ - id :id, - name: name, - songs: [] - }); + else{ + if(isNaN(id)){ + throw "ID must be a number"; + } + } + player.playlists.push({id, name, songs: []}); return id; } @@ -287,14 +280,14 @@ function playlistDuration(id) { } function searchByQuery(query) { - const newQuery = query.toLowerCase(); + const lowerCaseQuery = query.toLowerCase(); const objectReturned = { "songs": [], "playlists": [] } player.songs.forEach(song => { - if(song.title.toLowerCase().includes(newQuery) || song.album.toLowerCase().includes(newQuery) || song.artist.toLowerCase().includes(newQuery)){ + if(song.title.toLowerCase().includes(lowerCaseQuery) || song.album.toLowerCase().includes(lowerCaseQuery) || song.artist.toLowerCase().includes(lowerCaseQuery)){ objectReturned["songs"].push(song); } }); @@ -307,7 +300,7 @@ function searchByQuery(query) { }) player.playlists.forEach(playlist => { - if(playlist.name.toLowerCase().includes(newQuery)){ + if(playlist.name.toLowerCase().includes(lowerCaseQuery)){ objectReturned["playlists"].push(playlist); } }); @@ -328,10 +321,13 @@ function searchByDuration(duration) { if(player.songs.length === 0){ throw "No songs exists"; } + else if(!check_Time_String_Validtion(duration)){ + throw "Duration is not valid (MM:SS)"; + } const secondsDuration = from_Time_String_To_Seconds(duration); let lowestReduceNumber = Math.abs(player.songs[0].duration - secondsDuration); - let selected_ID = 0; - let returnSongOrPlaylist = ""; + let selected_ID = player.songs[0].id; + let returnSongOrPlaylist = "song"; player.songs.forEach(song => { if(lowestReduceNumber > Math.abs(song.duration - secondsDuration)){ lowestReduceNumber = Math.abs(song.duration - secondsDuration); @@ -355,6 +351,13 @@ function searchByDuration(duration) { return player.findPlaylistByID(selected_ID); } } +console.log(searchByDuration('04:01')); + +// ===> Check if String is in MM:SS format <=== +function check_Time_String_Validtion(str){ + const regex = new RegExp('^[0-9]{2}:[0-9]{2}$'); + return regex.test(str); +} From dee242bffe297d0297d2dddf42572c47969f1f20 Mon Sep 17 00:00:00 2001 From: DanielPhilosoph Date: Fri, 10 Sep 2021 18:41:35 +0300 Subject: [PATCH 14/18] Adding new extra functions Added functions for code that repeats itself. Created a new area for those functions. Tests unharmed. --- index.js | 327 ++++++++++++++++++++++++------------------------------- 1 file changed, 145 insertions(+), 182 deletions(-) diff --git a/index.js b/index.js index 5510bb7..caa677a 100644 --- a/index.js +++ b/index.js @@ -49,17 +49,11 @@ const player = { ], playSong(song) { + id_is_int_and_does_exists(song, "song"); const songObj = this.findSongByID(song); - if(isNaN(song)){ - throw "ID must be a number"; - } - // If not exists - else if(songObj === undefined){ - throw ("non existent ID") - } - else{ - console.log("Playing " + songObj.title + " from " + songObj.album + " by " + songObj.artist + " | " + calcPlayTime(songObj.duration) + "."); - } + + console.log("Playing " + songObj.title + " from " + songObj.album + " by " + songObj.artist + " | " + calcPlayTime(songObj.duration) + "."); + }, // ===> Returns the song by the ID given <=== @@ -78,205 +72,81 @@ function playSong(id) { } function removeSong(id) { - const songObj = player.findSongByID(id); + id_is_int_and_does_exists(id, "song"); + remove_ID_from(id, "songs"); + + // Remove from playlist + player.playlists.forEach(playlistObj => { + const index = playlistObj.songs.indexOf(id) + if(index > -1){ + playlistObj.songs.splice(index, 1); + } + }); - if(isNaN(id)){ - throw "ID must be a number"; - } - else if(songObj === undefined){ - throw ("non existent ID"); - } - // If ID does exists - remove - else{ - // Remove from songs - player.songs.forEach((songObj, index) => { - if(songObj.id === id) - player.songs.splice(index, 1); - }); - - // Remove from playlist - player.playlists.forEach(playlistObj => { - const index = playlistObj.songs.indexOf(id) - if(index > -1){ - playlistObj.songs.splice(index, 1); - } - }); - } -} - - -// ===> Convert MM:SS to Seconds <=== -function from_Time_String_To_Seconds(duration){ - const newDuration = duration.split(":") - return parseInt(newDuration[0]) * 60 + parseInt(newDuration[1]); -} - -// ===> Reformat from seconds to MM:SS <=== -function calcPlayTime(durationTime) { - const min = Math.floor(durationTime / 60); - const sec = durationTime - min * 60; - // Should add 0 before the number? numberS = number as string - const numberS1 = (min < 10) ? "0" : ""; - const numberS2 = (sec < 10) ? "0" : ""; - return numberS1 + min + ":" + numberS2 + sec; -} - -// ===> Generate a new ID <=== -function generate_ID(songOrPlaylist){ - let i = 1 - // To Songs - while(songOrPlaylist === "song"){ - const songObj = player.findSongByID(i); - // If ID does not exists - if(songObj === undefined){ - return i; - } - i++; - } - - // To Playlist - while(songOrPlaylist === "playlist"){ - const playlistObj = player.findPlaylistByID(i); - // If ID does not exists - if(playlistObj === undefined){ - return i; - } - i++; - } } - -// Params: String String String MM:SS Optional function addSong(title, album, artist, duration, id) { - const newDuration = from_Time_String_To_Seconds(duration); - - - if(player.findSongByID(id) !== undefined){ - throw "That ID has been taken"; - } - - // If ID doesnt omitted - generate ID - if(id === undefined){ - id = generate_ID("song") - } - else{ - if(isNaN(id)){ - throw "ID must be a number"; - } - } + const convertedDuration = from_Time_String_To_Seconds(duration); + id = id_been_taken_or_generate(id, "song"); - player.songs.push({ - title: title, - album: album, - duration: newDuration, - artist: artist, - id: id - }); + player.songs.push({title, album, duration: convertedDuration, artist, id}); return id; } function removePlaylist(id) { - if(isNaN(id)){ - throw "ID must be a number"; - } - else if(player.findPlaylistByID(id) === undefined){ - throw "non existent ID"; - } - else{ - // Removes from playlist - player.playlists.forEach((playlistObj, index) => { - if(playlistObj.id === id) - player.playlists.splice(index, 1); - }); - } + id_is_int_and_does_exists(id, "playlist"); + remove_ID_from(id, "playlists"); } + + function createPlaylist(name, id) { - - if(player.findPlaylistByID(id) !== undefined){ - throw "That ID has been taken"; - }; - // If ID doesnt omitted - generate ID - if(id === undefined){ - id = generate_ID("playlist"); - } - else{ - if(isNaN(id)){ - throw "ID must be a number"; - } - } + id = id_been_taken_or_generate(id, "playlist"); player.playlists.push({id, name, songs: []}); return id; } function playPlaylist(id) { - const playlistObj = player.findPlaylistByID(id); - if(isNaN(id)){ - throw "ID must be a number"; - } - else if(playlistObj === undefined){ - throw "non existent ID"; - } - else{ - playlistObj.songs.forEach(song => playSong(song)); - } + id_is_int_and_does_exists(id, "playlist"); + const playlistObj = player.findPlaylistByID(id); + playlistObj.songs.forEach(song => playSong(song)); } - function editPlaylist(playlistId, songId) { const myPlaylist = player.findPlaylistByID(playlistId); const song = player.findSongByID(songId); - if(isNaN(playlistId)){ - throw "playlist ID must be a number"; - } - else if(isNaN(songId)){ - throw "song ID must be a number"; - } - else if(myPlaylist === undefined){ - throw "playlist dosent exists"; - } - else if(song === undefined){ - throw "song dosent exists"; + id_is_int_and_does_exists(playlistId, "playlist"); + id_is_int_and_does_exists(songId, "song"); + + if(myPlaylist.songs.find(song => song === songId) === songId){ + const index = myPlaylist.songs.indexOf(songId); + if(index > -1){ + myPlaylist.songs.splice(index, 1); + //If it was the last song in the playlist - delete playlist + if(myPlaylist.songs.length === 0){ + removePlaylist(playlistId); + } + } } + //If Song ID doesnt exists - add else{ - //If Song ID exists - remove - if(myPlaylist.songs.find(song => song === songId) === songId){ - const index = myPlaylist.songs.indexOf(songId); - if(index > -1){ - myPlaylist.songs.splice(index, 1); - //If it was the last song in the playlist - delete playlist - if(myPlaylist.songs.length === 0){ - removePlaylist(playlistId); - } - } - } - //If Song ID doesnt exists - add - else{ - myPlaylist.songs.push(songId); - } - } + myPlaylist.songs.push(songId); + } } function playlistDuration(id) { const myPlaylist = player.findPlaylistByID(id); + id_is_int_and_does_exists(id, "playlist"); - if(isNaN(id)){ - throw "playlist ID must be a number"; - } - else if(myPlaylist === undefined){ - throw "playlist dosent exists"; - } - else{ - let durationInSeconds = 0; - myPlaylist.songs.forEach(song => { - const songObj = player.findSongByID(song); - durationInSeconds += songObj.duration; - }); - return durationInSeconds; - } + let durationInSeconds = 0; + myPlaylist.songs.forEach(song => { + const songObj = player.findSongByID(song); + durationInSeconds += songObj.duration; + }); + return durationInSeconds; } function searchByQuery(query) { @@ -329,16 +199,18 @@ function searchByDuration(duration) { let selected_ID = player.songs[0].id; let returnSongOrPlaylist = "song"; player.songs.forEach(song => { - if(lowestReduceNumber > Math.abs(song.duration - secondsDuration)){ - lowestReduceNumber = Math.abs(song.duration - secondsDuration); + let diffrence = Math.abs(song.duration - secondsDuration); + if(lowestReduceNumber > diffrence){ + lowestReduceNumber = diffrence; selected_ID = song.id; returnSongOrPlaylist = "song"; } }); player.playlists.forEach(playlist => { - if(lowestReduceNumber > Math.abs(playlistDuration(playlist.id) - secondsDuration)){ - lowestReduceNumber = Math.abs(playlistDuration(playlist.id) - secondsDuration); + let diffrence = Math.abs(playlistDuration(playlist.id) - secondsDuration); + if(lowestReduceNumber > diffrence){ + lowestReduceNumber = diffrence; selected_ID = playlist.id; returnSongOrPlaylist = "playlist"; } @@ -351,7 +223,11 @@ function searchByDuration(duration) { return player.findPlaylistByID(selected_ID); } } -console.log(searchByDuration('04:01')); + + +//============================================== +// ============ Extra Functions ================ +//============================================== // ===> Check if String is in MM:SS format <=== function check_Time_String_Validtion(str){ @@ -359,6 +235,93 @@ function check_Time_String_Validtion(str){ return regex.test(str); } +// ===> removing ID from an attribute <=== +function remove_ID_from(id, attribute){ + player[attribute].forEach((Obj, index) => { + if(Obj.id === id) + player[attribute].splice(index, 1); + }); +} + +// ===> Returns ID. Checks if ID is taken - if not, generate ID and returns it <=== +function id_been_taken_or_generate(id, songOrPlaylist){ + let returnedObj; + if(songOrPlaylist === "song"){ + returnedObj = player.findSongByID(id); + } + else if (songOrPlaylist === "playlist"){ + returnedObj = player.findPlaylistByID(id); + } + + if(returnedObj !== undefined){ + throw "That ID has been taken"; + }; + // If ID doesnt omitted - generate ID + if(id === undefined){ + return generate_ID(songOrPlaylist); + } + else{ + if(isNaN(id)){ + throw "ID must be a number"; + } + } + return id; +} + +// ===> Validation of id in a playlist or song <=== +function id_is_int_and_does_exists(id, songOrPlaylist){ + let Obj; + if(songOrPlaylist === "song") + Obj = player.findSongByID(id); + else if(songOrPlaylist === "playlist") + Obj = player.findPlaylistByID(id); + + if(isNaN(id)){ + throw "ID must be a number"; + } + else if(Obj === undefined){ + throw "non existent ID"; + } +} + +// ===> Convert MM:SS to Seconds <=== +function from_Time_String_To_Seconds(duration){ + const newDuration = duration.split(":") + return parseInt(newDuration[0]) * 60 + parseInt(newDuration[1]); +} + +// ===> Reformat from seconds to MM:SS <=== +function calcPlayTime(durationTime) { + const min = Math.floor(durationTime / 60); + const sec = durationTime - min * 60; + // Should add 0 before the number? numberS = number as string + const numberS1 = (min < 10) ? "0" : ""; + const numberS2 = (sec < 10) ? "0" : ""; + return numberS1 + min + ":" + numberS2 + sec; +} + +// ===> Generate a new ID <=== +function generate_ID(songOrPlaylist){ + let i = 1 + // To Songs + while(songOrPlaylist === "song"){ + const songObj = player.findSongByID(i); + if(songObj === undefined){ + return i; + } + i++; + } + + // To Playlist + while(songOrPlaylist === "playlist"){ + const playlistObj = player.findPlaylistByID(i); + if(playlistObj === undefined){ + return i; + } + i++; + } +} + From d49c8aa00f03c0e6f3a39b3d180f6e0f303b38d7 Mon Sep 17 00:00:00 2001 From: DanielPhilosoph Date: Fri, 10 Sep 2021 23:41:55 +0300 Subject: [PATCH 15/18] Changed function, fixed edge at addSong Changed check_Time_String_Validtion to void function. Added a check validation at add song to "duration" --- index.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index caa677a..9de9cbd 100644 --- a/index.js +++ b/index.js @@ -52,8 +52,7 @@ const player = { id_is_int_and_does_exists(song, "song"); const songObj = this.findSongByID(song); - console.log("Playing " + songObj.title + " from " + songObj.album + " by " + songObj.artist + " | " + calcPlayTime(songObj.duration) + "."); - + console.log("Playing " + songObj.title + " from " + songObj.album + " by " + songObj.artist + " | " + calcPlayTime(songObj.duration) + "."); }, // ===> Returns the song by the ID given <=== @@ -86,22 +85,19 @@ function removeSong(id) { } function addSong(title, album, artist, duration, id) { + check_Time_String_Validtion(duration); const convertedDuration = from_Time_String_To_Seconds(duration); id = id_been_taken_or_generate(id, "song"); - player.songs.push({title, album, duration: convertedDuration, artist, id}); - + return id; } - function removePlaylist(id) { id_is_int_and_does_exists(id, "playlist"); remove_ID_from(id, "playlists"); } - - function createPlaylist(name, id) { id = id_been_taken_or_generate(id, "playlist"); player.playlists.push({id, name, songs: []}); @@ -186,18 +182,18 @@ function searchByQuery(query) { return objectReturned; } - function searchByDuration(duration) { if(player.songs.length === 0){ throw "No songs exists"; } - else if(!check_Time_String_Validtion(duration)){ - throw "Duration is not valid (MM:SS)"; + else { + check_Time_String_Validtion(duration); } const secondsDuration = from_Time_String_To_Seconds(duration); let lowestReduceNumber = Math.abs(player.songs[0].duration - secondsDuration); let selected_ID = player.songs[0].id; let returnSongOrPlaylist = "song"; + player.songs.forEach(song => { let diffrence = Math.abs(song.duration - secondsDuration); if(lowestReduceNumber > diffrence){ @@ -224,7 +220,6 @@ function searchByDuration(duration) { } } - //============================================== // ============ Extra Functions ================ //============================================== @@ -232,7 +227,8 @@ function searchByDuration(duration) { // ===> Check if String is in MM:SS format <=== function check_Time_String_Validtion(str){ const regex = new RegExp('^[0-9]{2}:[0-9]{2}$'); - return regex.test(str); + if(!regex.test(str)) + throw "Duration is not valid (MM:SS)"; } // ===> removing ID from an attribute <=== @@ -324,8 +320,6 @@ function generate_ID(songOrPlaylist){ - - module.exports = { player, playSong, From 5c8d952a27423be505a95e04e9a99b1a7f42366b Mon Sep 17 00:00:00 2001 From: DanielPhilosoph Date: Sat, 11 Sep 2021 14:07:00 +0300 Subject: [PATCH 16/18] Indentation and organization Fixed TABS, ";", and useless functions. Readable code - haven't changed any logics. --- index.js | 303 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 158 insertions(+), 145 deletions(-) diff --git a/index.js b/index.js index 9de9cbd..5a44b9f 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,6 @@ const player = { + MAX_SONGS: 100000, + MAX_PLAYLIST: 1000, songs: [ { id: 1, @@ -48,99 +50,131 @@ const player = { { id: 5, name: 'Israeli', songs: [4, 5] }, ], - playSong(song) { - id_is_int_and_does_exists(song, "song"); - const songObj = this.findSongByID(song); - + playSong(id) { + const songObj = this.findSongByID(id); console.log("Playing " + songObj.title + " from " + songObj.album + " by " + songObj.artist + " | " + calcPlayTime(songObj.duration) + "."); }, // ===> Returns the song by the ID given <=== - findSongByID(id){ - return player.songs.find(songObj => songObj.id === id); + findSongByID(id){ + return player.songs.find(songObj => songObj.id === id); }, // ===> Returns the playlist by the ID given <=== - findPlaylistByID(id){ - return player.playlists.find(songObj => songObj.id === id); + findPlaylistByID(id){ + return player.playlists.find(playlistObj => playlistObj.id === id); } } +//=============================================== +// ============ Player Functions ================ +//=============================================== + function playSong(id) { player.playSong(id); } function removeSong(id) { - id_is_int_and_does_exists(id, "song"); - remove_ID_from(id, "songs"); - - // Remove from playlist - player.playlists.forEach(playlistObj => { - const index = playlistObj.songs.indexOf(id) - if(index > -1){ - playlistObj.songs.splice(index, 1); - } - }); + let existFlag = false; + + validateID(id); + player.songs.forEach((Obj, index) => { + if(Obj.id === id){ + player.songs.splice(index, 1); + // Song Existed - remove from all playlists + player.playlists.forEach(playlistObj => { + const index = playlistObj.songs.indexOf(id); + if(index > -1){ + playlistObj.songs.splice(index, 1); + } + }); + existFlag = true; + } + }); + if (!existFlag) + throw "remove song: non existent Song ID: " + id; } -function addSong(title, album, artist, duration, id) { - check_Time_String_Validtion(duration); - const convertedDuration = from_Time_String_To_Seconds(duration); - id = id_been_taken_or_generate(id, "song"); - player.songs.push({title, album, duration: convertedDuration, artist, id}); +function addSong(title, album, artist, duration, id) { + + validateTimeString(duration); + validateString(title,"title"); + validateString(album,"album"); + validateString(artist,"artist"); + + const durationSec = convertTimeToSec(duration); + + // If ID omitted - generate ID + if(id === undefined){ + id = generateSongID(); + } + else{ + validateID(id); + if (player.findSongByID(id) !== undefined){ + throw "song already exist: " + id; + } + } + + player.songs.push({title, album, duration: durationSec, artist, id}); return id; } function removePlaylist(id) { - id_is_int_and_does_exists(id, "playlist"); - remove_ID_from(id, "playlists"); + validatePlaylist(id); + player.playlists.forEach((Obj, index) => { + if(Obj.id === id){ + player.playlists.splice(index, 1); + } + }); } -function createPlaylist(name, id) { - id = id_been_taken_or_generate(id, "playlist"); +function createPlaylist(name, id) { + validateString(name, "playlist name"); + + // If ID omitted - generate ID + if(id === undefined){ + id = generatePlaylistID(); + } + else{ + validateID(id); + if (player.findPlaylistByID(id) !== undefined) + throw "song already exist: " + id; + } + player.playlists.push({id, name, songs: []}); return id; } function playPlaylist(id) { - id_is_int_and_does_exists(id, "playlist"); + validatePlaylist(id); const playlistObj = player.findPlaylistByID(id); playlistObj.songs.forEach(song => playSong(song)); } function editPlaylist(playlistId, songId) { - const myPlaylist = player.findPlaylistByID(playlistId); - const song = player.findSongByID(songId); - id_is_int_and_does_exists(playlistId, "playlist"); - id_is_int_and_does_exists(songId, "song"); - - if(myPlaylist.songs.find(song => song === songId) === songId){ - const index = myPlaylist.songs.indexOf(songId); - if(index > -1){ - myPlaylist.songs.splice(index, 1); - //If it was the last song in the playlist - delete playlist - if(myPlaylist.songs.length === 0){ - removePlaylist(playlistId); - } - } - } - //If Song ID doesnt exists - add - else{ - myPlaylist.songs.push(songId); + validatePlaylist(playlistId); + validateSong(songId); + const myPlaylist = player.findPlaylistByID(playlistId); + let songIndex = myPlaylist.songs.indexOf(songId); + + if(songIndex > -1){ + myPlaylist.songs.splice(songIndex, 1); + if(myPlaylist.songs.length === 0) + removePlaylist(playlistId); } + else + myPlaylist.songs.push(songId); } -function playlistDuration(id) { +function playlistDuration(id) { + validatePlaylist(id); const myPlaylist = player.findPlaylistByID(id); - id_is_int_and_does_exists(id, "playlist"); - let durationInSeconds = 0; - myPlaylist.songs.forEach(song => { - const songObj = player.findSongByID(song); - durationInSeconds += songObj.duration; + myPlaylist.songs.forEach(song => { + durationInSeconds += player.findSongByID(song).duration; }); return durationInSeconds; } @@ -153,13 +187,15 @@ function searchByQuery(query) { } player.songs.forEach(song => { - if(song.title.toLowerCase().includes(lowerCaseQuery) || song.album.toLowerCase().includes(lowerCaseQuery) || song.artist.toLowerCase().includes(lowerCaseQuery)){ - objectReturned["songs"].push(song); + if(song.title.toLowerCase().includes(lowerCaseQuery) || + song.album.toLowerCase().includes(lowerCaseQuery) || + song.artist.toLowerCase().includes(lowerCaseQuery)){ + objectReturned.songs.push(song); } }); + // Sorting Songs by title - objectReturned.songs.sort(function(name1, name2) { - // Ignore uppercase and lowercase chars + objectReturned.songs.sort(function(name1, name2) { var nameA = name1.title.toUpperCase(); var nameB = name2.title.toUpperCase(); return (nameA < nameB) ? -1 : (nameA > nameB) ? 1 : 0; @@ -167,13 +203,12 @@ function searchByQuery(query) { player.playlists.forEach(playlist => { if(playlist.name.toLowerCase().includes(lowerCaseQuery)){ - objectReturned["playlists"].push(playlist); + objectReturned.playlists.push(playlist); } }); // Sorting Playlists by names - objectReturned.playlists.sort(function(name1, name2) { - // Ignore uppercase and lowercase chars + objectReturned.playlists.sort(function(name1, name2) { var nameA = name1.name.toUpperCase(); var nameB = name2.name.toUpperCase(); return (nameA < nameB) ? -1 : (nameA > nameB) ? 1 : 0; @@ -185,103 +220,84 @@ function searchByQuery(query) { function searchByDuration(duration) { if(player.songs.length === 0){ throw "No songs exists"; - } - else { - check_Time_String_Validtion(duration); - } - const secondsDuration = from_Time_String_To_Seconds(duration); - let lowestReduceNumber = Math.abs(player.songs[0].duration - secondsDuration); - let selected_ID = player.songs[0].id; - let returnSongOrPlaylist = "song"; - + } + validateTimeString(duration); + const secondsDuration = convertTimeToSec(duration); + + let lowestDiffrenceSec = Math.abs(player.songs[0].duration - secondsDuration); + let lowestDiffrenceID = player.songs[0].id; + let lowestDiffrenceType = "song"; + player.songs.forEach(song => { let diffrence = Math.abs(song.duration - secondsDuration); - if(lowestReduceNumber > diffrence){ - lowestReduceNumber = diffrence; - selected_ID = song.id; - returnSongOrPlaylist = "song"; + if(lowestDiffrenceSec > diffrence){ + lowestDiffrenceSec = diffrence; + lowestDiffrenceID = song.id; } }); player.playlists.forEach(playlist => { let diffrence = Math.abs(playlistDuration(playlist.id) - secondsDuration); - if(lowestReduceNumber > diffrence){ - lowestReduceNumber = diffrence; - selected_ID = playlist.id; - returnSongOrPlaylist = "playlist"; + if(lowestDiffrenceSec > diffrence){ + lowestDiffrenceSec = diffrence; + lowestDiffrenceID = playlist.id; + lowestDiffrenceType = "playlist"; } }); - if(returnSongOrPlaylist === "song"){ - return player.findSongByID(selected_ID); + if(lowestDiffrenceType === "song"){ + return player.findSongByID(lowestDiffrenceID); } - else if (returnSongOrPlaylist === "playlist"){ - return player.findPlaylistByID(selected_ID); + else if (lowestDiffrenceType === "playlist"){ + return player.findPlaylistByID(lowestDiffrenceID); } } //============================================== -// ============ Extra Functions ================ +// ============ Internal Functions ================ //============================================== // ===> Check if String is in MM:SS format <=== -function check_Time_String_Validtion(str){ +function validateTimeString(str){ const regex = new RegExp('^[0-9]{2}:[0-9]{2}$'); if(!regex.test(str)) - throw "Duration is not valid (MM:SS)"; + throw "Duration is not valid format (MM:SS) :" + str; } -// ===> removing ID from an attribute <=== -function remove_ID_from(id, attribute){ - player[attribute].forEach((Obj, index) => { - if(Obj.id === id) - player[attribute].splice(index, 1); - }); +function validateString(str, stringLabel){ + + if(str === "" || str === undefined) + throw stringLabel + " is not valid :" + str; } -// ===> Returns ID. Checks if ID is taken - if not, generate ID and returns it <=== -function id_been_taken_or_generate(id, songOrPlaylist){ - let returnedObj; - if(songOrPlaylist === "song"){ - returnedObj = player.findSongByID(id); - } - else if (songOrPlaylist === "playlist"){ - returnedObj = player.findPlaylistByID(id); - } - - if(returnedObj !== undefined){ - throw "That ID has been taken"; - }; - // If ID doesnt omitted - generate ID - if(id === undefined){ - return generate_ID(songOrPlaylist); - } - else{ - if(isNaN(id)){ - throw "ID must be a number"; - } - } - return id; +function validateSong(id){ + let Obj; + + validateID(id); + Obj = player.findSongByID(id); + if(Obj === undefined) + throw "non existent Song ID: " + id; } -// ===> Validation of id in a playlist or song <=== -function id_is_int_and_does_exists(id, songOrPlaylist){ +function validatePlaylist(id){ let Obj; - if(songOrPlaylist === "song") - Obj = player.findSongByID(id); - else if(songOrPlaylist === "playlist") - Obj = player.findPlaylistByID(id); - - if(isNaN(id)){ - throw "ID must be a number"; - } - else if(Obj === undefined){ - throw "non existent ID"; - } + + validateID(id); + Obj = player.findPlaylistByID(id); + if(Obj === undefined) + throw "non existent Song ID: " + id; } +// ===> Validation wheter id is a number <=== +function validateID(id){ + if(isNaN(id)) + throw "ID must be a number: " + id; + if (id === undefined) + throw "ID is undefined"; +} + // ===> Convert MM:SS to Seconds <=== -function from_Time_String_To_Seconds(duration){ +function convertTimeToSec(duration){ const newDuration = duration.split(":") return parseInt(newDuration[0]) * 60 + parseInt(newDuration[1]); } @@ -290,35 +306,32 @@ function from_Time_String_To_Seconds(duration){ function calcPlayTime(durationTime) { const min = Math.floor(durationTime / 60); const sec = durationTime - min * 60; - // Should add 0 before the number? numberS = number as string - const numberS1 = (min < 10) ? "0" : ""; - const numberS2 = (sec < 10) ? "0" : ""; - return numberS1 + min + ":" + numberS2 + sec; + // Should add 0 before the number? + const numberAsString1 = (min < 10) ? "0" : ""; + const numberAsString2 = (sec < 10) ? "0" : ""; + return numberAsString1 + min + ":" + numberAsString2 + sec; } - -// ===> Generate a new ID <=== -function generate_ID(songOrPlaylist){ - let i = 1 - // To Songs - while(songOrPlaylist === "song"){ +function generateSongID(){ + for (i=1; i < player.MAX_SONGS; i++) { const songObj = player.findSongByID(i); - if(songObj === undefined){ + if(songObj === undefined) return i; - } - i++; } + throw "Sorry, Songs reached max capacity"; +} - // To Playlist - while(songOrPlaylist === "playlist"){ +function generatePlaylistID(){ + for (i=1; i < player.MAX_PLAYLIST; i++) { const playlistObj = player.findPlaylistByID(i); - if(playlistObj === undefined){ + if(playlistObj === undefined) return i; - } - i++; } + throw "Sorry, Playlists reached max capacity"; } - +//============================================== +// ============ Test Export ================ +//============================================== module.exports = { player, From 6769e5f4006008f037978056628b83ee5568f0e9 Mon Sep 17 00:00:00 2001 From: DanielPhilosoph Date: Mon, 13 Sep 2021 16:37:30 +0300 Subject: [PATCH 17/18] nothing --- index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.js b/index.js index 5a44b9f..b2ce41b 100644 --- a/index.js +++ b/index.js @@ -69,6 +69,7 @@ const player = { //=============================================== // ============ Player Functions ================ //=============================================== +// function playSong(id) { player.playSong(id); @@ -311,6 +312,7 @@ function calcPlayTime(durationTime) { const numberAsString2 = (sec < 10) ? "0" : ""; return numberAsString1 + min + ":" + numberAsString2 + sec; } + function generateSongID(){ for (i=1; i < player.MAX_SONGS; i++) { const songObj = player.findSongByID(i); From 5aee839d5a250b602f9831938504f799f91490ef Mon Sep 17 00:00:00 2001 From: DanielPhilosoph Date: Mon, 13 Sep 2021 16:42:43 +0300 Subject: [PATCH 18/18] Update index.js --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index b2ce41b..b2e4afc 100644 --- a/index.js +++ b/index.js @@ -70,6 +70,7 @@ const player = { // ============ Player Functions ================ //=============================================== // +// function playSong(id) { player.playSong(id);