diff --git a/README.md b/README.md index 290ef50..56457fa 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ +This is my 2nd Weekend Assignment in the Cyber4S program: +the task is the following: + + # MP3 Player - Second Weekend Assignment You are going to implement an MP3 player. diff --git a/index.js b/index.js index 10f4784..cb50790 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,5 @@ +const { pipelinePrimaryTopicReference } = require("@babel/types"); + const player = { songs: [ { @@ -48,48 +50,257 @@ 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 playSong(id) { - // your code here +function durationConvert(duration) // converts duration value to mm/ss +{ + + if(typeof(duration)!=="number") //if duration is not a number + { + throw "not a suitable number"; + } + 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) +{ + 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); //generates a random id to the song + while(id === GetsongById(id)) // if the genrated 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}; //create new song object + 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) //if the id already exists throw an error + { + throw "this is an existing ID"; + } + if(id===undefined) + { + id= Math.floor(Math.random()*100); //generates a random id to the playlist + while(id === GetPlaylistById(id)) // if the genrated 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()