From d07fb68b43f8d9300c2732cd40ca7bc2cfa0a04b Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Mon, 6 Sep 2021 03:47:10 +0300 Subject: [PATCH 01/22] added a function that convert seconds to minutes (mm:ss format) --- index.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 10f4784..af36298 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,15 @@ +/* +this function gets one parameter seconds +and return a string value of the minutes +in 'mm:ss' format. +*/ +function toMinutes(sec) { + let minFormat = ["mm", "ss"]; + minFormat[1] = sec % 60 < 10 ? "0" + sec % 60: sec % 60; + minFormat[0] = sec / 60 < 10 ? "0" + Math.floor(sec / 60): Math.floor(sec / 60); + return minFormat.join(':'); +} + const player = { songs: [ { @@ -48,7 +60,7 @@ const player = { { id: 5, name: 'Israeli', songs: [4, 5] }, ], playSong(song) { - console.log(/* your code here */) + console.log(`Playing ${song.title} from ${song.album} by ${song.artist} | ${toMinutes(song.duration)}.`); }, } From d38e328869f454e64617f6a7dd6a3d55f6238e33 Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Mon, 6 Sep 2021 04:27:24 +0300 Subject: [PATCH 02/22] updated playSong function --- index.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index af36298..a016840 100644 --- a/index.js +++ b/index.js @@ -65,7 +65,14 @@ const player = { } function playSong(id) { - // your code here + let isExist = false; + for(song of player.songs) { + if(song.id === id) { + player.playSong(song); + isExist = true; + } + } + if(!isExist) throw 'song does not exist!'; } function removeSong(id) { From 8f494206616a7d6619f70077df149aa04a7fb04a Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Mon, 6 Sep 2021 04:36:00 +0300 Subject: [PATCH 03/22] updated playSongs function return and passed 2 tests --- index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index a016840..f528903 100644 --- a/index.js +++ b/index.js @@ -60,15 +60,18 @@ const player = { { id: 5, name: 'Israeli', songs: [4, 5] }, ], playSong(song) { - console.log(`Playing ${song.title} from ${song.album} by ${song.artist} | ${toMinutes(song.duration)}.`); + return `Playing ${song.title} from ${song.album} by ${song.artist} | ${toMinutes(song.duration)}.`; }, } +/* CHECKING deleteeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee lineeeeeeeeeeeeeeee*/ +// playSong(5); + function playSong(id) { let isExist = false; for(song of player.songs) { if(song.id === id) { - player.playSong(song); + console.log(player.playSong(song)); isExist = true; } } From 20dc2ea129cd833846c70db698fc2d56cd5a4718 Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Mon, 6 Sep 2021 06:10:51 +0300 Subject: [PATCH 04/22] modified the removeSong function but did not pass the tests --- index.js | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index f528903..30e34e0 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,24 @@ function toMinutes(sec) { return minFormat.join(':'); } +function removeSongFromPlayer(id) { + for(let i in player.songs) { + if(player.songs[i].id === id) { + player.songs.splice(i,1); + return true; + } + } + return false; +} + +function removeSongFromPlaylists(id) { + for(let playlist of player.playlists) { + for (let i in playlist.songs) { + if(playlist.songs[i] === id) playlist.songs.splice(i, 1); + } + } +} + const player = { songs: [ { @@ -65,11 +83,22 @@ const player = { } /* CHECKING deleteeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee lineeeeeeeeeeeeeeee*/ -// playSong(5); +// // let arr = [1,2,3,4]; +// // arr.splice(2,1); +// // console.log(arr); +// console.log(player.playlists); +// playSong(5); +// console.log(player.playlists); +// console.log(player.songs); +// removeSong(2); +// removeSong(5); +// console.log(player.playlists); +// console.log(player.songs); +// playSong(5); function playSong(id) { let isExist = false; - for(song of player.songs) { + for(let song of player.songs) { if(song.id === id) { console.log(player.playSong(song)); isExist = true; @@ -79,7 +108,9 @@ function playSong(id) { } function removeSong(id) { - // your code here + let isExist = removeSongFromPlayer(id); + if(!isExist) throw 'song does not exist!'; + removeSongFromPlaylists(id); } function addSong(title, album, artist, duration, id) { From 6976406ae6be525b054921de3ee1bb72f7a64c3a Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Mon, 6 Sep 2021 06:21:56 +0300 Subject: [PATCH 05/22] passed tests 3-5 --- index.js | 9 --------- 1 file changed, 9 deletions(-) diff --git a/index.js b/index.js index 30e34e0..b25dbc5 100644 --- a/index.js +++ b/index.js @@ -86,15 +86,6 @@ const player = { // // let arr = [1,2,3,4]; // // arr.splice(2,1); // // console.log(arr); -// console.log(player.playlists); -// playSong(5); -// console.log(player.playlists); -// console.log(player.songs); -// removeSong(2); -// removeSong(5); -// console.log(player.playlists); -// console.log(player.songs); -// playSong(5); function playSong(id) { let isExist = false; From 18df00770b6bcaabcefa594c1529a468dfb967cd Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Mon, 6 Sep 2021 06:49:53 +0300 Subject: [PATCH 06/22] added getMaxId function this function return the max id of an objects array --- index.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/index.js b/index.js index b25dbc5..3781c58 100644 --- a/index.js +++ b/index.js @@ -28,6 +28,17 @@ function removeSongFromPlaylists(id) { } } +/* +this funcion return the max id of an objects array +*/ +function getMaxId(arr) { + let max = 0; + for (let obj of arr) { + if(obj.id > max) max = obj.id; + } + return max; +} + const player = { songs: [ { @@ -86,6 +97,7 @@ const player = { // // let arr = [1,2,3,4]; // // arr.splice(2,1); // // console.log(arr); +console.log(getMaxId(player.songs)) function playSong(id) { let isExist = false; From 1b70497c2c1ae916129f17aa5500934bd3cc55d2 Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Mon, 6 Sep 2021 06:57:10 +0300 Subject: [PATCH 07/22] added isIdExist function this function checks if an id exists in the objects array --- index.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 3781c58..94bb484 100644 --- a/index.js +++ b/index.js @@ -29,7 +29,17 @@ function removeSongFromPlaylists(id) { } /* -this funcion return the max id of an objects array +this function checks if an id exists in the objects array +*/ +function isIdExist(arr, id) { + for (let obj of arr) { + if(obj.id === id) return true; + } + return false; +} + +/* +this function return the max id of an objects array */ function getMaxId(arr) { let max = 0; @@ -97,7 +107,7 @@ const player = { // // let arr = [1,2,3,4]; // // arr.splice(2,1); // // console.log(arr); -console.log(getMaxId(player.songs)) +console.log(isIdExist(player.songs, 6)) function playSong(id) { let isExist = false; From bdf33ee5baab5c550aad93f650f4eb0cd14b8057 Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Mon, 6 Sep 2021 07:14:49 +0300 Subject: [PATCH 08/22] added toSeconds function and passed addSong tests --- index.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 94bb484..930abd0 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,12 @@ function toMinutes(sec) { return minFormat.join(':'); } +function toSeconds(duration) { + //04:19 + let arr = duration.split(':'); + return parseInt(arr[0]) * 60 + parseInt(arr[1]); +} + function removeSongFromPlayer(id) { for(let i in player.songs) { if(player.songs[i].id === id) { @@ -107,7 +113,7 @@ const player = { // // let arr = [1,2,3,4]; // // arr.splice(2,1); // // console.log(arr); -console.log(isIdExist(player.songs, 6)) +// console.log(toSeconds("04:19")) function playSong(id) { let isExist = false; @@ -127,7 +133,14 @@ function removeSong(id) { } function addSong(title, album, artist, duration, id) { - // your code here + if(id === undefined) { + id = getMaxId(player.songs) + 1; + } else { + if(isIdExist(player.songs, id)) throw 'this id already exist!'; + } + duration = toSeconds(duration); + player.songs.push({id, title, album, artist, duration}); + return id; } function removePlaylist(id) { From bf591817285325657efbef30ce50847485b56360 Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Mon, 6 Sep 2021 07:40:59 +0300 Subject: [PATCH 09/22] added removedPlaylist and createPlaylist functions --- index.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 930abd0..c831c5b 100644 --- a/index.js +++ b/index.js @@ -116,14 +116,10 @@ const player = { // console.log(toSeconds("04:19")) function playSong(id) { - let isExist = false; + if(!isIdExist(player.songs, id)) throw 'song does not exist!'; for(let song of player.songs) { - if(song.id === id) { - console.log(player.playSong(song)); - isExist = true; - } + if(song.id === id) console.log(player.playSong(song)); } - if(!isExist) throw 'song does not exist!'; } function removeSong(id) { @@ -134,7 +130,7 @@ function removeSong(id) { function addSong(title, album, artist, duration, id) { if(id === undefined) { - id = getMaxId(player.songs) + 1; + id = getMaxId(player.songs) + 1; //generates auto id (max id + 1) } else { if(isIdExist(player.songs, id)) throw 'this id already exist!'; } @@ -144,11 +140,21 @@ function addSong(title, album, artist, duration, id) { } function removePlaylist(id) { - // your code here + if(!isIdExist(player.playlists, id)) throw 'id does not exist!'; + for (let i in player.playlists) { + if(player.playlists[i].id === id) player.playlists.splice(i, 1); + } } function createPlaylist(name, id) { - // your code here + if(id === undefined) { + id = getMaxId(player.playlists) + 1; //generates auto id (max id + 1) + } else { + if(isIdExist(player.playlists, id)) throw 'this id already exist!'; + } + // duration = toSeconds(duration); + player.playlists.push({id, name, songs: []}); + return id; } function playPlaylist(id) { From 9270e23287c6788861369421309b74dbda521eda Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Mon, 6 Sep 2021 07:51:21 +0300 Subject: [PATCH 10/22] finished playPlaylist function --- index.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index c831c5b..7ce76d4 100644 --- a/index.js +++ b/index.js @@ -55,6 +55,12 @@ function getMaxId(arr) { return max; } +function getPlaylist(id) { + for(let playlist of player.playlists) { + if(playlist.id === id) return playlist; + } +} + const player = { songs: [ { @@ -152,13 +158,16 @@ function createPlaylist(name, id) { } else { if(isIdExist(player.playlists, id)) throw 'this id already exist!'; } - // duration = toSeconds(duration); player.playlists.push({id, name, songs: []}); return id; } function playPlaylist(id) { - // your code here + if(!isIdExist(player.playlists, id)) throw 'this id already exist!'; + const playlist = getPlaylist(id); + for (let i = 0; i < playlist.songs.length; i++) { + playSong(playlist.songs[i]); + } } function editPlaylist(playlistId, songId) { From 226bab015c4991584ce77627a62d11f601d40945 Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Mon, 6 Sep 2021 08:05:16 +0300 Subject: [PATCH 11/22] refactory --- index.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 7ce76d4..2dfc5d6 100644 --- a/index.js +++ b/index.js @@ -61,6 +61,13 @@ function getPlaylist(id) { } } +function existError() { + throw 'this id already exist!'; +} +function notExistError() { + throw 'this id does not exist!'; +} + const player = { songs: [ { @@ -122,7 +129,7 @@ const player = { // console.log(toSeconds("04:19")) function playSong(id) { - if(!isIdExist(player.songs, id)) throw 'song does not exist!'; + if(!isIdExist(player.songs, id)) notExistError(); for(let song of player.songs) { if(song.id === id) console.log(player.playSong(song)); } @@ -130,7 +137,7 @@ function playSong(id) { function removeSong(id) { let isExist = removeSongFromPlayer(id); - if(!isExist) throw 'song does not exist!'; + if(!isExist) notExistError(); removeSongFromPlaylists(id); } @@ -138,7 +145,7 @@ function addSong(title, album, artist, duration, id) { if(id === undefined) { id = getMaxId(player.songs) + 1; //generates auto id (max id + 1) } else { - if(isIdExist(player.songs, id)) throw 'this id already exist!'; + if(isIdExist(player.songs, id)) existError(); } duration = toSeconds(duration); player.songs.push({id, title, album, artist, duration}); @@ -146,7 +153,7 @@ function addSong(title, album, artist, duration, id) { } function removePlaylist(id) { - if(!isIdExist(player.playlists, id)) throw 'id does not exist!'; + if(!isIdExist(player.playlists, id)) notExistError(); for (let i in player.playlists) { if(player.playlists[i].id === id) player.playlists.splice(i, 1); } @@ -156,14 +163,14 @@ function createPlaylist(name, id) { if(id === undefined) { id = getMaxId(player.playlists) + 1; //generates auto id (max id + 1) } else { - if(isIdExist(player.playlists, id)) throw 'this id already exist!'; + if(isIdExist(player.playlists, id)) existError(); } player.playlists.push({id, name, songs: []}); return id; } function playPlaylist(id) { - if(!isIdExist(player.playlists, id)) throw 'this id already exist!'; + if(!isIdExist(player.playlists, id)) notExistError(); const playlist = getPlaylist(id); for (let i = 0; i < playlist.songs.length; i++) { playSong(playlist.songs[i]); @@ -171,7 +178,7 @@ function playPlaylist(id) { } function editPlaylist(playlistId, songId) { - // your code here + // if(!isIdExist(player.playlists, playlistId)) } function playlistDuration(id) { From 894ece2d185298a7aefad72d01594acf3d129881 Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Mon, 6 Sep 2021 08:20:12 +0300 Subject: [PATCH 12/22] finished the editPlaylist function --- index.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 2dfc5d6..4728439 100644 --- a/index.js +++ b/index.js @@ -178,7 +178,15 @@ function playPlaylist(id) { } function editPlaylist(playlistId, songId) { - // if(!isIdExist(player.playlists, playlistId)) + if(!isIdExist(player.playlists, playlistId)) notExistError(); + if(!isIdExist(player.songs, songId)) notExistError(); + const playlist = getPlaylist(playlistId); + if(playlist.songs.indexOf(songId) >= 0) { + playlist.songs.splice(playlist.songs.indexOf(songId), 1); + if(playlist.songs.length === 0) removePlaylist(playlistId); + } else { + playlist.songs.push(songId); + } } function playlistDuration(id) { From 733d5d2dcdae4a4b1c1283e2dafe371f98f9628e Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Mon, 6 Sep 2021 09:30:58 +0300 Subject: [PATCH 13/22] finished playlistDuration function --- index.js | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 4728439..afe38d3 100644 --- a/index.js +++ b/index.js @@ -61,6 +61,12 @@ function getPlaylist(id) { } } +function getSong(id) { + for(let song of player.songs) { + if(song.id === id) return song; + } +} + function existError() { throw 'this id already exist!'; } @@ -68,6 +74,15 @@ function notExistError() { throw 'this id does not exist!'; } +/* +recursion function that sums the array elements. +*/ +function sumArr(arr) { + if(arr.length === 0) return 0; + return arr.pop() + sumArr(arr.slice(0, arr.length)); +} + + const player = { songs: [ { @@ -127,6 +142,10 @@ const player = { // // arr.splice(2,1); // // console.log(arr); // console.log(toSeconds("04:19")) +// console.log(sumArr([242, 213, 270])); +// console.log(playlistDuration(1)); +// console.log(player.songs[4].album.includes("ll")); +console.log(searchByQuery("ll")); function playSong(id) { if(!isIdExist(player.songs, id)) notExistError(); @@ -182,7 +201,7 @@ function editPlaylist(playlistId, songId) { if(!isIdExist(player.songs, songId)) notExistError(); const playlist = getPlaylist(playlistId); if(playlist.songs.indexOf(songId) >= 0) { - playlist.songs.splice(playlist.songs.indexOf(songId), 1); + console.log(playlist.songs.splice(playlist.songs.indexOf(songId), 1)); if(playlist.songs.length === 0) removePlaylist(playlistId); } else { playlist.songs.push(songId); @@ -190,11 +209,33 @@ function editPlaylist(playlistId, songId) { } function playlistDuration(id) { - // your code here + if(!isIdExist(player.playlists, id)) notExistError(); + const playlist = getPlaylist(id); + const secondsArr = []; + for(let songId of playlist.songs) { + secondsArr.push(getSong(songId).duration); + } + return sumArr(secondsArr); } function searchByQuery(query) { - // your code here + const obj = {songs: [], playlist: []}; + for(let song of player.songs) { + if(song.title.includes(query)) { + obj.songs.push(song); + continue; //continue to the next song + } + if(song.album.includes(query)) { + obj.songs.push(song); + continue; //continue to the next song + } + if(song.artist.includes(query)) { + obj.songs.push(song); + continue; //continue to the next song + } + } + // obj.songs.sort(); + return obj; } function searchByDuration(duration) { From 15e7d92a7d3402671c83c6ceecde038165eea0ee Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Mon, 6 Sep 2021 19:27:18 +0300 Subject: [PATCH 14/22] finished the searchByQuery function and passed its tests --- index.js | 63 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index afe38d3..61fbe63 100644 --- a/index.js +++ b/index.js @@ -11,7 +11,6 @@ function toMinutes(sec) { } function toSeconds(duration) { - //04:19 let arr = duration.split(':'); return parseInt(arr[0]) * 60 + parseInt(arr[1]); } @@ -82,6 +81,43 @@ function sumArr(arr) { return arr.pop() + sumArr(arr.slice(0, arr.length)); } +/* +this function gets a query, an array of objects, +and array of keys and returns an array of +objects if the query includes in the keys +*/ +function queryArr(query, objArr, keyArr) { + const queryArr = []; + for(let obj of objArr) { + for(let i = 0; i < keyArr.length; i++) { + if(obj[keyArr[i]].includes(query)) { + queryArr.push(obj); + i = keyArr.length; + } + } + } + return queryArr; +} + +/* +this function gets an array of objects and sort +it alphanumerically by the property of the objects +*/ +function sortObjectsArray(arr, property) { + const sortedKeys = []; + for(let key of arr) { + sortedKeys.push(key[property]); + } + sortedKeys.sort(); + const sortedObjects = []; + for (let i = 0; i < sortedKeys.length; i++) { + for (let j = 0; j < arr.length; j++) { + if(sortedKeys[i] === arr[j][property]) sortedObjects.push(arr[j]); + } + } + return sortedObjects; +} + const player = { songs: [ @@ -145,7 +181,7 @@ const player = { // console.log(sumArr([242, 213, 270])); // console.log(playlistDuration(1)); // console.log(player.songs[4].album.includes("ll")); -console.log(searchByQuery("ll")); +// console.log(searchByQuery("ll")); function playSong(id) { if(!isIdExist(player.songs, id)) notExistError(); @@ -212,29 +248,18 @@ function playlistDuration(id) { if(!isIdExist(player.playlists, id)) notExistError(); const playlist = getPlaylist(id); const secondsArr = []; - for(let songId of playlist.songs) { + for (let songId of playlist.songs) { secondsArr.push(getSong(songId).duration); } return sumArr(secondsArr); } function searchByQuery(query) { - const obj = {songs: [], playlist: []}; - for(let song of player.songs) { - if(song.title.includes(query)) { - obj.songs.push(song); - continue; //continue to the next song - } - if(song.album.includes(query)) { - obj.songs.push(song); - continue; //continue to the next song - } - if(song.artist.includes(query)) { - obj.songs.push(song); - continue; //continue to the next song - } - } - // obj.songs.sort(); + const obj = {songs: [], playlists: []}; + obj.songs = queryArr(query, player.songs, ['title', 'album', 'artist']); + obj.playlists = queryArr(query, player.playlists, ['name']); + obj.songs = sortObjectsArray(obj.songs, 'title'); + obj.playlists = sortObjectsArray(obj.playlists, 'name'); return obj; } From 26e4afb9cf861a103d5505432146e5ea6935cda2 Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Mon, 6 Sep 2021 19:52:59 +0300 Subject: [PATCH 15/22] finished searchByDuration function and passed all tests! --- index.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 61fbe63..8693869 100644 --- a/index.js +++ b/index.js @@ -264,7 +264,24 @@ function searchByQuery(query) { } function searchByDuration(duration) { - // your code here + duration = toSeconds(duration); + let difference; + let obj = null; + for(let playlist of player.playlists) { + let diff = playlistDuration(playlist.id) - duration; + if(Math.abs(diff) < difference || !difference) { + difference = Math.abs(diff); + obj = playlist; + } + } + for(let song of player.songs) { + let diff = song.duration - duration; + if(Math.abs(diff) < difference) { + difference = Math.abs(diff); + obj = song; + } + } + return obj; } module.exports = { From b78c134b3bab47920eb9c741c97765813081b21a Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Mon, 6 Sep 2021 23:27:34 +0300 Subject: [PATCH 16/22] refactory :) --- index.js | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 8693869..edf9b88 100644 --- a/index.js +++ b/index.js @@ -118,6 +118,24 @@ function sortObjectsArray(arr, property) { return sortedObjects; } +/* +this function gets an array of objects and duration +(in seconds) and returns the object with the minimum +difference between the durations. +*/ +function minDurDifference(arr, duration) { + let difference = null; + let newObj = null; + for(let obj of arr) { + let diff = obj.hasOwnProperty('name') ? playlistDuration(obj.id) - duration: obj.duration - duration; + if(Math.abs(diff) < difference || !difference) { + difference = Math.abs(diff); + newObj = obj; + } + } + return newObj; +} + const player = { songs: [ @@ -265,23 +283,11 @@ function searchByQuery(query) { function searchByDuration(duration) { duration = toSeconds(duration); - let difference; - let obj = null; - for(let playlist of player.playlists) { - let diff = playlistDuration(playlist.id) - duration; - if(Math.abs(diff) < difference || !difference) { - difference = Math.abs(diff); - obj = playlist; - } - } - for(let song of player.songs) { - let diff = song.duration - duration; - if(Math.abs(diff) < difference) { - difference = Math.abs(diff); - obj = song; - } - } - return obj; + const songObj = minDurDifference(player.songs, duration); + const playlistObj = minDurDifference(player.playlists, duration); + const songDur = Math.abs(songObj.duration - duration); + const playlistDur = Math.abs(playlistDuration(playlistObj.id) - duration); + return songDur < playlistDur ? songObj: playlistObj; } module.exports = { From 15316a7ae6e765e3fc15fdaa8f5528cec276609c Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Mon, 6 Sep 2021 23:29:19 +0300 Subject: [PATCH 17/22] removed checking section --- index.js | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/index.js b/index.js index edf9b88..0bf0207 100644 --- a/index.js +++ b/index.js @@ -191,16 +191,6 @@ const player = { }, } -/* CHECKING deleteeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee lineeeeeeeeeeeeeeee*/ -// // let arr = [1,2,3,4]; -// // arr.splice(2,1); -// // console.log(arr); -// console.log(toSeconds("04:19")) -// console.log(sumArr([242, 213, 270])); -// console.log(playlistDuration(1)); -// console.log(player.songs[4].album.includes("ll")); -// console.log(searchByQuery("ll")); - function playSong(id) { if(!isIdExist(player.songs, id)) notExistError(); for(let song of player.songs) { From fa1e2d6fbafd6aa51453719c2d8a14a34fa22af3 Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Mon, 6 Sep 2021 23:37:37 +0300 Subject: [PATCH 18/22] refactory :+1: --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 0bf0207..6edda03 100644 --- a/index.js +++ b/index.js @@ -4,14 +4,14 @@ and return a string value of the minutes in 'mm:ss' format. */ function toMinutes(sec) { - let minFormat = ["mm", "ss"]; + const minFormat = ["mm", "ss"]; minFormat[1] = sec % 60 < 10 ? "0" + sec % 60: sec % 60; minFormat[0] = sec / 60 < 10 ? "0" + Math.floor(sec / 60): Math.floor(sec / 60); return minFormat.join(':'); } function toSeconds(duration) { - let arr = duration.split(':'); + const arr = duration.split(':'); return parseInt(arr[0]) * 60 + parseInt(arr[1]); } @@ -127,7 +127,7 @@ function minDurDifference(arr, duration) { let difference = null; let newObj = null; for(let obj of arr) { - let diff = obj.hasOwnProperty('name') ? playlistDuration(obj.id) - duration: obj.duration - duration; + const diff = obj.hasOwnProperty('name') ? playlistDuration(obj.id) - duration: obj.duration - duration; if(Math.abs(diff) < difference || !difference) { difference = Math.abs(diff); newObj = obj; @@ -199,7 +199,7 @@ function playSong(id) { } function removeSong(id) { - let isExist = removeSongFromPlayer(id); + const isExist = removeSongFromPlayer(id); if(!isExist) notExistError(); removeSongFromPlaylists(id); } From bddb4ac1e803886456794b91b0a9fcff9f2649fa Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Tue, 7 Sep 2021 01:48:05 +0300 Subject: [PATCH 19/22] done refactory --- index.js | 68 +++++++++++++++++++------------------------------------- 1 file changed, 23 insertions(+), 45 deletions(-) diff --git a/index.js b/index.js index 6edda03..c9d19e3 100644 --- a/index.js +++ b/index.js @@ -16,20 +16,14 @@ function toSeconds(duration) { } function removeSongFromPlayer(id) { - for(let i in player.songs) { - if(player.songs[i].id === id) { - player.songs.splice(i,1); - return true; - } - } - return false; + const songIndex = player.songs.indexOf(getSong(id)); + player.songs.splice(songIndex, 1); } function removeSongFromPlaylists(id) { for(let playlist of player.playlists) { - for (let i in playlist.songs) { - if(playlist.songs[i] === id) playlist.songs.splice(i, 1); - } + const songIndex = playlist.songs.indexOf(id); + playlist.songs.splice(songIndex, 1); } } @@ -37,10 +31,7 @@ function removeSongFromPlaylists(id) { this function checks if an id exists in the objects array */ function isIdExist(arr, id) { - for (let obj of arr) { - if(obj.id === id) return true; - } - return false; + return arr.find(x => x.id === id) !== undefined; } /* @@ -55,15 +46,13 @@ function getMaxId(arr) { } function getPlaylist(id) { - for(let playlist of player.playlists) { - if(playlist.id === id) return playlist; - } + const playlist = player.playlists.find(x => x.id === id); + return playlist; } function getSong(id) { - for(let song of player.songs) { - if(song.id === id) return song; - } + const song = player.songs.find(x => x.id === id); + return song; } function existError() { @@ -74,11 +63,11 @@ function notExistError() { } /* -recursion function that sums the array elements. +recursion function that sums the duration of a playlist. */ -function sumArr(arr) { +function sumDuration(arr) { if(arr.length === 0) return 0; - return arr.pop() + sumArr(arr.slice(0, arr.length)); + return getSong(arr.pop()).duration + sumDuration(arr.slice(0, arr.length)); } /* @@ -104,17 +93,11 @@ this function gets an array of objects and sort it alphanumerically by the property of the objects */ function sortObjectsArray(arr, property) { - const sortedKeys = []; - for(let key of arr) { - sortedKeys.push(key[property]); - } - sortedKeys.sort(); const sortedObjects = []; - for (let i = 0; i < sortedKeys.length; i++) { - for (let j = 0; j < arr.length; j++) { - if(sortedKeys[i] === arr[j][property]) sortedObjects.push(arr[j]); - } + for (let obj of arr) { + sortedObjects.push(obj); } + sortedObjects.sort((a, b) => {if(a[property] < b[property]) return -1;}); return sortedObjects; } @@ -193,14 +176,13 @@ const player = { function playSong(id) { if(!isIdExist(player.songs, id)) notExistError(); - for(let song of player.songs) { - if(song.id === id) console.log(player.playSong(song)); - } + const song = player.songs.find(x => x.id === id); + console.log(player.playSong(song)); } function removeSong(id) { - const isExist = removeSongFromPlayer(id); - if(!isExist) notExistError(); + if(!isIdExist(player.songs, id)) notExistError(); + removeSongFromPlayer(id); removeSongFromPlaylists(id); } @@ -241,11 +223,10 @@ function playPlaylist(id) { } function editPlaylist(playlistId, songId) { - if(!isIdExist(player.playlists, playlistId)) notExistError(); - if(!isIdExist(player.songs, songId)) notExistError(); + if(!isIdExist(player.playlists, playlistId) || !isIdExist(player.songs, songId)) notExistError(); const playlist = getPlaylist(playlistId); if(playlist.songs.indexOf(songId) >= 0) { - console.log(playlist.songs.splice(playlist.songs.indexOf(songId), 1)); + playlist.songs.splice(playlist.songs.indexOf(songId), 1); if(playlist.songs.length === 0) removePlaylist(playlistId); } else { playlist.songs.push(songId); @@ -255,11 +236,8 @@ function editPlaylist(playlistId, songId) { function playlistDuration(id) { if(!isIdExist(player.playlists, id)) notExistError(); const playlist = getPlaylist(id); - const secondsArr = []; - for (let songId of playlist.songs) { - secondsArr.push(getSong(songId).duration); - } - return sumArr(secondsArr); + const secondsArr = [...playlist.songs]; + return sumDuration(secondsArr); } function searchByQuery(query) { From 9d884c3db6d9a1dbb637191b5609f40aa99d4d6e Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Sun, 12 Sep 2021 11:25:45 +0300 Subject: [PATCH 20/22] end --- index.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index c9d19e3..a503a07 100644 --- a/index.js +++ b/index.js @@ -241,12 +241,12 @@ function playlistDuration(id) { } function searchByQuery(query) { - const obj = {songs: [], playlists: []}; - obj.songs = queryArr(query, player.songs, ['title', 'album', 'artist']); - obj.playlists = queryArr(query, player.playlists, ['name']); - obj.songs = sortObjectsArray(obj.songs, 'title'); - obj.playlists = sortObjectsArray(obj.playlists, 'name'); - return obj; + const resultObj = {songs: [], playlists: []}; + resultObj.songs = queryArr(query, player.songs, ['title', 'album', 'artist']); + resultObj.playlists = queryArr(query, player.playlists, ['name']); + resultObj.songs = sortObjectsArray(resultObj.songs, 'title'); + resultObj.playlists = sortObjectsArray(resultObj.playlists, 'name'); + return resultObj; } function searchByDuration(duration) { From 2d4410cb2408f388a93c91561f70fd1f39094c0f Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Sun, 12 Sep 2021 11:33:22 +0300 Subject: [PATCH 21/22] final version --- index.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index a503a07..8978902 100644 --- a/index.js +++ b/index.js @@ -241,12 +241,13 @@ function playlistDuration(id) { } function searchByQuery(query) { - const resultObj = {songs: [], playlists: []}; - resultObj.songs = queryArr(query, player.songs, ['title', 'album', 'artist']); - resultObj.playlists = queryArr(query, player.playlists, ['name']); - resultObj.songs = sortObjectsArray(resultObj.songs, 'title'); - resultObj.playlists = sortObjectsArray(resultObj.playlists, 'name'); - return resultObj; + let songs = []; + let playlists = []; + songs = queryArr(query, player.songs, ['title', 'album', 'artist']); + playlists = queryArr(query, player.playlists, ['name']); + songs = sortObjectsArray(songs, 'title'); + playlists = sortObjectsArray(playlists, 'name'); + return {songs, playlists}; } function searchByDuration(duration) { From b59c34a1ec3c34b44e0b1897d22e111eb736c207 Mon Sep 17 00:00:00 2001 From: shaharkamay Date: Sun, 12 Sep 2021 13:30:20 +0300 Subject: [PATCH 22/22] really final commit --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 8978902..18616d9 100644 --- a/index.js +++ b/index.js @@ -79,7 +79,8 @@ function queryArr(query, objArr, keyArr) { const queryArr = []; for(let obj of objArr) { for(let i = 0; i < keyArr.length; i++) { - if(obj[keyArr[i]].includes(query)) { + console.log(obj[keyArr[i]]); + if(obj[keyArr[i]].toLowerCase().includes(query.toLowerCase())) { queryArr.push(obj); i = keyArr.length; } @@ -174,6 +175,7 @@ const player = { }, } + function playSong(id) { if(!isIdExist(player.songs, id)) notExistError(); const song = player.songs.find(x => x.id === id);