diff --git a/index.js b/index.js index 10f4784..ec4d152 100644 --- a/index.js +++ b/index.js @@ -48,48 +48,248 @@ const player = { { id: 5, name: 'Israeli', songs: [4, 5] }, ], playSong(song) { - console.log(/* your code here */) + 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) { - // your code here + let num=songById(id); + if(num==-1) + throw("ID doesn't exist"); + player.playSong(num); } - +//check if the id exist in the playlist.song and remove the id +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); + } + } +} +} // check if the id exist and add the song to player.song function addSong(title, album, artist, duration, id) { - // your code here -} + let temp = duration.split(":"); + let min = temp[0]*60; + 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; + } +player.songs.forEach(song => { + if(song.id == id) + throw "exception"; +}); +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) { - // 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"; + } +} //check if the id exsist and add new playlist to the playlists. +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; +}// get a playlist id and play the song in the playlist. +function playPlaylist(id) { + 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"; + } } +//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); -function createPlaylist(name, id) { - // your code here + } + 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 a playlist and returns the sum of the duration of all the songs in the playlist. +function playlistDuration(id) { + let countDuration = 0; + let playlistIndex; + for (let i = 0; i< player.playlists.length; i++) { + if(player.playlists[i].id == id) + playlistIndex = i; + } -function playPlaylist(id) { - // your code here + 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; + } } - -function editPlaylist(playlistId, songId) { - // your code here +return countDuration; } +//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:[], + 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; + }); -function playlistDuration(id) { - // your code here -} + 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; + }); -function searchByQuery(query) { - // your code here -} + return obj; +} +//function that get a duration and check which playlist's/song's duration is closest to the duration picked. 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 returns a new unused id. +function uniqueId(){ +let id =1; +player.playlists.forEach(playlist => { + if (playlist.id>id) { + id = playlist.id; + + } + +}); +return id+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; + } + throw ("non-existent song ID"); +} +// 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 = { @@ -105,3 +305,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" } }