From f06b9ef43f87975fb402755346ce3256289f148c Mon Sep 17 00:00:00 2001 From: tamartzuf Date: Fri, 10 Sep 2021 15:24:59 +0300 Subject: [PATCH 01/11] play song and remove song are working --- index.js | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 10f4784..3dca00f 100644 --- a/index.js +++ b/index.js @@ -48,18 +48,46 @@ 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} | ${convertDuration(song.duration)}.`) }, } +function convertDuration(duration) +{ + let minutes=Math.floor(duration/60); + let seconds=duration%60; + if(minutes<10) + minutes="0"+minutes; + if(seconds<10) + seconds="0"+seconds; + return minutes+":"+seconds; +} +function playSong(id) +{ + + var wantedSong = player.songs.find(res => res.id == id); + if(wantedSong===null){ + throw new Error("ID not found"); + } + player.playSong(wantedSong); -function playSong(id) { - // your code here } + function removeSong(id) { - // your code here + var wantedSong = player.songs.find(res => res.id == id); + + let index = player.songs.indexOf(wantedSong); + if(index ===-1){ + throw new Error("ID not found"); + } + player.songs.splice(index,1); + player.playlists.forEach(element => { + let indexpl = element.songs.indexOf(id); + element.songs.splice(indexpl, 1); + }); } + function addSong(title, album, artist, duration, id) { // your code here } @@ -105,3 +133,8 @@ module.exports = { searchByQuery, searchByDuration, } + + +function Main(){ + player.playSong(1); +} \ No newline at end of file From e27d9ec37f8ee7ade07b0a3ffa5ce57e3e666072 Mon Sep 17 00:00:00 2001 From: tamartzuf Date: Fri, 10 Sep 2021 16:44:51 +0300 Subject: [PATCH 02/11] add song functiom --- index.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 3dca00f..a64d41e 100644 --- a/index.js +++ b/index.js @@ -89,7 +89,35 @@ function removeSong(id) { function addSong(title, album, artist, duration, id) { - // your code here + +if (arguments.length < 5 || id === undefined){ + var id = 1; + player.songs.forEach(element => { + id += element.id; + }); + + +} + +player.songs.forEach(element => { + if(element.id === id){ + throw new Error("the ID is taken"); + } +}); + +durationArr=duration.split(":"); +duration=parseInt(durationArr[0])*60+parseInt(durationArr[1]); +var newSong = { + id: id, + title: title, + album: album, + artist: artist, + duration: duration, +}; + + player.songs.push(newSong); + + return id; } function removePlaylist(id) { From bbd4dde18b99a781e652cfa5fc6b230af1c8c617 Mon Sep 17 00:00:00 2001 From: tamartzuf Date: Fri, 10 Sep 2021 16:50:51 +0300 Subject: [PATCH 03/11] remove playlist --- index.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index a64d41e..55a287a 100644 --- a/index.js +++ b/index.js @@ -95,8 +95,6 @@ if (arguments.length < 5 || id === undefined){ player.songs.forEach(element => { id += element.id; }); - - } player.songs.forEach(element => { @@ -116,12 +114,18 @@ var newSong = { }; player.songs.push(newSong); - return id; } function removePlaylist(id) { - // your code here + var wantedplay = player.playlists.find(res => res.id == id); + + let index = player.playlists.indexOf(wantedplay); + if(index ===-1){ + throw new Error("ID not found"); + } + + player.playlists.splice(index,1); } function createPlaylist(name, id) { @@ -163,6 +167,3 @@ module.exports = { } -function Main(){ - player.playSong(1); -} \ No newline at end of file From f6a29a959dde17f62462311ae767b37b012a0b3d Mon Sep 17 00:00:00 2001 From: tamartzuf Date: Fri, 10 Sep 2021 17:03:58 +0300 Subject: [PATCH 04/11] create playlist --- index.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 55a287a..bb6a7e5 100644 --- a/index.js +++ b/index.js @@ -129,7 +129,23 @@ function removePlaylist(id) { } function createPlaylist(name, id) { - // your code here + if(arguments < 2 || id===undefined) + { + id=1; + player.playlists.forEach(element=> { + id+=element.id; + }); + } + player.playlists.forEach (element => { + if(id===element.id){ + throw new Error("ID is already exist"); + } + }); + var newPlay={ + id: id, name: name , songs: [] + }; + player.playlists.push(newPlay); + return id; } function playPlaylist(id) { From 22f8d836e1c56a1b2252aab10b23304ec9dff8d8 Mon Sep 17 00:00:00 2001 From: tamartzuf Date: Fri, 10 Sep 2021 17:11:17 +0300 Subject: [PATCH 05/11] create playlist --- index.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index bb6a7e5..687f17f 100644 --- a/index.js +++ b/index.js @@ -149,7 +149,15 @@ function createPlaylist(name, id) { } function playPlaylist(id) { - // your code here + var wantedPlay=player.playlists.find(element=> element.id==id) ; +var index = player.playlists.indexOf(wantedPlay); + + if(index===-1) + { + throw new Error("ID is not exist"); + } +wantedPlay.songs.forEach(element => playSong(element)); + } function editPlaylist(playlistId, songId) { From bf9ee91e610a74466a3fa64027fc550c13baf106 Mon Sep 17 00:00:00 2001 From: tamartzuf Date: Fri, 10 Sep 2021 17:45:22 +0300 Subject: [PATCH 06/11] function edit playplaylist --- index.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 687f17f..102a1aa 100644 --- a/index.js +++ b/index.js @@ -161,7 +161,30 @@ wantedPlay.songs.forEach(element => playSong(element)); } function editPlaylist(playlistId, songId) { - // your code here + var wantedPlay=player.playlists.find(element => element.id==playlistId) ; + var indexP = player.playlists.indexOf(wantedPlay); + + if(indexP===-1) + { + throw new Error("ID playlist is not exist"); + } + var wantedSong=player.songs.find(element => element.id==songId) ; + var indexS = player.songs.indexOf(wantedSong); + + if(indexS===-1) + { + throw new Error("ID song is not exist"); + } + + var indexSongPlaylist = player.playlists[indexP].songs.indexOf(songId); + if(indexSongPlaylist===-1){ + player.playlists[indexP].songs.push(songId); + + }else if(player.playlists[indexP].songs.length === 1){ + player.playlists.splice(indexP, 1); + }else{ + player.playlists[indexP].songs.splice(indexSongPlaylist, 1); + } } function playlistDuration(id) { From 80f986e8e09a0e16d39fb6c7da8a84e00d762acf Mon Sep 17 00:00:00 2001 From: tamartzuf Date: Fri, 10 Sep 2021 19:53:09 +0300 Subject: [PATCH 07/11] function playlistDuration --- index.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 102a1aa..963a15d 100644 --- a/index.js +++ b/index.js @@ -188,9 +188,17 @@ function editPlaylist(playlistId, songId) { } function playlistDuration(id) { - // your code here + var sum=0; + var wantedPlaylist= player.playlists.find(element => element.id==id) ; + for(let i=0; i elem.id==wantedPlaylist.songs[i]); + sum += wantedSong.duration; + } + return sum; + } + function searchByQuery(query) { // your code here } From 2cfda371727c574b360705cc276d339c45db082d Mon Sep 17 00:00:00 2001 From: tamartzuf Date: Fri, 10 Sep 2021 21:21:16 +0300 Subject: [PATCH 08/11] functionn searchByDuration --- index.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 963a15d..1c8d74b 100644 --- a/index.js +++ b/index.js @@ -204,7 +204,31 @@ function searchByQuery(query) { } function searchByDuration(duration) { - // your code here +var durationArr=duration.split(":"); +var time=parseInt(durationArr[0]*60) + parseInt(durationArr[1]) ; + let closestObj; + let minTime; + + //set minimum time according to duration + minTime=Math.abs( time-player.songs[0].duration); + + //search in songs + for(let song of player.songs){ + if(Math.abs(time - song.duration) <= minTime){ + closestObj = song; + minTime = Math.abs(time - song.duration); + } + } + + //search in playlists + for (let playlist of player.playlists) { + if (Math.abs(time - playlistDuration(playlist.id)) < minTime) { + closestObj = playlist; + minTime = Math.abs(time - playlistDuration(playlist.id)); + } + } + + return closestObj; } module.exports = { From 88bd2461f207eff96546432968ce7fbd94f5529f Mon Sep 17 00:00:00 2001 From: tamartzuf Date: Sat, 11 Sep 2021 13:37:19 +0300 Subject: [PATCH 09/11] functionn searchByQuery --- index.js | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 1c8d74b..2bce52b 100644 --- a/index.js +++ b/index.js @@ -200,9 +200,43 @@ function playlistDuration(id) { function searchByQuery(query) { - // your code here + query=query.toLowerCase(); + let songs=[]; + let playlists=[]; + for(let song of player.songs){ + if (song.title.toLowerCase().includes(query) || + song.album.toLowerCase().includes(query) || + song.artist.toLowerCase().includes(query)){ + songs.push(song); + } + } + for(let playlist of player.playlists){ + if(playlist.name.toLowerCase().includes(query)){ + playlists.push(playlist); + } + } + + songs.sort(function (a, b) { + if (a.title < b.title){ + return -1; } + else if (a.title > b.title){ + return 1; } + return 0; + }); + + //sort playlist array by name + playlists.sort(function (a, b) { + if (a.name < b.name){ + return -1; } + if (a.name > b.name){ + return 1; } + return 0; + }); + + return {songs , playlists}; } + function searchByDuration(duration) { var durationArr=duration.split(":"); var time=parseInt(durationArr[0]*60) + parseInt(durationArr[1]) ; From ee52f02b44d608b6dd3db9300ad5e3c58c73297f Mon Sep 17 00:00:00 2001 From: tamartzuf Date: Sat, 11 Sep 2021 15:50:37 +0300 Subject: [PATCH 10/11] comments --- index.js | 76 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 59 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 2bce52b..2607d80 100644 --- a/index.js +++ b/index.js @@ -48,9 +48,12 @@ const player = { { id: 5, name: 'Israeli', songs: [4, 5] }, ], playSong(song) { + //change to the correct format and duration to mm:ss console.log(`Playing ${song.title} from ${song.album} by ${song.artist} | ${convertDuration(song.duration)}.`) }, } + + //convert duration to mm:ss format function convertDuration(duration) { let minutes=Math.floor(duration/60); @@ -61,35 +64,37 @@ function convertDuration(duration) seconds="0"+seconds; return minutes+":"+seconds; } + + function playSong(id) { - - var wantedSong = player.songs.find(res => res.id == id); + var wantedSong = player.songs.find(res => res.id == id); //finds the song with the ID if(wantedSong===null){ - throw new Error("ID not found"); + throw new Error("ID not found"); //if id not exist, throw error } player.playSong(wantedSong); - } function removeSong(id) { var wantedSong = player.songs.find(res => res.id == id); - - let index = player.songs.indexOf(wantedSong); + let index = player.songs.indexOf(wantedSong); //find the index of the wanted song if(index ===-1){ - throw new Error("ID not found"); + throw new Error("ID not found"); //if id not exist, throw error } - player.songs.splice(index,1); + //remove song from songs array and from playlists + player.songs.splice(index,1); player.playlists.forEach(element => { - let indexpl = element.songs.indexOf(id); - element.songs.splice(indexpl, 1); + //save the index of the wanted song in each playlist + let indexpl = element.songs.indexOf(id); + element.songs.splice(indexpl, 1); }); } function addSong(title, album, artist, duration, id) { +//create new id if (arguments.length < 5 || id === undefined){ var id = 1; player.songs.forEach(element => { @@ -97,14 +102,18 @@ if (arguments.length < 5 || id === undefined){ }); } +//throw error if the given id already exist player.songs.forEach(element => { if(element.id === id){ throw new Error("the ID is taken"); } }); +//change duration to mm:ss format durationArr=duration.split(":"); duration=parseInt(durationArr[0])*60+parseInt(durationArr[1]); + +//create new song object var newSong = { id: id, title: title, @@ -113,22 +122,28 @@ var newSong = { duration: duration, }; - player.songs.push(newSong); - return id; +//adding the new song to the songs array +player.songs.push(newSong); +return id; } + function removePlaylist(id) { + //findind the wanted playlist with the same id and the index of it in the playlists array var wantedplay = player.playlists.find(res => res.id == id); - let index = player.playlists.indexOf(wantedplay); + + //throw error if the given id is not exist if(index ===-1){ - throw new Error("ID not found"); + throw new Error("ID not found"); } - player.playlists.splice(index,1); + //removing the wanted playlist from the array + player.playlists.splice(index,1); } function createPlaylist(name, id) { + //creating new id if not exist if(arguments < 2 || id===undefined) { id=1; @@ -136,11 +151,15 @@ function createPlaylist(name, id) { id+=element.id; }); } + + //throw error if the given id already exist player.playlists.forEach (element => { if(id===element.id){ throw new Error("ID is already exist"); } }); + + //creating new playlist object and adding it to the playlists array var newPlay={ id: id, name: name , songs: [] }; @@ -149,13 +168,17 @@ function createPlaylist(name, id) { } function playPlaylist(id) { - var wantedPlay=player.playlists.find(element=> element.id==id) ; +//finding the wanted playlist and it index +var wantedPlay=player.playlists.find(element=> element.id==id) ; var index = player.playlists.indexOf(wantedPlay); +//throw error if the id not exist if(index===-1) { throw new Error("ID is not exist"); } + + //playing all the songs in the wanted playlist wantedPlay.songs.forEach(element => playSong(element)); } @@ -164,10 +187,12 @@ function editPlaylist(playlistId, songId) { var wantedPlay=player.playlists.find(element => element.id==playlistId) ; var indexP = player.playlists.indexOf(wantedPlay); + //throw error if the id of the given song or playlist doesnt exist if(indexP===-1) { throw new Error("ID playlist is not exist"); } + var wantedSong=player.songs.find(element => element.id==songId) ; var indexS = player.songs.indexOf(wantedSong); @@ -176,20 +201,31 @@ function editPlaylist(playlistId, songId) { throw new Error("ID song is not exist"); } + //the index of the song in the playlist var indexSongPlaylist = player.playlists[indexP].songs.indexOf(songId); + + //adding song to playlist if(indexSongPlaylist===-1){ player.playlists[indexP].songs.push(songId); + //removing playlist }else if(player.playlists[indexP].songs.length === 1){ player.playlists.splice(indexP, 1); - }else{ + } + + //removing song from playlist + else{ player.playlists[indexP].songs.splice(indexSongPlaylist, 1); } } function playlistDuration(id) { var sum=0; + + //getting the wanted playlist by id var wantedPlaylist= player.playlists.find(element => element.id==id) ; + + //if the song exist in the playlist, adding to the sum the duration for(let i=0; i elem.id==wantedPlaylist.songs[i]); sum += wantedSong.duration; @@ -200,9 +236,12 @@ function playlistDuration(id) { function searchByQuery(query) { + //case-insensetive query=query.toLowerCase(); let songs=[]; let playlists=[]; + + //checking if the query exist in the given songs array for(let song of player.songs){ if (song.title.toLowerCase().includes(query) || song.album.toLowerCase().includes(query) || @@ -210,12 +249,15 @@ function searchByQuery(query) { songs.push(song); } } + + //checking if the query exist in the given playlists array for(let playlist of player.playlists){ if(playlist.name.toLowerCase().includes(query)){ playlists.push(playlist); } } + //sort songs array by title songs.sort(function (a, b) { if (a.title < b.title){ return -1; } From 22ec7be53274503ead49846f21d39587c49ee521 Mon Sep 17 00:00:00 2001 From: tamartzuf Date: Sun, 12 Sep 2021 13:33:38 +0300 Subject: [PATCH 11/11] Update index.js --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 2607d80..1440dd2 100644 --- a/index.js +++ b/index.js @@ -152,7 +152,7 @@ function createPlaylist(name, id) { }); } - //throw error if the given id already exist + //throw error if the given id is already exist player.playlists.forEach (element => { if(id===element.id){ throw new Error("ID is already exist"); @@ -182,6 +182,7 @@ var index = player.playlists.indexOf(wantedPlay); wantedPlay.songs.forEach(element => playSong(element)); } +//some cool code function editPlaylist(playlistId, songId) { var wantedPlay=player.playlists.find(element => element.id==playlistId) ;