From cf240aa758931d8f75c99d98c0f008ec8315e15c Mon Sep 17 00:00:00 2001 From: Roee Kazma Date: Thu, 9 Sep 2021 13:56:51 +0300 Subject: [PATCH 01/16] Made a fuction that find a song with an id input --- index.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/index.js b/index.js index 10f4784..9153839 100644 --- a/index.js +++ b/index.js @@ -92,6 +92,23 @@ function searchByDuration(duration) { // your code here } + +//#region ALL THE EXTRA FUNCTIONS +function findSongById (id) +{ + let idToSongConvertor= player["songs"].find(finder=> finder["id"]===id); + return idToSongConvertor; +} + + + + +//#endregion + + + + + module.exports = { player, playSong, From 24fea461a5cb4771fa0056877922cd23ac183f6a Mon Sep 17 00:00:00 2001 From: Roee Kazma Date: Thu, 9 Sep 2021 13:57:26 +0300 Subject: [PATCH 02/16] Made a function that convert time from seconds to the mm:ss format --- index.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/index.js b/index.js index 9153839..0eb43b7 100644 --- a/index.js +++ b/index.js @@ -101,7 +101,34 @@ function findSongById (id) } +function durationConvertor (duration) +{ + let min=""; + let sec=""; + if (typeof(duration) !== "number" || typeof(duration) === undefined) + { + throw "Please Enter a number" + } + min = Math.floor(duration/60); + sec = duration%60; + + if (min < 10 && sec < 10) //making sure the time format get out correctly + { + return("0"+min+":"+"0"+sec) + } + else if (min < 10 && sec > 10) + { + return("0"+min+":"+sec) + } + else if (min > 10 && sec < 10) + { + return(min+":"+"0"+sec) + } + else { + return(min+":"+sec) + } +} //#endregion From eb80ecf3b2bc0ead4379228f17ae7eb3ff08791b Mon Sep 17 00:00:00 2001 From: Roee Kazma Date: Thu, 9 Sep 2021 14:05:31 +0300 Subject: [PATCH 03/16] Added the playSong function --- index.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 0eb43b7..85e4aa1 100644 --- a/index.js +++ b/index.js @@ -48,12 +48,17 @@ const player = { { id: 5, name: 'Israeli', songs: [4, 5] }, ], playSong(song) { - console.log(/* your code here */) + return ("Playing "+ song.title + " from " + song.album + " by "+song.artist + " | "+durationConvertor(song.duration)+".") }, } function playSong(id) { - // your code here + let songPlayer = findSongById(id); + if (songPlayer === undefined) + { + throw "This is not an existing song ID" + } + console.log(player.playSong(songPlayer)); } function removeSong(id) { From bcbcc4abb3aeab80244a1db72f0bbab695ef2871 Mon Sep 17 00:00:00 2001 From: Roee Kazma Date: Thu, 9 Sep 2021 14:44:27 +0300 Subject: [PATCH 04/16] Added the remove song function --- index.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 85e4aa1..4799a07 100644 --- a/index.js +++ b/index.js @@ -62,7 +62,27 @@ function playSong(id) { } function removeSong(id) { - // your code here + if (findSongById(id)===undefined) + { + throw "This is not a valid ID" + } + else { + let songIndex= player.songs.indexOf(findSongById(id)); + player.songs.splice(songIndex,1); // removes the song from player.songs + + for (let i of player.playlists) // removes the song from all the playlists + { + for (let j = 0; j < i.songs.length; j++) + { + if (i.songs[j] === id) + { + i.songs.splice(j,1); + } + } + } + } + + } function addSong(title, album, artist, duration, id) { From ac5a086f88bfd2bc67385fd508465af56781cf20 Mon Sep 17 00:00:00 2001 From: Roee Kazma Date: Thu, 9 Sep 2021 15:56:09 +0300 Subject: [PATCH 05/16] Added the add song function --- index.js | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 4799a07..f410427 100644 --- a/index.js +++ b/index.js @@ -70,7 +70,7 @@ function removeSong(id) { let songIndex= player.songs.indexOf(findSongById(id)); player.songs.splice(songIndex,1); // removes the song from player.songs - for (let i of player.playlists) // removes the song from all the playlists + for (let i of player.playlists) // removes the song from all the playlists { for (let j = 0; j < i.songs.length; j++) { @@ -82,15 +82,39 @@ function removeSong(id) { } } - } function addSong(title, album, artist, duration, id) { - // your code here -} + if (findSongById(id) !== undefined) + { + throw "This ID already exist" + } + + if (id === undefined) + { + id= Math.floor(Math.random()*50); + while (id === findSongById(id)) //by defult the id will be a random number, but if there is already a song with the same id it will generate a new one until the new id is a unique one. + { + id= Math.floor(Math.random()*50); + } + } -function removePlaylist(id) { - // your code here + const newSong= // making a new song to push to the array + { + id: id, + title: title, + album: album, + artist: artist, + duration: duration + }; + player.songs.push(newSong); + console.log(newSong); + return newSong["id"]; + } + + + function removePlaylist(id) { + } function createPlaylist(name, id) { From efaf7d07ad134b30cea4a23d6619f024c96e1710 Mon Sep 17 00:00:00 2001 From: Roee Kazma Date: Thu, 9 Sep 2021 15:57:28 +0300 Subject: [PATCH 06/16] Added the Remove playlist function --- index.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index f410427..2cc090d 100644 --- a/index.js +++ b/index.js @@ -114,7 +114,19 @@ function addSong(title, album, artist, duration, id) { function removePlaylist(id) { - + let isExist=false; + for (let i = 0; i Date: Thu, 9 Sep 2021 16:25:12 +0300 Subject: [PATCH 07/16] Added the create playlist function , currently it passes 2/3 tests, i will fix it later --- index.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 2cc090d..835ef64 100644 --- a/index.js +++ b/index.js @@ -87,7 +87,7 @@ function removeSong(id) { function addSong(title, album, artist, duration, id) { if (findSongById(id) !== undefined) { - throw "This ID already exist" + throw "There is already a song with this ID" } if (id === undefined) @@ -130,9 +130,31 @@ function addSong(title, album, artist, duration, id) { } function createPlaylist(name, id) { - // your code here + if(findSongById(id) !== undefined) + { + throw "There is already a playlist with this ID"; + } + + if (id === undefined) + { + id = Math.floor(Math.random()*50); + while (id === findSongById(id)) //by defult the id will be a random number, but if there is already a song with the same id it will generate a new one until the new id is a unique one. + { + id= Math.floor(Math.random()*50); + } + } + const newPlaylist = { + id:id, + name:name, + songs:[] + }; + + player.playlists.push(newPlaylist); + console.log(newPlaylist); + return newPlaylist["id"]; } + function playPlaylist(id) { // your code here } From 423be468232e9ac74ad74c562a298ea5855c9030 Mon Sep 17 00:00:00 2001 From: Roee Kazma Date: Thu, 9 Sep 2021 17:20:12 +0300 Subject: [PATCH 08/16] Added play playlist function and a find a playlist by id function --- index.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 835ef64..2d31ad7 100644 --- a/index.js +++ b/index.js @@ -110,7 +110,7 @@ function addSong(title, album, artist, duration, id) { player.songs.push(newSong); console.log(newSong); return newSong["id"]; - } +} function removePlaylist(id) { @@ -156,7 +156,16 @@ function createPlaylist(name, id) { function playPlaylist(id) { - // your code here + if(findPlaylistById(id)===undefined) + { + throw "playlist id doesn't exist"; + } + + const currentPlaylist=findPlaylistById(id); + for(let i=0; i x["id"]===id); + return idToPlaylistConvertor; +} //#endregion From b407f0f157c02bb1a1727ea5d0604c527fd3dab9 Mon Sep 17 00:00:00 2001 From: Roee Kazma Date: Sat, 11 Sep 2021 13:21:10 +0300 Subject: [PATCH 09/16] Added the Playlist duration function --- index.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 2d31ad7..506212f 100644 --- a/index.js +++ b/index.js @@ -168,12 +168,21 @@ function playPlaylist(id) { } } -function editPlaylist(playlistId, songId) { - // your code here +function editPlaylist(playlistId, songId) { + } function playlistDuration(id) { - // your code here + let currentPlaylist = findPlaylistById(id); + let sum = 0; + + for(let num in currentPlaylist.songs) + { + sum += findSongById(currentPlaylist.songs[num]).duration; + } + console.log(sum); + return sum; + } function searchByQuery(query) { @@ -197,7 +206,7 @@ function durationConvertor (duration) { let min=""; let sec=""; - if (typeof(duration) !== "number" || typeof(duration) === undefined) + if (typeof(duration) !== "number") { throw "Please Enter a number" } From 30c71ce94e4b606e3fb5a4b1db9834f84fb2c067 Mon Sep 17 00:00:00 2001 From: Roee Kazma Date: Sat, 11 Sep 2021 13:39:39 +0300 Subject: [PATCH 10/16] Fixed create playlist function --- index.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 506212f..1a3ce0a 100644 --- a/index.js +++ b/index.js @@ -107,13 +107,15 @@ function addSong(title, album, artist, duration, id) { artist: artist, duration: duration }; + player.songs.push(newSong); console.log(newSong); + console.log(player.songs); return newSong["id"]; } - function removePlaylist(id) { +function removePlaylist(id) { let isExist=false; for (let i = 0; i Date: Sat, 11 Sep 2021 15:46:42 +0300 Subject: [PATCH 11/16] Added edit playlist function --- index.js | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 1a3ce0a..c2cd09e 100644 --- a/index.js +++ b/index.js @@ -85,6 +85,7 @@ function removeSong(id) { } function addSong(title, album, artist, duration, id) { + if (findSongById(id) !== undefined) { throw "There is already a song with this ID" @@ -92,14 +93,14 @@ function addSong(title, album, artist, duration, id) { if (id === undefined) { - id= Math.floor(Math.random()*50); + id = Math.floor(Math.random()*50); while (id === findSongById(id)) //by defult the id will be a random number, but if there is already a song with the same id it will generate a new one until the new id is a unique one. { - id= Math.floor(Math.random()*50); + id = Math.floor(Math.random()*50); } } - const newSong= // making a new song to push to the array + const newSong = // making a new song to push to the array { id: id, title: title, @@ -172,8 +173,32 @@ function playPlaylist(id) { function editPlaylist(playlistId, songId) { + let currentPlaylist = findPlaylistById(playlistId); + let currentSong= isSongInPlaylist(songId, playlistId) + + if (findSongById(songId) && findPlaylistById(playlistId)) + { + if (currentSong === -1) + { + currentPlaylist.songs.push(songId); + } + else if(currentPlaylist.songs.length > 1) + { + currentPlaylist.songs.splice(currentSong,1); + } + else + { + removePlaylist(currentPlaylist.id) + } + } + else + { + throw "This ID does not exist" + } + } + function playlistDuration(id) { let currentPlaylist = findPlaylistById(id); let sum = 0; @@ -204,6 +229,14 @@ function findSongById (id) return idToSongConvertor; } +function isSongInPlaylist (songId, playlistId) +{ + let playlist = findPlaylistById(playlistId); + for (let num in playlist.songs) + { + return playlist.songs[num] === songId ? num:-1; + } +} function durationConvertor (duration) { From 045587fe39a9bad960a67e804caede0de2dc1b25 Mon Sep 17 00:00:00 2001 From: Roee Kazma Date: Sat, 11 Sep 2021 16:50:43 +0300 Subject: [PATCH 12/16] Added search by duration function --- index.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index c2cd09e..d78ebae 100644 --- a/index.js +++ b/index.js @@ -178,7 +178,7 @@ function editPlaylist(playlistId, songId) { if (findSongById(songId) && findPlaylistById(playlistId)) { - if (currentSong === -1) + if (currentSong === false) { currentPlaylist.songs.push(songId); } @@ -209,7 +209,6 @@ function playlistDuration(id) { } console.log(sum); return sum; - } @@ -218,7 +217,36 @@ function searchByQuery(query) { } function searchByDuration(duration) { - // your code here + let songDuration = reverseDurationConvertor(duration); + let closestSong; + let closestPlaylist; + let lowestGapSong = 1000000; + let lowestGapPlaylist = 1000000; + for (let i in player.songs) + { + if (Math.abs(songDuration - player.songs[i].duration) < lowestGapSong) + { + lowestGapSong = Math.abs(songDuration - player.songs[i].duration); + closestSong = player.songs[i]; + } + } + for (let j of player.playlists) { + if (Math.abs(songDuration - playlistDuration(j.id)) < lowestGapPlaylist) + { + lowestGapPlaylist = Math.abs(songDuration - playlistDuration(j.id)); + closestPlaylist = j; + } + } + + if (lowestGapPlaylist < lowestGapSong) + { + return closestPlaylist; + } + else + { + return closestSong + } + } @@ -229,12 +257,20 @@ function findSongById (id) return idToSongConvertor; } -function isSongInPlaylist (songId, playlistId) +function isSongInPlaylist (songId, playlistId) // checks if there is a spesific song in a playlist, if there isnt it returns false, if there is it returns the index of the song { let playlist = findPlaylistById(playlistId); - for (let num in playlist.songs) + for (let i in playlist.songs) { - return playlist.songs[num] === songId ? num:-1; + if (playlist.songs[i] === songId) + { + return i; + } + else + { + return false; + } + } } @@ -267,6 +303,14 @@ function durationConvertor (duration) } +function reverseDurationConvertor (duration) +{ + duration = duration.split(':') + let minutes = parseInt(duration[0]) * 60 + let seconds = parseInt(duration[1]) + return minutes + seconds +} + function findPlaylistById(id) //return playlist object by id { let idToPlaylistConvertor= player.playlists.find(x=> x["id"]===id); @@ -279,6 +323,7 @@ function findPlaylistById(id) //return playlist object by id // playlistDuration(1) // playPlaylist(1) // createPlaylist("the Kazma playlist", 3) +// searchByDuration(200) //#endregion From a5433a03f04468c60422b324db09f48ae40c8955 Mon Sep 17 00:00:00 2001 From: Roee Kazma Date: Sat, 11 Sep 2021 17:33:23 +0300 Subject: [PATCH 13/16] Fixed AddSong function --- index.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index d78ebae..95539b7 100644 --- a/index.js +++ b/index.js @@ -42,6 +42,7 @@ const player = { artist: 'Full Trunk', duration: 259, }, + ], playlists: [ { id: 1, name: 'Metal', songs: [1, 7, 4] }, @@ -100,19 +101,19 @@ function addSong(title, album, artist, duration, id) { } } - const newSong = // making a new song to push to the array + const addedSong = // making a new song to push to the array { id: id, title: title, album: album, artist: artist, - duration: duration + duration: reverseDurationConvertor(duration) }; - player.songs.push(newSong); - console.log(newSong); + player.songs.push(addedSong); + console.log(addedSong); console.log(player.songs); - return newSong["id"]; + return addedSong["id"]; } @@ -213,7 +214,8 @@ function playlistDuration(id) { function searchByQuery(query) { - // your code here +let queryThings = {songs: songs, playlists: playlists}; + } function searchByDuration(duration) { From 44d63ca4885acb08612012a058b23ba9d46bfaf7 Mon Sep 17 00:00:00 2001 From: Roee Kazma Date: Sat, 11 Sep 2021 18:51:36 +0300 Subject: [PATCH 14/16] added search by query function --- index.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 95539b7..71775c6 100644 --- a/index.js +++ b/index.js @@ -213,9 +213,28 @@ function playlistDuration(id) { } -function searchByQuery(query) { -let queryThings = {songs: songs, playlists: playlists}; +function searchByQuery(query) { +let queryThings = {songs:[], playlists:[]}; +let lowerCaseQuery = query.toLowerCase(); + + +player.songs.forEach(song => { + if (song.title.toLocaleLowerCase().includes(lowerCaseQuery) || song.album.toLocaleLowerCase().includes(lowerCaseQuery)|| song.artist.toLocaleLowerCase().includes(lowerCaseQuery)) + { + queryThings.songs.push(song) + } +}) + +player.playlists.forEach(playlist => { + if (playlist.name.toLocaleLowerCase().includes(lowerCaseQuery)) + { + queryThings.playlists.push(playlist) + } +}); +queryThings.songs.sort((a, b) => (a.title > b.title) * 2 - 1) + +return queryThings; } function searchByDuration(duration) { @@ -326,6 +345,7 @@ function findPlaylistById(id) //return playlist object by id // playPlaylist(1) // createPlaylist("the Kazma playlist", 3) // searchByDuration(200) +// searchByQuery("stone"); //#endregion From 90cfe1a3229237d2bc026afe06fe7fd68a855e46 Mon Sep 17 00:00:00 2001 From: Roee Kazma Date: Sat, 11 Sep 2021 19:00:49 +0300 Subject: [PATCH 15/16] added some comments, the task is COMPLETED --- index.js | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 71775c6..ca92998 100644 --- a/index.js +++ b/index.js @@ -107,7 +107,7 @@ function addSong(title, album, artist, duration, id) { title: title, album: album, artist: artist, - duration: reverseDurationConvertor(duration) + duration: reverseDurationConvertor(duration) // making sure the duration gets in with the right format }; player.songs.push(addedSong); @@ -116,7 +116,6 @@ function addSong(title, album, artist, duration, id) { return addedSong["id"]; } - function removePlaylist(id) { let isExist=false; for (let i = 0; i { queryThings.playlists.push(playlist) } }); -queryThings.songs.sort((a, b) => (a.title > b.title) * 2 - 1) +queryThings.songs.sort((a, b) => (a.title > b.title) * 2 - 1) +console.log(queryThings); return queryThings; } @@ -243,7 +240,7 @@ function searchByDuration(duration) { let closestPlaylist; let lowestGapSong = 1000000; let lowestGapPlaylist = 1000000; - for (let i in player.songs) + for (let i in player.songs) // checks which songs is the closest to the given duration by comparing it to a "gap" variable. { if (Math.abs(songDuration - player.songs[i].duration) < lowestGapSong) { @@ -251,7 +248,7 @@ function searchByDuration(duration) { closestSong = player.songs[i]; } } - for (let j of player.playlists) { + for (let j of player.playlists) { if (Math.abs(songDuration - playlistDuration(j.id)) < lowestGapPlaylist) { lowestGapPlaylist = Math.abs(songDuration - playlistDuration(j.id)); @@ -259,7 +256,7 @@ function searchByDuration(duration) { } } - if (lowestGapPlaylist < lowestGapSong) + if (lowestGapPlaylist < lowestGapSong) // checks if the gap between the songs is lower or greater then the gap between the playlist { return closestPlaylist; } @@ -270,8 +267,8 @@ function searchByDuration(duration) { } - //#region ALL THE EXTRA FUNCTIONS + function findSongById (id) { let idToSongConvertor= player["songs"].find(finder=> finder["id"]===id); @@ -295,7 +292,7 @@ function isSongInPlaylist (songId, playlistId) // checks if there is a spesific } } -function durationConvertor (duration) +function durationConvertor (duration) // convert duration format from seconds to MM:SS format { let min=""; let sec=""; @@ -324,7 +321,7 @@ function durationConvertor (duration) } -function reverseDurationConvertor (duration) +function reverseDurationConvertor (duration) // convert duration format from MM:SS to seconds format { duration = duration.split(':') let minutes = parseInt(duration[0]) * 60 @@ -337,18 +334,19 @@ function findPlaylistById(id) //return playlist object by id let idToPlaylistConvertor= player.playlists.find(x=> x["id"]===id); return idToPlaylistConvertor; } + //#endregion //#region checking the functions + // addSong("New title", "New album", "New Album", 250, 10) // playlistDuration(1) // playPlaylist(1) // createPlaylist("the Kazma playlist", 3) // searchByDuration(200) -// searchByQuery("stone"); -//#endregion - +// searchByQuery("TAL"); +//#endregion module.exports = { player, From 166bd08eebcb45f7a57a00525f2a51b4a99dfa18 Mon Sep 17 00:00:00 2001 From: Roee Kazma Date: Sat, 11 Sep 2021 22:37:27 +0300 Subject: [PATCH 16/16] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index ca92998..64335bf 100644 --- a/index.js +++ b/index.js @@ -360,4 +360,4 @@ module.exports = { playlistDuration, searchByQuery, searchByDuration, -} +} \ No newline at end of file