diff --git a/index.js b/index.js index 10f4784..89fbb4f 100644 --- a/index.js +++ b/index.js @@ -48,50 +48,257 @@ 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)}.`) }, } + +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("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[i].id;} + } + newID=max+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 newID; } + +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) { - // 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 + let Pindex=getPlaylistByID(id); + for (let i = 0; i < Pindex.songs.length; i++) { + playSong (Pindex.songs[i]); + } } + +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 + let allDuration=0; + let playlist=getPlaylistByID(id) + for(let i=0;i b.title){ + return 1; } + return 0; + }) + return obj; } 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