diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..7a9dfa0 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "pwa-chrome", + "request": "launch", + "name": "Launch Chrome against localhost", + "url": "http://localhost:8080", + "webRoot": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/index.js b/index.js index 10f4784..bb10b62 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,5 @@ +const { pipelinePrimaryTopicReference } = require("@babel/types"); + const player = { songs: [ { @@ -48,48 +50,255 @@ const player = { { id: 5, name: 'Israeli', songs: [4, 5] }, ], playSong(song) { - console.log(/* your code here */) + return ("Playing "+song.title+ " from " +song.album+" by "+song.artist+" | "+durationConvert(song.duration)+"."); }, } +function durationConvert(duration) // converts duration value to mm/ss +{ + + if(typeof(duration)!=="number") //if duration is not a suitable number + { + + throw "not a suitable number"; // an error leading to catch + } + else + { + let min = ""; + if (Math.floor(duration/60)>=10) min = `${Math.floor(duration/60)}`; + if (Math.floor(duration/60)>=1 && Math.floor(duration/60)<10) min = `0${Math.floor(duration/60)}`; + if (Math.floor(duration/60)==0) min = "00"; + let sec = ""; + if ((duration%60)>=10) sec = `${duration%60}`; + if ((duration%60)>=1 && (duration%60)<10) sec = `0${duration%60}`; + if ((duration%60)==0) sec = `00`; + return min+":"+sec; +}} +function GetsongById(id) //return song object by id +{ + let songObj= player.songs.find(x=> x["id"]===id); + return songObj; +} +function GetSongIndexById(id) //get index of song in songs array +{ + let songIndex= player.songs.indexOf(GetsongById(id)); + return songIndex; +} + function GetsongfromplaylistBysongId(id) //return playlist songs object by id- not used! + { + let songObj= player.playlists.find(x=>x["songs"].find(d=> d===id)===id); + return songObj; + } + + function FilterfromPlaylistByID(id) //filters the array from the song - not used! + { + let songObj= GetsongfromplaylistBysongId(id); + let indexPlaySong=songObj["songs"].filter(x=> x!=id); + return indexPlaySong; + } function playSong(id) { - // your code here + { + let songObj= GetsongById(id); + if(songObj===undefined) // if id is not found in the player an error will be thrown + { + throw "Not a Valid ID" + } + console.log((player.playSong(songObj))); + } } function removeSong(id) { - // your code here + if(GetsongById(id)===undefined) + { + throw "invalid ID"; + } + else{ + let songIndex= GetSongIndexById(id); //get index of song in songs array + player.songs.splice(songIndex,1); // removed song from songs array + // let songObj= GetsongfromplaylistBysongId(id); //get object of song from playlist by id + // let filteredPlayilst= FilterfromPlaylistByID(id); // filtered array of the songs in playlist + // songObj["songs"]=filteredPlayilst; + for(let i of player.playlists) //filter playlist from songs with id + { + for(let j=0;j< i.songs.length; j++) + { + if(i.songs[j]===id){ + i.songs.splice(j,1); + } + } + } + return player; + } } function addSong(title, album, artist, duration, id) { - // your code here + if(GetsongById(id) !==undefined) //if the id already exists throw an error + { + throw "this is an existing ID"; + } + if(id===undefined) + { + id= Math.floor(Math.random()*100); //a random id to the song + while(id === GetsongById(id)) // if the id exists generate a new one + { + id= Math.floor(Math.random()*100); + } + duration = parseInt(duration.slice(0,Math.floor(duration.length/2)))*60+parseInt(duration.slice(Math.ceil(duration.length/2))) + const newSong = {id,title, album ,artist, duration}; + player.songs.push(newSong) + return newSong["id"]; +} +function GetPlaylistById(id) //return playlist object by id +{ + let playObj= player.playlists.find(x=> x["id"]===id); + return playObj; } function removePlaylist(id) { - // your code here + if(GetPlaylistById(id)===undefined) + { + throw "playlist ID not defined" + } + for(let i=0; i< player.playlists.length; i++) + { + if(player.playlists[i]["id"]===id) + { + player.playlists.splice(i,1); + } + } } function createPlaylist(name, id) { - // your code here + if(GetPlaylistById(id) !==undefined) // id already exists throw an error + { + throw "this is an existing ID"; + } + if(id===undefined) + { + id= Math.floor(Math.random()*100); //create a random id to the playlist + while(id === GetPlaylistById(id)) // if the id exists generate a new one + { + id= Math.floor(Math.random()*100); + } + } + const newPlaylist = {id, name, songs:[]}; //create new playlist object + player.playlists.push(newPlaylist); + return newPlaylist["id"]; } function playPlaylist(id) { - // your code here + if(GetPlaylistById(id)===undefined) + { + throw "playlist id doesn't exist"; + } + const playlistObj=GetPlaylistById(id); + for(let i=0; i {if(a["title"].toLowerCase() {if(a["name"].toLowerCase()