From 590315be79d5fecc05af6c1258e217cf054ebd55 Mon Sep 17 00:00:00 2001 From: Omer Naveh <89572395+OmerNaveh@users.noreply.github.com> Date: Sun, 5 Sep 2021 17:12:25 +0300 Subject: [PATCH 01/13] Opened a new branch and updated the .md file Added personal description to .md file --- README.md | 4 ++++ 1 file changed, 4 insertions(+) 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. From fb427af01b3c18893b244d8ebda408e6bc47de03 Mon Sep 17 00:00:00 2001 From: Omer Naveh <89572395+OmerNaveh@users.noreply.github.com> Date: Sun, 5 Sep 2021 17:59:00 +0300 Subject: [PATCH 02/13] Added objects function + durationConvert function The object's funtion now logs according to the Request output. durationConvert function which converts duration value to mm/ss --- index.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 10f4784..e24ddb9 100644 --- a/index.js +++ b/index.js @@ -48,10 +48,26 @@ 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 833bbd793478eca2a4e759122b32fa7d0b38cc74 Mon Sep 17 00:00:00 2001 From: Omer Naveh <89572395+OmerNaveh@users.noreply.github.com> Date: Sun, 5 Sep 2021 18:59:50 +0300 Subject: [PATCH 03/13] playSong function added + added error throws completed the function playSong which recieved an id and activates the object's function. added throws which return errors in unsuitable inputs. --- index.js | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index e24ddb9..8a218a6 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,5 @@ +const { pipelinePrimaryTopicReference } = require("@babel/types"); + const player = { songs: [ { @@ -54,22 +56,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 + } + 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(""); } - durationArr.unshift(duration%10); - duration=Math.floor(duration/10); - } - durationArr.unshift(0); - return durationArr.join(""); } - -function playSong(id) { - // your code here + +function playSong(id) +{ + 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 b0900e4e72724fd446cb93929d400880786d366b Mon Sep 17 00:00:00 2001 From: Omer Naveh <89572395+OmerNaveh@users.noreply.github.com> Date: Sun, 5 Sep 2021 21:19:45 +0300 Subject: [PATCH 04/13] function removeSong completed --- index.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 8a218a6..7bc82f5 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)+"."); }, } @@ -77,10 +77,32 @@ function durationConvert(duration) // converts duration value to mm/ss return durationArr.join(""); } } - -function playSong(id) + +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" @@ -89,7 +111,27 @@ function playSong(id) } 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 88dbd6737f5c49035b8f3c79e4549810a758a36c Mon Sep 17 00:00:00 2001 From: Omer Naveh <89572395+OmerNaveh@users.noreply.github.com> Date: Mon, 6 Sep 2021 12:39:59 +0300 Subject: [PATCH 05/13] addSong function added + fixed durationConvert --- index.js | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 7bc82f5..2699a7a 100644 --- a/index.js +++ b/index.js @@ -50,31 +50,28 @@ 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 { - if(duration>1000 || duration<=99) //if duration is not a suitable number + if(typeof(duration)!=="number") //if duration is not a number { - throw "not a suitable number"; // an error leading to catch + throw "not a suitable number"; } 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; } } @@ -107,9 +104,8 @@ function playSong(id) { throw "Not a Valid ID" } - return player.playSong(songObj); + console.log((player.playSong(songObj))); } - function removeSong(id) { if(GetsongById(id)===undefined) { @@ -135,7 +131,23 @@ function removeSong(id) { } function addSong(title, album, artist, duration, id) { - // your code here + + 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); + } + 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 b7e5dc3b04bc694eaf62691b0268e73e5a3b7967 Mon Sep 17 00:00:00 2001 From: Omer Naveh <89572395+OmerNaveh@users.noreply.github.com> Date: Mon, 6 Sep 2021 13:07:49 +0300 Subject: [PATCH 06/13] removePlaylist function added --- index.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 2699a7a..286b0e7 100644 --- a/index.js +++ b/index.js @@ -150,8 +150,24 @@ function addSong(title, album, artist, duration, id) { 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 b3316c476ca80f276347e37bfb6dbfe067f9933e Mon Sep 17 00:00:00 2001 From: Omer Naveh <89572395+OmerNaveh@users.noreply.github.com> Date: Mon, 6 Sep 2021 13:29:04 +0300 Subject: [PATCH 07/13] CreatePlaylist function added + fixed addSong function --- index.js | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 286b0e7..064fec6 100644 --- a/index.js +++ b/index.js @@ -131,7 +131,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); //generates a random id to the song @@ -139,13 +142,9 @@ 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}; //create new song object player.songs.push(newSong) return newSong["id"]; } @@ -171,7 +170,21 @@ function removePlaylist(id) { } 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) { From 159fde549d28697dbeee000f150be81f40cb900a Mon Sep 17 00:00:00 2001 From: Omer Naveh <89572395+OmerNaveh@users.noreply.github.com> Date: Mon, 6 Sep 2021 14:24:09 +0300 Subject: [PATCH 08/13] playPlaylist function added --- index.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 064fec6..d903977 100644 --- a/index.js +++ b/index.js @@ -188,9 +188,16 @@ function createPlaylist(name, 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: Mon, 6 Sep 2021 16:27:15 +0300 Subject: [PATCH 09/13] editPlaylist function Added --- index.js | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d903977..d2f38ed 100644 --- a/index.js +++ b/index.js @@ -80,11 +80,13 @@ 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); @@ -97,6 +99,7 @@ function GetSongIndexById(id) //get index of song in songs array let indexPlaySong=songObj["songs"].filter(x=> x!=id); return indexPlaySong; } + function playSong(id) { let songObj= GetsongById(id); @@ -106,6 +109,7 @@ function playSong(id) } console.log((player.playSong(songObj))); } + function removeSong(id) { if(GetsongById(id)===undefined) { @@ -198,8 +202,35 @@ function playPlaylist(id) { playSong(playlistObj["songs"][i]); } } + function editPlaylist(playlistId, songId) { - // your code here + if(GetPlaylistById(playlistId)===undefined || GetsongById(songId) ===undefined) //checks if the id exists + { + throw "the given id doesn't exist"; + } +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 +{ + playliSongs.push(song); +} +else{ + for(let i=0; i Date: Mon, 6 Sep 2021 17:28:51 +0300 Subject: [PATCH 10/13] playlistDuration function added --- index.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d2f38ed..c3e1984 100644 --- a/index.js +++ b/index.js @@ -234,7 +234,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 3d2b2940fde73127e898ca2e6a8fe4142b94eaa9 Mon Sep 17 00:00:00 2001 From: Omer Naveh <89572395+OmerNaveh@users.noreply.github.com> Date: Mon, 6 Sep 2021 18:24:07 +0300 Subject: [PATCH 11/13] searchByQuery almost complete 2 tests still not passed on this function: caseInsesitive + sort alphanumericaly --- index.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index c3e1984..6b8c564 100644 --- a/index.js +++ b/index.js @@ -246,9 +246,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: Mon, 6 Sep 2021 19:04:18 +0300 Subject: [PATCH 12/13] SearchByDuration Function added all tests passed besides searchbyQuery-need to fix 2 tests! --- index.js | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 6b8c564..462fe16 100644 --- a/index.js +++ b/index.js @@ -257,7 +257,7 @@ function searchByQuery(query) { } } - for(let j of player.playlists) //go throu all playlists + for(let j of player.playlists) //go through all playlists { if(query2.includes(j["name"].toLowerCase())) { @@ -270,7 +270,38 @@ function searchByQuery(query) { console.log(searchByQuery("full trunk, israeli, metal, all is one, thunderstruck")) function searchByDuration(duration) { - // your code here + let durationConverted = parseInt(duration.slice(0,Math.floor(duration.length/2)))*60+parseInt(duration.slice(Math.ceil(duration.length/2))); //duration string converted to a number + let closestsong=100000; + let closestplaylist=10000; + let closestIndexSong=0; + let closestIndexPlaylist=0; + const songs= player.songs; + const playlist= player.playlists; + for(let i =0; i Date: Mon, 6 Sep 2021 23:01:42 +0300 Subject: [PATCH 13/13] corrected searchByQuery- All tests Passed All tests Passed --- index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 462fe16..cb50790 100644 --- a/index.js +++ b/index.js @@ -250,7 +250,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()