From 350835bded686539cc14624b65811e7b03d1b693 Mon Sep 17 00:00:00 2001 From: Tomeryac <89574827+Tomeryac@users.noreply.github.com> Date: Thu, 9 Sep 2021 12:23:17 +0300 Subject: [PATCH 1/9] test 1+2 playsong function --- index.js | 25 ++++++++++++++++++++++--- package-lock.json | 16 ++++++++++------ package.json | 3 +++ 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 10f4784..8b7025f 100644 --- a/index.js +++ b/index.js @@ -48,13 +48,31 @@ const player = { { id: 5, name: 'Israeli', songs: [4, 5] }, ], playSong(song) { - console.log(/* your code here */) + let mm; + let ss; + let found; + this.songs.forEach(element => { // check if the element of the right id exist// + if(element.id == song){ + mm = Math.floor(element.duration/60); // calculate the minutes// + if(mm<10) + mm = "0"+ mm; + ss = element.duration % 60; // calculate the second// + found = element; + } + }); + try{ + console.log(`Playing ${found.title} from ${found.album} by ${found.artist} | ${mm}:${ss}.`); + } + catch(error){ + console.log("this song is unavailable"); + } }, -} +} function playSong(id) { - // your code here + player.playSong(id); } +playSong(33); function removeSong(id) { // your code here @@ -105,3 +123,4 @@ module.exports = { searchByQuery, searchByDuration, } + diff --git a/package-lock.json b/package-lock.json index b61f807..9a3331b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -957,14 +957,12 @@ "balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", - "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", - "dev": true + "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" }, "brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", - "dev": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -1115,8 +1113,7 @@ "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", - "dev": true + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" }, "convert-source-map": { "version": "1.8.0", @@ -2346,7 +2343,6 @@ "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", - "dev": true, "requires": { "brace-expansion": "^1.1.7" } @@ -2618,6 +2614,14 @@ "glob": "^7.1.3" } }, + "run": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/run/-/run-1.4.0.tgz", + "integrity": "sha1-4X2ekEOrL+F3dsspnhI3848LT/o=", + "requires": { + "minimatch": "*" + } + }, "safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", diff --git a/package.json b/package.json index 3ba8b23..d613338 100644 --- a/package.json +++ b/package.json @@ -25,5 +25,8 @@ "testMatch": [ "**/__tests__/**/*.test.+(ts|tsx|js)" ] + }, + "dependencies": { + "run": "^1.4.0" } } From 06ee962f53c54bc198d38842f3b55945509db2e4 Mon Sep 17 00:00:00 2001 From: Tomeryac <89574827+Tomeryac@users.noreply.github.com> Date: Thu, 9 Sep 2021 20:49:51 +0300 Subject: [PATCH 2/9] new test 1-2 and test 3-5 removesong function --- index.js | 65 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/index.js b/index.js index 8b7025f..47436c6 100644 --- a/index.js +++ b/index.js @@ -48,34 +48,36 @@ const player = { { id: 5, name: 'Israeli', songs: [4, 5] }, ], playSong(song) { - let mm; - let ss; - let found; - this.songs.forEach(element => { // check if the element of the right id exist// - if(element.id == song){ - mm = Math.floor(element.duration/60); // calculate the minutes// - if(mm<10) - mm = "0"+ mm; - ss = element.duration % 60; // calculate the second// - found = element; - } - }); - try{ - console.log(`Playing ${found.title} from ${found.album} by ${found.artist} | ${mm}:${ss}.`); - } - catch(error){ - console.log("this song is unavailable"); - } + console.log("Playing "+player.songs[song].title+" from "+player.songs[song].album+" by "+player.songs[song].artist+" | "+songDuration(player.songs[song].duration)+"."); }, } function playSong(id) { - player.playSong(id); + let num=songById(id); + if(num==-1) + throw("ID doesn't exist"); + player.playSong(num); } -playSong(33); +playSong(1); +removeSong(4); function removeSong(id) { - // your code here + let idR = songById(id); + if (idR==-1) + throw("ID doesn't exist!"); + player.songs.splice(idR,1); + for (let i = 0; i < player.playlists.length; i++) { + for (let j = 0; j < player.playlists[i].songs.length; j++) { + if(player.playlists[i].songs[j]==id){ + player.playlists[i].songs.splice(j,1); + console.log(player.playlists[i].songs.length); + } + } + + +} + + } function addSong(title, album, artist, duration, id) { @@ -109,6 +111,27 @@ function searchByQuery(query) { function searchByDuration(duration) { // your code here } +// found the index in the array of the id +function songById(idT) +{ + for (let i = 0; i < player.songs.length; i++) { + if(player.songs[i].id==idT) + return i; + } + return -1; +} +// get duration and return it in min:sec format +function songDuration(duration){ + let min=0; + while(duration >=60) + { + min++; + duration-=60; + } + if(duration<10) + return "0"+min+":0"+duration; + return "0"+min+":"+duration; +} module.exports = { player, From 077f7c571eb58ee58c45231c0b1e5b92faa7c6d6 Mon Sep 17 00:00:00 2001 From: Tomeryac <89574827+Tomeryac@users.noreply.github.com> Date: Thu, 9 Sep 2021 21:50:12 +0300 Subject: [PATCH 3/9] test 6 function addSong --- index.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 47436c6..ed41064 100644 --- a/index.js +++ b/index.js @@ -72,16 +72,23 @@ function removeSong(id) { player.playlists[i].songs.splice(j,1); console.log(player.playlists[i].songs.length); } - } - - -} - - + } +} } - function addSong(title, album, artist, duration, id) { - // your code here +if(songById(id)!=-1){ + let num=1; + while(songById(num)==-1) + { + num++; + } + id=num; + let temp = duration.split(":"); + let min = temp[0]*60; + let sec = temp[1]*1 + let ssduration = min + sec; +} +player.songs.push({id:id,title:title,album:album,artist:artist,duration:ssduration}); } function removePlaylist(id) { From ec6abf764db9c59f511653cb466726a67d66d747 Mon Sep 17 00:00:00 2001 From: Tomeryac <89574827+Tomeryac@users.noreply.github.com> Date: Fri, 10 Sep 2021 12:38:55 +0300 Subject: [PATCH 4/9] test 7-10 removeplaylist --- index.js | 69 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index ed41064..7137838 100644 --- a/index.js +++ b/index.js @@ -59,7 +59,7 @@ function playSong(id) { player.playSong(num); } playSong(1); - +//check if the id exist in the playlist.song and remove the id removeSong(4); function removeSong(id) { let idR = songById(id); @@ -74,29 +74,58 @@ function removeSong(id) { } } } -} +} // check if the id exist and add the song to player.song function addSong(title, album, artist, duration, id) { -if(songById(id)!=-1){ - let num=1; - while(songById(num)==-1) - { - num++; - } - id=num; - let temp = duration.split(":"); + let temp = duration.split(":"); let min = temp[0]*60; - let sec = temp[1]*1 + let sec = temp[1]*1; let ssduration = min + sec; -} + + if (id == undefined) id = 1; + for (let i = 0; i < player.songs.length; i++) { + const song = player.songs[i]; + if(song.id > id) + id = song.id +1; + }/* + if(songById(id)!=-1){ + let num=1; + + while(songById(num)==-1) + { + num++; + } + id=num; + +}*/ +player.songs.forEach(song => { + if(song.id == id) + throw "exception"; +}); +console.log("the id is:"+id); player.songs.push({id:id,title:title,album:album,artist:artist,duration:ssduration}); +return id; } function removePlaylist(id) { - // your code here + let found = false; + for (let i = 0; i < player.playlists.length && !found; i++) { + if(player.playlists[i].id==id){ + player.playlists.splice(i,1); + found = true; + } + } + if(!found){ + throw "ID does not exist"; + } } -function createPlaylist(name, id) { - // your code here +function createPlaylist(name, id) +{ + for (let i = 0; i < player.playlists.length; i++) { + if(player.playlists[i].id==id){ + + } + } } function playPlaylist(id) { @@ -118,9 +147,19 @@ function searchByQuery(query) { function searchByDuration(duration) { // your code here } +//check if the the idT exist in the array playlist +function playlistsById(idT) +{ + for (let i = 0; i < player.playlists.length; i++) { + if(player.playlists[i].id==idT) + return i; + } + return -1; +} // found the index in the array of the id function songById(idT) { + if (idT == undefined) idT = 0; for (let i = 0; i < player.songs.length; i++) { if(player.songs[i].id==idT) return i; From 9bb43dbe2306e211d27aa5e4e0bc4b5bf1d8859e Mon Sep 17 00:00:00 2001 From: Tomeryac <89574827+Tomeryac@users.noreply.github.com> Date: Fri, 10 Sep 2021 13:43:59 +0300 Subject: [PATCH 5/9] test 11-13 createPlaylist --- index.js | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 7137838..1e5605a 100644 --- a/index.js +++ b/index.js @@ -118,16 +118,25 @@ function removePlaylist(id) { throw "ID does not exist"; } } - -function createPlaylist(name, id) -{ - for (let i = 0; i < player.playlists.length; i++) { - if(player.playlists[i].id==id){ - - } +function createPlaylist(name, id){ + if(id==undefined) { + id = uniqueId(); + } + else{ + player.playlists.forEach(playlist => { + if(playlist.id==id) + throw "id exsist"; + }); } + player.playlists.push({id:id, name:name, songs:[]}); + return id; } + + + + + function playPlaylist(id) { // your code here } @@ -147,14 +156,17 @@ function searchByQuery(query) { function searchByDuration(duration) { // your code here } -//check if the the idT exist in the array playlist -function playlistsById(idT) -{ - for (let i = 0; i < player.playlists.length; i++) { - if(player.playlists[i].id==idT) - return i; +//check the id and give the max id +1 +function uniqueId(){ +let id =1; +player.playlists.forEach(playlist => { + if (playlist.id>id) { + id = playlist.id; + } - return -1; + +}); +return id+1; } // found the index in the array of the id function songById(idT) From 78eee07fe8d8794a2121572f4b1ed3c602c4f1bb Mon Sep 17 00:00:00 2001 From: Tomeryac <89574827+Tomeryac@users.noreply.github.com> Date: Fri, 10 Sep 2021 14:21:44 +0300 Subject: [PATCH 6/9] test 13-15 playPlaylist --- index.js | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 1e5605a..5e27042 100644 --- a/index.js +++ b/index.js @@ -58,7 +58,6 @@ function playSong(id) { throw("ID doesn't exist"); player.playSong(num); } -playSong(1); //check if the id exist in the playlist.song and remove the id removeSong(4); function removeSong(id) { @@ -105,19 +104,19 @@ console.log("the id is:"+id); player.songs.push({id:id,title:title,album:album,artist:artist,duration:ssduration}); return id; } - +//check if the id exsist and remove it from the playlists function removePlaylist(id) { let found = false; for (let i = 0; i < player.playlists.length && !found; i++) { if(player.playlists[i].id==id){ - player.playlists.splice(i,1); - found = true; + player.playlists.splice(i,1); + found = true; } } if(!found){ throw "ID does not exist"; } -} +} //check if the id exsist and add new playlist to the playlists function createPlaylist(name, id){ if(id==undefined) { id = uniqueId(); @@ -130,21 +129,26 @@ function createPlaylist(name, id){ } player.playlists.push({id:id, name:name, songs:[]}); return id; -} - - - - - - +}// get a playlist id and play the song in the playlist. function playPlaylist(id) { - // your code here + let p; + player.playlists.forEach(playlist => { + if(playlist.id == id) + p = playlist; + }); + if(p != undefined) + { + p.songs.forEach(song => { + playSong(song); + }); + } + else{ + throw " id does not exist"; + } } function editPlaylist(playlistId, songId) { - // your code here } - function playlistDuration(id) { // your code here } From d54cf5b1d4507eb2aca63924c7ccec2dfec0f348 Mon Sep 17 00:00:00 2001 From: Tomeryac <89574827+Tomeryac@users.noreply.github.com> Date: Sat, 11 Sep 2021 14:51:53 +0300 Subject: [PATCH 7/9] test 16-21 function editPlaylist function playlistDuration --- index.js | 60 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 5e27042..4903e32 100644 --- a/index.js +++ b/index.js @@ -85,17 +85,8 @@ function addSong(title, album, artist, duration, id) { const song = player.songs[i]; if(song.id > id) id = song.id +1; - }/* - if(songById(id)!=-1){ - let num=1; - - while(songById(num)==-1) - { - num++; } - id=num; -}*/ player.songs.forEach(song => { if(song.id == id) throw "exception"; @@ -146,14 +137,57 @@ function playPlaylist(id) { throw " id does not exist"; } } - +//found the index of the playlist and the song function editPlaylist(playlistId, songId) { + let songIndex; + let playlistIndex; + for (let i = 0; i < player.playlists.length; i++) { + if (player.playlists[i].id == playlistId) { + playlistIndex = i; + for (let j = 0; j < player.playlists[i].songs.length; j++) { + if (player.playlists[i].songs[j] == songId) { + songIndex = j; + } + + } + + } + + } + if(songIndex != undefined){ // if the song exsist in the playlist and there is no more song remove the playlist + if(player.playlists[playlistIndex].songs.length == 1){ + player.playlists.splice(playlistIndex,1); + + } + else{ + player.playlists[playlistIndex].songs.splice(songIndex,1);// if the song exsist in the plqylist remove the song + } + } + else{ + player.playlists[playlistIndex].songs.push(songId);// if the song dosent exsist in the playlist add the song + } + songById(songId); + } +// function that gets id of playlist and returns the sum of duration of all the songs in the playlist function playlistDuration(id) { - // your code here + let countDuration = 0; + let playlistIndex; + for (let i = 0; i< player.playlists.length; i++) { + if(player.playlists[i].id == id) + playlistIndex = i; + } + + for (let i = 0; i < player.playlists[playlistIndex].songs.length; i++) { + for (let j = 0; j < player.songs.length; j++) { + if(player.songs[j].id == player.playlists[playlistIndex].songs[i]) + countDuration += player.songs[j].duration; + } +} +return countDuration; } -function searchByQuery(query) { +function searchByQuery(query){ // your code here } @@ -180,7 +214,7 @@ function songById(idT) if(player.songs[i].id==idT) return i; } - return -1; + throw ("non-existent song ID"); } // get duration and return it in min:sec format function songDuration(duration){ From 1f23ddc07c36080751e226cd5bf9561dcfde1002 Mon Sep 17 00:00:00 2001 From: Tomeryac <89574827+Tomeryac@users.noreply.github.com> Date: Sat, 11 Sep 2021 17:00:27 +0300 Subject: [PATCH 8/9] test 21-26 playlistDuration searchByQuery searchByDuration --- index.js | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 69 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 4903e32..d21b433 100644 --- a/index.js +++ b/index.js @@ -69,7 +69,6 @@ function removeSong(id) { for (let j = 0; j < player.playlists[i].songs.length; j++) { if(player.playlists[i].songs[j]==id){ player.playlists[i].songs.splice(j,1); - console.log(player.playlists[i].songs.length); } } } @@ -91,7 +90,6 @@ player.songs.forEach(song => { if(song.id == id) throw "exception"; }); -console.log("the id is:"+id); player.songs.push({id:id,title:title,album:album,artist:artist,duration:ssduration}); return id; } @@ -186,13 +184,78 @@ function playlistDuration(id) { } return countDuration; } - +//function that get a query and return object with all the songs and the playlists that include the query function searchByQuery(query){ - // your code here -} + let obj = { + songs:[], + playlists:[] + } + query = query.toLowerCase(); + for (let i = 0; i < player.songs.length; i++) { + const song = player.songs[i]; + if(song.title.toLowerCase().includes(query) || song.album.toLowerCase().includes(query) || song.artist.toLowerCase().includes(query)) + obj.songs.push(song); + } + for (let i = 0; i < player.playlists.length; i++) { + const playlist = player.playlists[i]; + if(playlist.name.toLowerCase().includes(query)) + obj.playlists.push(playlist); + } + obj.songs.sort(function(a, b){ + let titleA = a.title.toLowerCase(); + let titleB = b.title.toLowerCase(); + if (titleA < titleB) + { + return -1; + } + else if (titleA > titleB) + { + return 1; + } + return 0; + }); + + obj.playlists.sort(function(a, b){ + let titleA = a.title.toLowerCase(); + let titleB = b.title.toLowerCase(); + if (titleA < titleB) + { + return -1; + } + else if (titleA > titleB) + { + return 1; + } + return 0; + }); + return obj; + +} +//function that get a duration and check what is the closest duration in the songs and playlists and return the closest one. function searchByDuration(duration) { - // your code here + let time = duration.split(":"); + let min = time[0]* 60; + let sec = time[1]*1; + let sumsec = min+sec; + let MinDiffrent = Math.abs(player.songs[0].duration - sumsec); + let obj = player.songs[0]; + for (let i = 0; i < player.songs.length; i++) { + const song = player.songs[i]; + if(MinDiffrent > Math.abs(song.duration - sumsec)){ + MinDiffrent = Math.abs(song.duration - sumsec); + obj = song; + } + } + for (let i = 0; i < player.playlists.length; i++) { + const playlist = player.playlists[i]; + if((Math.abs(playlistDuration(playlist.id) - sumsec)) < MinDiffrent){ + MinDiffrent = Math.abs(playlistDuration(playlist.id) - sumsec); + obj = playlist; + } + } + + return obj; } //check the id and give the max id +1 function uniqueId(){ From 24329c1d6a5c1034c7040ff7cf939cdbf5506d23 Mon Sep 17 00:00:00 2001 From: Tomeryac <89574827+Tomeryac@users.noreply.github.com> Date: Sun, 12 Sep 2021 11:43:39 +0300 Subject: [PATCH 9/9] Update index.js --- index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index d21b433..ec4d152 100644 --- a/index.js +++ b/index.js @@ -93,7 +93,7 @@ player.songs.forEach(song => { player.songs.push({id:id,title:title,album:album,artist:artist,duration:ssduration}); return id; } -//check if the id exsist and remove it from the playlists +//check if the id exsist and remove it from the playlists. function removePlaylist(id) { let found = false; for (let i = 0; i < player.playlists.length && !found; i++) { @@ -105,7 +105,7 @@ function removePlaylist(id) { if(!found){ throw "ID does not exist"; } -} //check if the id exsist and add new playlist to the playlists +} //check if the id exsist and add new playlist to the playlists. function createPlaylist(name, id){ if(id==undefined) { id = uniqueId(); @@ -135,7 +135,7 @@ function playPlaylist(id) { throw " id does not exist"; } } -//found the index of the playlist and the song +//found the index of the playlist and the song. function editPlaylist(playlistId, songId) { let songIndex; let playlistIndex; @@ -167,7 +167,7 @@ function editPlaylist(playlistId, songId) { songById(songId); } -// function that gets id of playlist and returns the sum of duration of all the songs in the playlist +// function that gets id of a playlist and returns the sum of the duration of all the songs in the playlist. function playlistDuration(id) { let countDuration = 0; let playlistIndex; @@ -184,7 +184,7 @@ function playlistDuration(id) { } return countDuration; } -//function that get a query and return object with all the songs and the playlists that include the query +//function that get a string and return an object with all the songs and the playlists that include the string. function searchByQuery(query){ let obj = { songs:[], @@ -232,7 +232,7 @@ function searchByQuery(query){ return obj; } -//function that get a duration and check what is the closest duration in the songs and playlists and return the closest one. +//function that get a duration and check which playlist's/song's duration is closest to the duration picked. function searchByDuration(duration) { let time = duration.split(":"); let min = time[0]* 60; @@ -257,7 +257,7 @@ function searchByDuration(duration) { return obj; } -//check the id and give the max id +1 +//check the id and returns a new unused id. function uniqueId(){ let id =1; player.playlists.forEach(playlist => {