From 6dbaad20d44af904facb1204aa90267ae5c238ee Mon Sep 17 00:00:00 2001 From: NativAmar <89576789+NativAmar@users.noreply.github.com> Date: Thu, 9 Sep 2021 16:53:40 +0300 Subject: [PATCH 1/9] playSong#1 --- index.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 10f4784..d20fd6a 100644 --- a/index.js +++ b/index.js @@ -48,12 +48,33 @@ 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} | ${durationConvert(song.duration)}.`) }, } +// convert to mm:ss +function durationConvert(duration) +{ + let min = Math.floor(duration / 60); + let sec = duration % 60; + + if (min < 10){ + min = "0" + String(min); + } + if (sec < 10) { + sec = "0" + String(sec); + } + return min+':'+sec +} + function playSong(id) { - // your code here + for (let song of player.songs){ + if (song.id === id) + { + return player.playSong(song); + } + } + throw new Error("No such ID"); } function removeSong(id) { From 81722a71fc103ae9a350d5311378e711e3a86378 Mon Sep 17 00:00:00 2001 From: NativAmar <89576789+NativAmar@users.noreply.github.com> Date: Fri, 10 Sep 2021 10:50:51 +0300 Subject: [PATCH 2/9] removeSong&addSong#2-3 --- index.js | 53 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index d20fd6a..c05f85d 100644 --- a/index.js +++ b/index.js @@ -51,8 +51,6 @@ const player = { console.log(`Playing ${song.title} from ${song.album} by ${song.artist} | ${durationConvert(song.duration)}.`) }, } - -// convert to mm:ss function durationConvert(duration) { let min = Math.floor(duration / 60); @@ -66,7 +64,6 @@ function durationConvert(duration) } return min+':'+sec } - function playSong(id) { for (let song of player.songs){ if (song.id === id) @@ -74,15 +71,55 @@ function playSong(id) { return player.playSong(song); } } - throw new Error("No such ID"); + throw new Error("This ID are not exist"); +} +function getSongByID(id){ + for (let index = 0; index < player.songs.length; index++) { + if(player.songs[index].id === id){ + return player.songs[index]; + } +} +throw new Error("This song are not exist") } - function removeSong(id) { - // your code here + let songIndex=player.songs.indexOf(getSongByID(id)) + player.songs.splice(songIndex,1); + for (let i=0; imax){ + max === player.songs[index].id; + } +} +return max; } - function addSong(title, album, artist, duration, id) { - // your code here + for (let index = 0; index < player.songs.length; index++) { + if(player.songs[index].id===id){ + throw new Error("Sorry , this ID already taken") + } + } + if(id===undefined){ + id===maxID()+1; + } + duration = duration.split(":"); + duration = parseInt(duration[0] *60) + parseInt(duration[1]); + let newSong= { + id: id, + title: title, + album: album, + artist: artist, + duration: duration }; + + player.songs.push(newSong); + return id; } function removePlaylist(id) { From 8048a31047bfcacc11d3c45861e717ec214452da Mon Sep 17 00:00:00 2001 From: NativAmar <89576789+NativAmar@users.noreply.github.com> Date: Fri, 10 Sep 2021 11:02:14 +0300 Subject: [PATCH 3/9] removePlaylist#4 --- index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index c05f85d..0c2a5f6 100644 --- a/index.js +++ b/index.js @@ -121,9 +121,18 @@ function addSong(title, album, artist, duration, id) { player.songs.push(newSong); return id; } +function getPlaylistByID(id){ + for (let index = 0; index < player.playlists.length; index++) { + if(player.playlists[index].id === id){ + return player.playlists[index]; + } +} +throw new Error("This playlist are not exist") +} function removePlaylist(id) { - // your code here + let playlistIndex=getPlaylistByID(id); + player.playlists.splice(playlistIndex,1); } function createPlaylist(name, id) { From dc5abf8b29e47a1d56278eba8adf017e7429820e Mon Sep 17 00:00:00 2001 From: NativAmar <89576789+NativAmar@users.noreply.github.com> Date: Fri, 10 Sep 2021 13:29:39 +0300 Subject: [PATCH 4/9] createPlaylist#5 --- index.js | 70 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 52 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 0c2a5f6..4104a78 100644 --- a/index.js +++ b/index.js @@ -51,6 +51,8 @@ const player = { console.log(`Playing ${song.title} from ${song.album} by ${song.artist} | ${durationConvert(song.duration)}.`) }, } + + function durationConvert(duration) { let min = Math.floor(duration / 60); @@ -64,6 +66,8 @@ function durationConvert(duration) } return min+':'+sec } + + function playSong(id) { for (let song of player.songs){ if (song.id === id) @@ -73,6 +77,8 @@ function playSong(id) { } throw new Error("This ID are not exist"); } + + function getSongByID(id){ for (let index = 0; index < player.songs.length; index++) { if(player.songs[index].id === id){ @@ -81,6 +87,8 @@ function getSongByID(id){ } throw new Error("This song are not exist") } + + function removeSong(id) { let songIndex=player.songs.indexOf(getSongByID(id)) player.songs.splice(songIndex,1); @@ -91,24 +99,22 @@ function removeSong(id) { } } } -function maxID(){ - let max=0; - for (let index = 0; index < player.songs.length; index++) { - if(player.songs[index].id>max){ - max === player.songs[index].id; - } -} -return max; -} + + function addSong(title, album, artist, duration, id) { + let newID=0; + let max=1; + if(id){ for (let index = 0; index < player.songs.length; index++) { if(player.songs[index].id===id){ - throw new Error("Sorry , this ID already taken") - } - } - if(id===undefined){ - id===maxID()+1; + throw new Error("Sorry , this ID already taken") }}} + else { + for (let i = 0; i < player.songs.length; i++) { + if(player.songs[i].id>max){ + max === player.songs[i].id;} } + newID=max+1; +} duration = duration.split(":"); duration = parseInt(duration[0] *60) + parseInt(duration[1]); let newSong= { @@ -117,10 +123,11 @@ function addSong(title, album, artist, duration, id) { album: album, artist: artist, duration: duration }; - player.songs.push(newSong); - return id; + return newID; } + + function getPlaylistByID(id){ for (let index = 0; index < player.playlists.length; index++) { if(player.playlists[index].id === id){ @@ -130,17 +137,44 @@ function getPlaylistByID(id){ throw new Error("This playlist are not exist") } + function removePlaylist(id) { let playlistIndex=getPlaylistByID(id); player.playlists.splice(playlistIndex,1); } + function createPlaylist(name, id) { - // your code here + let newP_ID=0 + let maxP_ID=1 + if(id){ + for (let index = 0; index < player.playlists.length; index++) { + if(player.playlists[index].id===id){ + throw new Error("Sorry , this ID already taken") + } + } + } +else { + for (let i = 0; i < player.playlists.length; i++) { + if(player.playlists[i].id>maxP_ID){ + maxP_ID === player.playlists[i].id; + } +} +newP_ID=maxP_ID; +} + + let newPlaylist={ + id:id, + name:name, + songs:[] + } + player.playlists.push(newPlaylist); + return newP_ID; } + function playPlaylist(id) { - // your code here + } function editPlaylist(playlistId, songId) { From 529c6f866ef379d33fcd26b94a203450e68f5140 Mon Sep 17 00:00:00 2001 From: NativAmar <89576789+NativAmar@users.noreply.github.com> Date: Fri, 10 Sep 2021 14:15:17 +0300 Subject: [PATCH 5/9] playPlaylist #6 --- index.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 4104a78..94614d7 100644 --- a/index.js +++ b/index.js @@ -174,11 +174,18 @@ newP_ID=maxP_ID; function playPlaylist(id) { - + let Pindex=getPlaylistByID(id); + for (let i = 0; i < Pindex.songs.length; i++) { + playSong (Pindex.songs[i]); + } } + function editPlaylist(playlistId, songId) { - // your code here + + + } + } } function playlistDuration(id) { From 97e987ccaae71c409a635859d5e5166daa9ad36b Mon Sep 17 00:00:00 2001 From: NativAmar <89576789+NativAmar@users.noreply.github.com> Date: Fri, 10 Sep 2021 16:21:48 +0300 Subject: [PATCH 6/9] editPlaylist#7 --- index.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 94614d7..c746cba 100644 --- a/index.js +++ b/index.js @@ -181,25 +181,68 @@ function playPlaylist(id) { } -function editPlaylist(playlistId, songId) { - +function indexSongInPlaylist( playlistId,songId) { + let playlist = getPlaylistByID(playlistId) + for (let index = 0; index < playlist.songs.length; index++) { + if (playlist.songs[index] == songId) + return index; + } + return -1; +} - } - } + +function songExist(songId){ + for(let i=0;i 1){ + playlist.songs.splice(songIndex,1) + } + else{ + removePlaylist(playlist.id) + } + } + else throw new Error("Somthing wrong"); +} + + function playlistDuration(id) { // your code here } + function searchByQuery(query) { // your code here } + function searchByDuration(duration) { // your code here } + module.exports = { player, playSong, From 8538180c002f2575877f990b8c8f73c684d01c16 Mon Sep 17 00:00:00 2001 From: NativAmar <89576789+NativAmar@users.noreply.github.com> Date: Fri, 10 Sep 2021 17:10:32 +0300 Subject: [PATCH 7/9] playlistDuration#8 --- index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index c746cba..5918b45 100644 --- a/index.js +++ b/index.js @@ -229,7 +229,12 @@ function editPlaylist(playlistId, songId) { function playlistDuration(id) { - // your code here + let allDuration=0; + let playlist=getPlaylistByID(id) + for(let i=0;i Date: Fri, 10 Sep 2021 22:07:07 +0300 Subject: [PATCH 8/9] searchByQuery#9 --- index.js | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 5918b45..b4da7e9 100644 --- a/index.js +++ b/index.js @@ -239,9 +239,37 @@ function playlistDuration(id) { function searchByQuery(query) { - // your code here -} + query=query.toLowerCase(); + let obj={ + songs:[], + playlists:[] + }; + for (let song of player.songs) + { + if( (song.title.toLowerCase().includes(query)) || + (song.album.toLowerCase().includes(query)) || + (song.artist.toLowerCase().includes(query)) ) + { + obj.songs.push(song); + } + } + for (let playlist of player.playlists) + { + if (playlist.name.toLowerCase().includes(query)) + { + obj.playlists.push(playlist); + } + } + obj.songs.sort(function (a, b) { + if (a.title < b.title){ + return -1; } + else if (a.title > b.title){ + return 1; } + return 0; + }) + return obj; +} function searchByDuration(duration) { // your code here From ec724487fcea08d313bced91b9be02be8fbbe6ba Mon Sep 17 00:00:00 2001 From: NativAmar <89576789+NativAmar@users.noreply.github.com> Date: Sat, 11 Sep 2021 14:13:46 +0300 Subject: [PATCH 9/9] searchByDuration#10 --- index.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index b4da7e9..89fbb4f 100644 --- a/index.js +++ b/index.js @@ -272,10 +272,33 @@ function searchByQuery(query) { } function searchByDuration(duration) { - // your code here + duration = duration.split(":"); + duration = parseInt(duration[0] *60) + parseInt(duration[1]); + + let closest = player.songs[0]; + let defoultClosest = Math.abs(player.songs[0].duration - duration); + + for (let i=0;i