From b415407c822b1ef1beae0d667c82e7b0ef3606f9 Mon Sep 17 00:00:00 2001 From: ronco1812 Date: Sat, 11 Sep 2021 12:09:50 +0300 Subject: [PATCH 1/9] First Task add duration convert changed line 51 and added a duration convertor function --- .vscode/launch.json | 15 +++++++++++++++ index.js | 17 ++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 .vscode/launch.json 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..c6a3433 100644 --- a/index.js +++ b/index.js @@ -48,9 +48,24 @@ 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) // converts duration value to mm/ss +{ + let durationArr=[]; + while(duration>=1) + { + if(durationArr.length===2) + { + durationArr.unshift(":"); + } + durationArr.unshift(duration%10); + duration=Math.floor(duration/10); + } + durationArr.unshift(0); + return durationArr.join(""); +} function playSong(id) { // your code here From 7aecfc6336fe7aaa290f14ca162452882e68e769 Mon Sep 17 00:00:00 2001 From: ronco1812 Date: Sat, 11 Sep 2021 12:14:57 +0300 Subject: [PATCH 2/9] added playSong func and an error thrower --- index.js | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index c6a3433..c70ed27 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,5 @@ +const { pipelinePrimaryTopicReference } = require("@babel/types"); + const player = { songs: [ { @@ -53,22 +55,36 @@ const player = { } function durationConvert(duration) // converts duration value to mm/ss { - let durationArr=[]; - while(duration>=1) - { - if(durationArr.length===2) + + if(duration>1000 || duration<=99) //if duration is not a suitable number { - durationArr.unshift(":"); + + throw "not a suitable number"; // an error leading to catch } - durationArr.unshift(duration%10); - duration=Math.floor(duration/10); - } - durationArr.unshift(0); - return durationArr.join(""); + else + { + let durationArr=[]; + while(duration>=1) + { + if(durationArr.length===2) + { + durationArr.unshift(":"); + } + durationArr.unshift(duration%10); + duration=Math.floor(duration/10); + } + durationArr.unshift(0); + return durationArr.join(""); } function playSong(id) { - // your code here + + let songObj= player.songs.find(x=> x["id"]===id); + if(songObj===undefined) // if id is not found in the player an error will be thrown + { + throw "Not a Valid ID" + } + return player.playSong(songObj) } function removeSong(id) { From 82b38ec53ebf4189027cdb4d70a5ddd79670e8b2 Mon Sep 17 00:00:00 2001 From: ronco1812 Date: Sat, 11 Sep 2021 12:20:55 +0300 Subject: [PATCH 3/9] added the remove song --- index.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index c70ed27..027595c 100644 --- a/index.js +++ b/index.js @@ -50,7 +50,7 @@ const player = { { id: 5, name: 'Israeli', songs: [4, 5] }, ], playSong(song) { - console.log("Playing "+song.title+ " from " +song.album+" by "+song.artist+"|"+durationConvert(song.duration)+"."); + console.log("Playing "+song.title+ " from " +song.album+" by "+song.artist+" | "+durationConvert(song.duration)+"."); }, } function durationConvert(duration) // converts duration value to mm/ss @@ -76,19 +76,62 @@ function durationConvert(duration) // converts duration value to mm/ss durationArr.unshift(0); return durationArr.join(""); } +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 playSong(id) { + 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; + } - let songObj= player.songs.find(x=> x["id"]===id); - if(songObj===undefined) // if id is not found in the player an error will be thrown +function playSong(id) { { - throw "Not a Valid 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" + } + return player.playSong(songObj); } - return 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) { From be7744a6086a8e8b67ef2470a04673e0320e654d Mon Sep 17 00:00:00 2001 From: ronco1812 Date: Sat, 11 Sep 2021 12:25:13 +0300 Subject: [PATCH 4/9] added the add song func --- index.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 027595c..62d83e9 100644 --- a/index.js +++ b/index.js @@ -50,7 +50,7 @@ const player = { { id: 5, name: 'Israeli', songs: [4, 5] }, ], playSong(song) { - console.log("Playing "+song.title+ " from " +song.album+" by "+song.artist+" | "+durationConvert(song.duration)+"."); + return ("Playing "+song.title+ " from " +song.album+" by "+song.artist+" | "+durationConvert(song.duration)+"."); }, } function durationConvert(duration) // converts duration value to mm/ss @@ -106,7 +106,7 @@ function playSong(id) { { throw "Not a Valid ID" } - return player.playSong(songObj); + console.log((player.playSong(songObj))); } } @@ -135,7 +135,22 @@ function removeSong(id) { } function addSong(title, album, artist, duration, id) { - // your code here + 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); + } + if(GetsongById(id) !==undefined) //if the id already exists throw an error + { + throw "this is an existing ID"; + } + } + let newDuration =durationConvert(duration); + const newSong = {id,title, album ,artist, duration:newDuration}; //create new song object + player.songs.push(newSong) + return newSong["id"]; } function removePlaylist(id) { From 06a57aff07a861e69f58c73ed85abcbba17f4b3f Mon Sep 17 00:00:00 2001 From: ronco1812 Date: Sat, 11 Sep 2021 12:27:30 +0300 Subject: [PATCH 5/9] added the remove playlist func --- index.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 62d83e9..e703ede 100644 --- a/index.js +++ b/index.js @@ -152,9 +152,24 @@ function addSong(title, album, artist, duration, id) { 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) { From 640c2ae7607713cc10b081355889501258bd79aa Mon Sep 17 00:00:00 2001 From: ronco1812 Date: Sat, 11 Sep 2021 13:37:56 +0300 Subject: [PATCH 6/9] added the edit playlist func and some changes to the duration convert func --- index.js | 90 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 67 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index e703ede..730c5d9 100644 --- a/index.js +++ b/index.js @@ -56,25 +56,22 @@ const player = { function durationConvert(duration) // converts duration value to mm/ss { - if(duration>1000 || duration<=99) //if duration is not a suitable number + if(typeof(duration)!=="number") //if duration is not a suitable number { throw "not a suitable number"; // an error leading to catch } else { - let durationArr=[]; - while(duration>=1) - { - if(durationArr.length===2) - { - durationArr.unshift(":"); - } - durationArr.unshift(duration%10); - duration=Math.floor(duration/10); - } - durationArr.unshift(0); - return durationArr.join(""); + 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 { @@ -135,6 +132,10 @@ function removeSong(id) { } function addSong(title, album, artist, duration, id) { + 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 @@ -142,13 +143,8 @@ function addSong(title, album, artist, duration, id) { { id= Math.floor(Math.random()*100); } - if(GetsongById(id) !==undefined) //if the id already exists throw an error - { - throw "this is an existing ID"; - } - } - let newDuration =durationConvert(duration); - const newSong = {id,title, album ,artist, duration:newDuration}; //create new song object + 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"]; } @@ -173,15 +169,63 @@ function removePlaylist(id) { } 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 Date: Sat, 11 Sep 2021 13:39:30 +0300 Subject: [PATCH 7/9] added the playlist duration func --- index.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 730c5d9..94f3db4 100644 --- a/index.js +++ b/index.js @@ -229,7 +229,15 @@ else{ } function playlistDuration(id) { - // your code here + let sum=0; +const playlistSongs=GetPlaylistById(id)["songs"]; //indicates songs array +for(let i of playlistSongs) //goes through all song id in array +{ + let songduration= GetsongById(i)["duration"]; //gets the songs duration + sum+=songduration; +} + + return sum; } function searchByQuery(query) { From 5113f50c5e9fec07f6f1d19228169e406c3a0e63 Mon Sep 17 00:00:00 2001 From: ronco1812 Date: Sat, 11 Sep 2021 13:47:00 +0300 Subject: [PATCH 8/9] added search by query func --- index.js | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 94f3db4..e8bfd51 100644 --- a/index.js +++ b/index.js @@ -72,7 +72,7 @@ function durationConvert(duration) // converts duration value to mm/ss 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); @@ -205,8 +205,8 @@ function editPlaylist(playlistId, songId) { } const playli= GetPlaylistById(playlistId) //a shortcut for later in the function const playliSongs= GetPlaylistById(playlistId)["songs"]; // the songs array in playlists -const song=GetsongById(songId)["id"]; //the song id in songs id -if(!playliSongs.includes(song)) //if song is not in playlist +const song=GetsongById(songId)["id"]; //finds the song id in songsId +if(!playliSongs.includes(song)) //if song doesnt exist in playlist { playliSongs.push(song); } @@ -231,7 +231,7 @@ else{ function playlistDuration(id) { let sum=0; const playlistSongs=GetPlaylistById(id)["songs"]; //indicates songs array -for(let i of playlistSongs) //goes through all song id in array +for(let i of playlistSongs) //search song id in array { let songduration= GetsongById(i)["duration"]; //gets the songs duration sum+=songduration; @@ -241,7 +241,29 @@ for(let i of playlistSongs) //goes through all song id in array } function searchByQuery(query) { - // your code here + const results={songs:[], playlists:[]}; + let query2=query.toLowerCase(); //be case insensitive + for(let i of player.songs) //go through all the songs and see if the query contains the different keys + { + if(query2.includes(i["album"].toLowerCase())||query2.includes(i["artist"].toLowerCase()) ||query2.includes(i["title"].toLowerCase())) + { + results.songs.push(i); + results.songs.sort((a,b)=> {if(a["title"].toLowerCase() {if(a["name"].toLowerCase() Date: Sat, 11 Sep 2021 14:33:29 +0300 Subject: [PATCH 9/9] add the search by duration --- index.js | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index e8bfd51..bb10b62 100644 --- a/index.js +++ b/index.js @@ -245,7 +245,7 @@ function searchByQuery(query) { let query2=query.toLowerCase(); //be case insensitive for(let i of player.songs) //go through all the songs and see if the query contains the different keys { - if(query2.includes(i["album"].toLowerCase())||query2.includes(i["artist"].toLowerCase()) ||query2.includes(i["title"].toLowerCase())) + if(i["album"].toLowerCase().includes(query2)||i["artist"].toLowerCase().includes(query2) || i["title"].toLowerCase().includes(query2)) { results.songs.push(i); results.songs.sort((a,b)=> {if(a["title"].toLowerCase() {if(a["name"].toLowerCase()