From ea13ed82a19f2608a8edeb639171e61eef6802e1 Mon Sep 17 00:00:00 2001 From: galgluska <89578212+galgluska@users.noreply.github.com> Date: Fri, 10 Sep 2021 12:39:05 +0300 Subject: [PATCH 01/10] playSong method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit כתיבת playSong method וכתיבת פעולה נוספת שתתמוך. --- index.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 10f4784..ce113a4 100644 --- a/index.js +++ b/index.js @@ -48,10 +48,31 @@ 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 + + " | " + + durationFormat(song.duration) + +".") }, } +// creating a function that returns a song duration in the required format +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 playSong(id) { // your code here } @@ -92,6 +113,9 @@ function searchByDuration(duration) { // your code here } + +// do not change below this line + module.exports = { player, playSong, From 9814b0520e9645899b5639c7d93694be27bc2c38 Mon Sep 17 00:00:00 2001 From: galgluska <89578212+galgluska@users.noreply.github.com> Date: Fri, 10 Sep 2021 14:13:05 +0300 Subject: [PATCH 02/10] removeSong added another function to support. --- index.js | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index ce113a4..de5f23c 100644 --- a/index.js +++ b/index.js @@ -74,16 +74,80 @@ function durationFormat(duration){ function playSong(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]); + } + +} + +// adding a function that checks if there is an id matching +function checkId(songs,id){ + for (let i = 0 ; i < songs.length ; i ++){ + if (id === songs[i].id) + return true; + } + return false; } + function removeSong(id) { - // your code here + if (!checkId(player.songs, id)){ + throw "ID doesn't exist." + } + + + for (let i = 0 ; i < player.songs.length ; i ++){ + if(player.songs[i].id === id ){ + player.songs.splice(i,1); + } + } + + for (let j = 0 ; j < player.playlists.length ; j ++){ +for (let x = 0; x < player.playlists[j].songs.length; x++) { + if(player.playlists[j].songs[x] === id){ + player.playlists[j].songs.splice(x,1); + } + + } -function addSong(title, album, artist, duration, id) { - // your code here + } + +} + +/* +// adding a function that returns a new id +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; +} + +// adding a function that takes the duration in a mm:ss format and turns it to seconds +function oppDuration(duration){ + duration = duration.split(':') + let minutes = parseInt(duration[0]) * 60 + let seconds = parseInt(duration[1]) + return minutes + seconds +} + +function addSong(title, album, artist, duration, id = newId(player.songs)) { + if (checkId(player.songs, id)){ + throw "ID already Exist." + } + + duration= oppDuration(duration); + + player.songs.push({title,album,artist,duration,id}); + return id; } +*/ function removePlaylist(id) { // your code here From c6e950382ca92161d18ad96c78b9a941237d9cd8 Mon Sep 17 00:00:00 2001 From: galgluska <89578212+galgluska@users.noreply.github.com> Date: Fri, 10 Sep 2021 14:13:50 +0300 Subject: [PATCH 03/10] addSong --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index de5f23c..f346dbe 100644 --- a/index.js +++ b/index.js @@ -118,7 +118,7 @@ for (let x = 0; x < player.playlists[j].songs.length; x++) { } -/* + // adding a function that returns a new id function newId(arr){ let max = arr[0]; @@ -147,7 +147,7 @@ function addSong(title, album, artist, duration, id = newId(player.songs)) { player.songs.push({title,album,artist,duration,id}); return id; } -*/ + function removePlaylist(id) { // your code here From a8a888004f75ae598beec6c36fecf886fab3f526 Mon Sep 17 00:00:00 2001 From: galgluska <89578212+galgluska@users.noreply.github.com> Date: Fri, 10 Sep 2021 14:42:46 +0300 Subject: [PATCH 04/10] remove --- index.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index f346dbe..e3bb5f3 100644 --- a/index.js +++ b/index.js @@ -150,15 +150,31 @@ function addSong(title, album, artist, duration, id = newId(player.songs)) { function removePlaylist(id) { - // your code here + if (!checkId(player.playlists, id)){ + throw "ID doesn't exist." + } + for (let i = 0; i < player.playlists.length; i++) { + if (player.playlists[i].id === id) + player.playlists.splice(i,1); + + } } -function createPlaylist(name, id) { - // your code here +/* +function createPlaylist(name, id = newId(player.playlists)) { + if (checkId(player.playlists, id)){ + throw "ID already exist."} + + player.playlists.push({name, id, songs: []}); + return id; + + } +*/ + function playPlaylist(id) { - // your code here + } function editPlaylist(playlistId, songId) { From e775f491f80ac93f80abad6aa322deeb38c9d84e Mon Sep 17 00:00:00 2001 From: galgluska <89578212+galgluska@users.noreply.github.com> Date: Fri, 10 Sep 2021 14:43:04 +0300 Subject: [PATCH 05/10] create playlist --- index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/index.js b/index.js index e3bb5f3..6989cfd 100644 --- a/index.js +++ b/index.js @@ -160,7 +160,7 @@ function removePlaylist(id) { } } -/* + function createPlaylist(name, id = newId(player.playlists)) { if (checkId(player.playlists, id)){ throw "ID already exist."} @@ -171,7 +171,6 @@ function createPlaylist(name, id = newId(player.playlists)) { } -*/ function playPlaylist(id) { From 0c6eced7d56ea211ffab42de372f8492304aecff Mon Sep 17 00:00:00 2001 From: galgluska <89578212+galgluska@users.noreply.github.com> Date: Fri, 10 Sep 2021 14:54:41 +0300 Subject: [PATCH 06/10] play playlist --- index.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/index.js b/index.js index 6989cfd..a9e95fa 100644 --- a/index.js +++ b/index.js @@ -173,7 +173,17 @@ function createPlaylist(name, id = newId(player.playlists)) { function playPlaylist(id) { + if (!checkId(player.playlists, id)){ + throw "ID doesn't 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 editPlaylist(playlistId, songId) { From afffac40f720efbbcc27c7ebbdc004a7d348b761 Mon Sep 17 00:00:00 2001 From: galgluska <89578212+galgluska@users.noreply.github.com> Date: Fri, 10 Sep 2021 15:27:43 +0300 Subject: [PATCH 07/10] editPlaylist --- index.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index a9e95fa..838d0e5 100644 --- a/index.js +++ b/index.js @@ -187,8 +187,29 @@ function playPlaylist(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) { // your code here From 412dde4f126447cdb94799a7ceb486fe656830b4 Mon Sep 17 00:00:00 2001 From: galgluska <89578212+galgluska@users.noreply.github.com> Date: Fri, 10 Sep 2021 15:51:40 +0300 Subject: [PATCH 08/10] playlist duration --- index.js | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 94 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 838d0e5..0d7e246 100644 --- a/index.js +++ b/index.js @@ -212,17 +212,108 @@ function editPlaylist(playlistId, songId) { function playlistDuration(id) { - // your code here + + let correctPlaylist = findPlaylistById(id) //correctPlaylist contain the wanted playlist + let save = 0, + sum = 0 + for (let i = 0; i < correctPlaylist.songs.length; i++) { + //run on the songs array inside this playlist + save = correctPlaylist.songs[i] + for (let j = 0; j < player.songs.length; j++) { + //run on the songs array + if (player.songs[j].id === save) sum += player.songs[j].duration + } + } + return sum } +// added a function that returns the right playlist. +function findPlaylistById(id) { + //Get a playlist id and return the wanted playlist by his id + let correctPlaylist + for (let i = 0; i < player.playlists.length; i++) { + //run on playlists array + if (id === player.playlists[i].id) correctPlaylist = player.playlists[i] + } + return correctPlaylist +} + +/* 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) //convert from mm:ss format to seconds + let arrSongs = arrLengthSongs(duration) //arrSongs contain array that look like this: [ closest-duartion-for-song , the-object-himself(song) ] + let arrPlaylist = arrLengthPlaylist(duration) //arrPlaylist contain array that look like this: [ closest-duartion-for-playlist , the-object-himself(playlist) ] + return arrSongs[0] < arrPlaylist[0] ? arrSongs[1] : arrPlaylist[1] +} + +function arrLengthSongs(duration) { + //gets song duartion return array of [closet-duration-song-in-seconds , closet-duration-song-the-object-himself(song)] + let arr = [] + let minDuration = duration, + index = 0 + for (let i = 0; i < player.songs.length; i++) { + //run on the songs array + 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) { + //gets playlist duartion return array of [closet-duration-playlist-in-seconds , closet-duration-playlist-the-object-himself(playlist)] + let arr = [] + let minDuration = duration, + index = 0 + for (let i = 0; i < player.playlists.length; i++) { + //run on playlists array + 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 } +*/ // do not change below this line From eec3fa99218c58f9c678916c0a3b37527ba78007 Mon Sep 17 00:00:00 2001 From: galgluska <89578212+galgluska@users.noreply.github.com> Date: Fri, 10 Sep 2021 15:52:24 +0300 Subject: [PATCH 09/10] search by query --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 0d7e246..370f13c 100644 --- a/index.js +++ b/index.js @@ -238,7 +238,7 @@ function findPlaylistById(id) { return correctPlaylist } -/* + function searchByQuery(query) { let tempQuery = query.toUpperCase() const results = { songs: [], playlists: [] } @@ -266,6 +266,7 @@ function searchByQuery(query) { } return results } +/* function searchByDuration(duration) { duration = oppDuration(duration) //convert from mm:ss format to seconds From 3c915b7762a51c427f9e9f1e3595f580ff0efd53 Mon Sep 17 00:00:00 2001 From: galgluska <89578212+galgluska@users.noreply.github.com> Date: Fri, 10 Sep 2021 15:53:04 +0300 Subject: [PATCH 10/10] search by duration --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 370f13c..b93deca 100644 --- a/index.js +++ b/index.js @@ -266,7 +266,7 @@ function searchByQuery(query) { } return results } -/* + function searchByDuration(duration) { duration = oppDuration(duration) //convert from mm:ss format to seconds @@ -314,7 +314,7 @@ function arrLengthPlaylist(duration) { return arr } -*/ + // do not change below this line