From c7f9bec0e4146232652946fa4bd1e035b436125d Mon Sep 17 00:00:00 2001 From: danielkravtsov5 Date: Sat, 11 Sep 2021 21:10:24 +0300 Subject: [PATCH 01/17] playSong- a method that plays a song. --- index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/index.js b/index.js index 10f4784..dd55e27 100644 --- a/index.js +++ b/index.js @@ -49,6 +49,13 @@ const player = { ], playSong(song) { console.log(/* your code here */) + console.log( + "Playing " + song.title + +" from " + song.album + + " by " + song.artist + + " | " + + durationFormat(song.duration) + +".") }, } From 15d91b02bcb35e26a7d8a516c91ec7897623781b Mon Sep 17 00:00:00 2001 From: danielkravtsov5 Date: Sat, 11 Sep 2021 21:12:01 +0300 Subject: [PATCH 02/17] remove song Gets a song ID. Removes the song with the given ID from the player (from songs and playlists). --- index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/index.js b/index.js index dd55e27..f59b032 100644 --- a/index.js +++ b/index.js @@ -65,6 +65,11 @@ function playSong(id) { function removeSong(id) { // your code here + if (!(checkId(player.songs,id))) throw "ERROR: id doesn't exict."; + else for(let i = 0 ; i < player.songs.length ; i ++){ + if (player.songs[i].id === id) + return player.playSong(player.songs[i]); + } } function addSong(title, album, artist, duration, id) { From e51d79a1b3a38990b6e4b9f24f16f4b8608073a8 Mon Sep 17 00:00:00 2001 From: danielkravtsov5 Date: Sat, 11 Sep 2021 21:13:02 +0300 Subject: [PATCH 03/17] checkId i built a function that check if a given id is existing --- index.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/index.js b/index.js index f59b032..f344162 100644 --- a/index.js +++ b/index.js @@ -78,6 +78,12 @@ function addSong(title, album, artist, duration, id) { function removePlaylist(id) { // your code here +function checkId(songs,id){ + for (let i = 0 ; i < songs.length ; i ++){ + if (id === songs[i].id) + return true; + } + return false; } function createPlaylist(name, id) { From 003d4b15f8aa82c10d0ce9a9e744aac86a2a2e8e Mon Sep 17 00:00:00 2001 From: danielkravtsov5 Date: Sat, 11 Sep 2021 21:13:50 +0300 Subject: [PATCH 04/17] playlistDuration Gets a playlist ID. Returns the total duration of the entire playlist with the given ID. --- index.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/index.js b/index.js index f344162..0798994 100644 --- a/index.js +++ b/index.js @@ -88,6 +88,18 @@ function checkId(songs,id){ function createPlaylist(name, id) { // your code here +function playlistDuration(id) { + + let correctPlaylist = findPlaylistById(id) //correctPlaylist contain the wanted playlist + let save = 0, + sum = 0 + for (let i = 0; i < correctPlaylist.songs.length; i++) { + save = correctPlaylist.songs[i] + for (let j = 0; j < player.songs.length; j++) { + if (player.songs[j].id === save) sum += player.songs[j].duration + } + } + return sum } function playPlaylist(id) { From 6791528775e3a0785d9ab7022180db670f5e20c8 Mon Sep 17 00:00:00 2001 From: danielkravtsov5 Date: Sat, 11 Sep 2021 21:15:23 +0300 Subject: [PATCH 05/17] durationFormat i built a function that takes a duration and makes it into mm:ss format --- index.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 0798994..55836ba 100644 --- a/index.js +++ b/index.js @@ -72,8 +72,15 @@ function removeSong(id) { } } -function addSong(title, album, artist, duration, id) { - // your code here +function durationFormat(duration){ + let minutes = Math.floor(duration / 60); + let seconds = duration % 60; + if(minutes < 10 && seconds < 10) + return "0"+minutes+":"+"0"+seconds; + + else if (minutes < 10) return "0"+minutes+":"+seconds; + else if (seconds < 10) return minutes+":0"+seconds; + else return minutes+":"+seconds; } function removePlaylist(id) { From becb20d580d2e5115fe673748ed65da34c5d07d1 Mon Sep 17 00:00:00 2001 From: danielkravtsov5 Date: Sat, 11 Sep 2021 21:15:50 +0300 Subject: [PATCH 06/17] findPlaylistById function that finds a playlist by the given id --- index.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/index.js b/index.js index 55836ba..da5ce4d 100644 --- a/index.js +++ b/index.js @@ -111,6 +111,12 @@ function playlistDuration(id) { function playPlaylist(id) { // your code here +function findPlaylistById(id) { + let correctPlaylist + for (let i = 0; i < player.playlists.length; i++) { + if (id === player.playlists[i].id) correctPlaylist = player.playlists[i] + } + return correctPlaylist } function editPlaylist(playlistId, songId) { From 7acc3f19fe4d61d3bd2acc9c6b419713c1f3ecf4 Mon Sep 17 00:00:00 2001 From: danielkravtsov5 Date: Sat, 11 Sep 2021 21:16:29 +0300 Subject: [PATCH 07/17] editPlaylist Gets a playlist ID & a song ID. If the song ID exists in the playlist, removes it. If it was the only song in the playlist, also deletes the playlist. If the song ID does not exist in the playlist, adds it to the end of the playlist. --- index.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index da5ce4d..2935923 100644 --- a/index.js +++ b/index.js @@ -120,7 +120,25 @@ function findPlaylistById(id) { } function editPlaylist(playlistId, songId) { - // your code here + if (!checkId(player.playlists, playlistId)){ + throw "ID of the playlist doesn't exist." + } + if (!checkId(player.songs, songId)){ + throw "ID of the song doesn't exist." + } + for (let i = 0; i < player.playlists.length; i++) { + if (playlistId === player.playlists[i].id){ + for (let x = 0; x < player.playlists[i].songs.length; x++) { + if (songId === player.playlists[i].songs[x]){ + player.playlists[i].songs.splice(x,1); + } + else + player.playlists[i].songs.push(songId); + } + if (player.playlists[i].songs.length === 0) + removePlaylist(player.playlists[i].id); + } + } } function playlistDuration(id) { From f87bf480128272f23e22ecac876f9d795b8cec55 Mon Sep 17 00:00:00 2001 From: danielkravtsov5 Date: Sat, 11 Sep 2021 21:17:09 +0300 Subject: [PATCH 08/17] newId function that gives a new id --- index.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/index.js b/index.js index 2935923..f88aebe 100644 --- a/index.js +++ b/index.js @@ -143,6 +143,15 @@ function editPlaylist(playlistId, songId) { function playlistDuration(id) { // your code here +function newId(arr){ + let max = arr[0]; + for (let i = 0; i < arr.length; i++) { + if (max < arr[i].id) + max = arr[i].id; + } + return max+1; +} + } function searchByQuery(query) { From e5bf8bfd59fb9ebdab2ce490a81d283693db22dd Mon Sep 17 00:00:00 2001 From: danielkravtsov5 Date: Sat, 11 Sep 2021 21:17:46 +0300 Subject: [PATCH 09/17] oppDuration function that makes time format to seconds --- index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/index.js b/index.js index f88aebe..a7b0998 100644 --- a/index.js +++ b/index.js @@ -152,6 +152,13 @@ function newId(arr){ return max+1; } +function oppDuration(duration){ + duration = duration.split(':') + let minutes = parseInt(duration[0]) * 60 + let seconds = parseInt(duration[1]) + return minutes + seconds +} + } function searchByQuery(query) { From 1bdab07eaa0b7678c0b3c3c25c56e04ea53f2976 Mon Sep 17 00:00:00 2001 From: danielkravtsov5 Date: Sat, 11 Sep 2021 21:18:13 +0300 Subject: [PATCH 10/17] addSong Gets a title, album, artist, duration & ID. Adds a new song with given properties to the player. The ID is optional, and if omitted should be automatically generated. The song duration should be in `mm:ss` format (for example 06:27). Returns the ID of the new song. --- index.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/index.js b/index.js index a7b0998..3050edd 100644 --- a/index.js +++ b/index.js @@ -159,6 +159,16 @@ function oppDuration(duration){ return minutes + seconds } +function addSong(title, album, artist, duration, id = newId(player.songs)) { + if (checkId(player.songs, id)){ + throw "ID exists" + } + duration= oppDuration(duration); + let newSong = {id: id, title: title, album: album, artist: artist, duration: duration}; + player.songs.push(newSong) + return id; +} + } function searchByQuery(query) { From 27a5d07ae41e2a45beb56952cca5c6ccc06d747d Mon Sep 17 00:00:00 2001 From: danielkravtsov5 Date: Sat, 11 Sep 2021 21:18:40 +0300 Subject: [PATCH 11/17] removeSong Gets a song ID. Removes the song with the given ID from the player (from songs and playlists). --- index.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/index.js b/index.js index 3050edd..67ee99d 100644 --- a/index.js +++ b/index.js @@ -169,6 +169,24 @@ function addSong(title, album, artist, duration, id = newId(player.songs)) { return id; } +function removeSong(id) { + if (!checkId(player.songs, id)){ + throw "ID doesn't exist."; + } + for (let i = 0; i Date: Sat, 11 Sep 2021 21:18:54 +0300 Subject: [PATCH 12/17] removePlaylist Gets a playlist ID. Remove the playlist with the given ID from the player (does not delete the songs inside it). --- index.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/index.js b/index.js index 67ee99d..0126b15 100644 --- a/index.js +++ b/index.js @@ -187,6 +187,17 @@ function removeSong(id) { } } +function removePlaylist(id) { + if (!checkId(player.playlists, id)){ + throw "ID doesn't exist."; + } + for (let i = 0; i Date: Sat, 11 Sep 2021 21:19:14 +0300 Subject: [PATCH 13/17] createPlaylist Gets a name & ID. Creates a new, empty playlist with the given details. The ID is optional, and if omitted should be automatically generated. Returns the ID of the new playlist. --- index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/index.js b/index.js index 0126b15..e505140 100644 --- a/index.js +++ b/index.js @@ -198,6 +198,13 @@ function removePlaylist(id) { } } +function createPlaylist(name, id = newId(player.playlists)) { + if (checkId(player.playlists, id)){ + throw "ID is taken" + } + let newPlaylist = { id: id, name: name, songs: []} + player.playlists.push(newPlaylist) + return id; } function searchByQuery(query) { From 3881f259acbeb965fbc65ef31cb1ffa469cbff33 Mon Sep 17 00:00:00 2001 From: danielkravtsov5 Date: Sat, 11 Sep 2021 21:19:43 +0300 Subject: [PATCH 14/17] playPlaylist Gets a playlist ID. Plays all songs in the specified playlist, in the order the appear in the playlist. --- index.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/index.js b/index.js index e505140..9b8ec58 100644 --- a/index.js +++ b/index.js @@ -207,6 +207,21 @@ function createPlaylist(name, id = newId(player.playlists)) { return id; } +function playPlaylist(id) { + if (!checkId(player.playlists, id)){ + throw "ID dont exist" + } + for (let i = 0; i < player.playlists.length; i++) { + if(id === player.playlists[i].id){ + for (let x = 0; x < player.playlists[i].songs.length; x++) { + playSong(player.playlists[i].songs[x]); + } + } + } + return id; +} + + function searchByQuery(query) { // your code here } From 4c5bfd9525fb4d351f36bebcd68de7f30c9b25f1 Mon Sep 17 00:00:00 2001 From: danielkravtsov5 Date: Sat, 11 Sep 2021 21:20:19 +0300 Subject: [PATCH 15/17] searchByQuery Gets a query string. Returns a results object, which has: - `songs`: an array of songs in which either title or album or artist contain the query string. The songs should be sorted by their titles. - `playlists`: an array of playlists in which the name contains the query string. The playlists should be sorted by their names. --- index.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 9b8ec58..b83c3dd 100644 --- a/index.js +++ b/index.js @@ -223,11 +223,73 @@ function playPlaylist(id) { function searchByQuery(query) { - // your code here + let tempQuery = query.toUpperCase() + const results = { songs: [], playlists: [] } + for (let i = 0; i < player.playlists.length; i++) { + //for playlists + if (player.playlists[i].name.toUpperCase().includes(tempQuery)) { + results.playlists.push(player.playlists[i]) + results.playlists.sort((a, b) => { + if (a.name.toUpperCase() < b.name.toUpperCase()) return -1 + }) + } + } + for (let i = 0; i < player.songs.length; i++) { + //for songs + if ( + player.songs[i].album.toUpperCase().includes(tempQuery) || + player.songs[i].artist.toUpperCase().includes(tempQuery) || + player.songs[i].title.toUpperCase().includes(tempQuery) + ) { + results.songs.push(player.songs[i]) + results.songs.sort((a, b) => { + if (a.title.toUpperCase() < b.title.toUpperCase()) return -1 + }) + } + } + return results } function searchByDuration(duration) { - // your code here + duration = oppDuration(duration) + let arrSongs = arrLengthSongs(duration) + let arrPlaylist = arrLengthPlaylist(duration) + return arrSongs[0] < arrPlaylist[0] ? arrSongs[1] : arrPlaylist[1] +} + +function arrLengthSongs(duration) { + let arr = [] + let minDuration = duration, + index = 0 + for (let i = 0; i < player.songs.length; i++) { + if (minDuration > Math.abs(duration - player.songs[i].duration)) { + minDuration = Math.abs(duration - player.songs[i].duration) + index = i + } + } + arr.push(minDuration) + arr.push(player.songs[index]) + return arr +} + +function arrLengthPlaylist(duration) { + let arr = [] + let minDuration = duration, + index = 0 + for (let i = 0; i < player.playlists.length; i++) { + if ( + minDuration > + Math.abs(duration - playlistDuration(player.playlists[i].id)) + ) { + minDuration = Math.abs( + duration - playlistDuration(player.playlists[i].id) + ) + index = i + } + } + arr.push(minDuration) + arr.push(player.playlists[index]) + return arr } module.exports = { From b0aa6e0ab866eeb3c1468e75b2fc4513fe7b1d2b Mon Sep 17 00:00:00 2001 From: danielkravtsov5 Date: Sat, 11 Sep 2021 21:41:04 +0300 Subject: [PATCH 16/17] extras --- index.js | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/index.js b/index.js index b83c3dd..17bc985 100644 --- a/index.js +++ b/index.js @@ -48,7 +48,6 @@ const player = { { id: 5, name: 'Israeli', songs: [4, 5] }, ], playSong(song) { - console.log(/* your code here */) console.log( "Playing " + song.title +" from " + song.album @@ -60,11 +59,7 @@ const player = { } function playSong(id) { - // your code here -} -function removeSong(id) { - // your code here if (!(checkId(player.songs,id))) throw "ERROR: id doesn't exict."; else for(let i = 0 ; i < player.songs.length ; i ++){ if (player.songs[i].id === id) @@ -83,8 +78,6 @@ function durationFormat(duration){ else return minutes+":"+seconds; } -function removePlaylist(id) { - // your code here function checkId(songs,id){ for (let i = 0 ; i < songs.length ; i ++){ if (id === songs[i].id) @@ -93,8 +86,6 @@ function checkId(songs,id){ return false; } -function createPlaylist(name, id) { - // your code here function playlistDuration(id) { let correctPlaylist = findPlaylistById(id) //correctPlaylist contain the wanted playlist @@ -109,8 +100,6 @@ function playlistDuration(id) { return sum } -function playPlaylist(id) { - // your code here function findPlaylistById(id) { let correctPlaylist for (let i = 0; i < player.playlists.length; i++) { @@ -141,8 +130,6 @@ function editPlaylist(playlistId, songId) { } } -function playlistDuration(id) { - // your code here function newId(arr){ let max = arr[0]; for (let i = 0; i < arr.length; i++) { @@ -235,7 +222,6 @@ function searchByQuery(query) { } } for (let i = 0; i < player.songs.length; i++) { - //for songs if ( player.songs[i].album.toUpperCase().includes(tempQuery) || player.songs[i].artist.toUpperCase().includes(tempQuery) || From c943ec28f604d35eaeb44be52e69e32d34ccf8db Mon Sep 17 00:00:00 2001 From: danielkravtsov5 Date: Sun, 12 Sep 2021 11:45:43 +0300 Subject: [PATCH 17/17] Update index.js --- index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/index.js b/index.js index 17bc985..d7bf5d5 100644 --- a/index.js +++ b/index.js @@ -208,7 +208,6 @@ function playPlaylist(id) { return id; } - function searchByQuery(query) { let tempQuery = query.toUpperCase() const results = { songs: [], playlists: [] } @@ -290,4 +289,4 @@ module.exports = { playlistDuration, searchByQuery, searchByDuration, -} +}