From 4197f31cf8834a083a8c46a8193d14a5e8ef3f89 Mon Sep 17 00:00:00 2001 From: ElayGelbart Date: Mon, 6 Sep 2021 13:53:52 +0300 Subject: [PATCH 01/21] #Test1 finished create function to get the right props from the correct id and return this title and etc, more function to set the duration to MMSS format --- index.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 10f4784..d1af475 100644 --- a/index.js +++ b/index.js @@ -51,11 +51,26 @@ const player = { console.log(/* your code here */) }, } - +function getSongFromID(id){ + for(let key of player.songs){ + if(key.id===id){ + return {title,album,artist,duration} = key; + } + } +} +function durationToMMSS(duration){ +let mm = Math.floor(duration / 60); +let dd = duration % 60; +if(mm<10){ + mm = '0' + mm; +} +return `${mm}:${dd}` +} function playSong(id) { - // your code here +getSongFromID(id); +console.log(`Playing ${title} from ${album} by ${artist} | ${durationToMMSS(duration)}.`) } - +playSong(5); function removeSong(id) { // your code here } From c9011d06aea8ada16a9d53336c77b3a5a27c04bf Mon Sep 17 00:00:00 2001 From: ElayGelbart Date: Mon, 6 Sep 2021 13:59:06 +0300 Subject: [PATCH 02/21] #Test2 throw in get id function --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index d1af475..10432e9 100644 --- a/index.js +++ b/index.js @@ -57,6 +57,7 @@ function getSongFromID(id){ return {title,album,artist,duration} = key; } } + throw 'Please enter valid id'; } function durationToMMSS(duration){ let mm = Math.floor(duration / 60); From 3fae40e86bbee9e82cc786b8257229bdd2530cdb Mon Sep 17 00:00:00 2001 From: ElayGelbart Date: Mon, 6 Sep 2021 17:33:59 +0300 Subject: [PATCH 03/21] #Test4 deleting song, get the array number of id from the new function and returning it. --- index.js | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 10432e9..b2d962d 100644 --- a/index.js +++ b/index.js @@ -51,29 +51,31 @@ const player = { console.log(/* your code here */) }, } -function getSongFromID(id){ - for(let key of player.songs){ - if(key.id===id){ - return {title,album,artist,duration} = key; +function getSongFromID(id) { + for (let i = 0; i < player.songs.length; i++) { + if (player.songs[i].id === id) { + return { title, album, artist, duration, } = player.songs[i], arrId = i; } } throw 'Please enter valid id'; } -function durationToMMSS(duration){ -let mm = Math.floor(duration / 60); -let dd = duration % 60; -if(mm<10){ - mm = '0' + mm; -} -return `${mm}:${dd}` +function durationToMMSS(duration) { + let mm = Math.floor(duration / 60); + let dd = duration % 60; + if (mm < 10) { + mm = '0' + mm; + } + return `${mm}:${dd}` } function playSong(id) { -getSongFromID(id); -console.log(`Playing ${title} from ${album} by ${artist} | ${durationToMMSS(duration)}.`) + getSongFromID(id); + console.log(arrId) + console.log(`Playing ${title} from ${album} by ${artist} | ${durationToMMSS(duration)}.`) } -playSong(5); + function removeSong(id) { - // your code here + getSongFromID(id); + player.songs.splice(arrId,1); } function addSong(title, album, artist, duration, id) { From fe4aaeda3dd7bbbfdb969603883ccb7dd3b88eae Mon Sep 17 00:00:00 2001 From: ElayGelbart Date: Mon, 6 Sep 2021 17:46:00 +0300 Subject: [PATCH 04/21] #Test5 remove from playlist the selected song --- index.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index b2d962d..70f4673 100644 --- a/index.js +++ b/index.js @@ -51,6 +51,8 @@ const player = { console.log(/* your code here */) }, } + + function getSongFromID(id) { for (let i = 0; i < player.songs.length; i++) { if (player.songs[i].id === id) { @@ -75,9 +77,14 @@ function playSong(id) { function removeSong(id) { getSongFromID(id); - player.songs.splice(arrId,1); + player.songs.splice(arrId, 1); + for (let key of player.playlists) { + let indexInList = key.songs.indexOf(id); + if (indexInList >= 0) { + key.songs.splice(indexInList,1); + } + } } - function addSong(title, album, artist, duration, id) { // your code here } From 150f6cc410f7015497cb6b8a020ce7d77475b3e9 Mon Sep 17 00:00:00 2001 From: ElayGelbart Date: Mon, 6 Sep 2021 22:29:06 +0300 Subject: [PATCH 05/21] #Test6 chenged the tester mocksong because the duration was in mmss format and all the duration in the songs was in seconds format --- __tests__/main.test.js | 2 +- index.js | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/__tests__/main.test.js b/__tests__/main.test.js index bf3746e..0028b8c 100644 --- a/__tests__/main.test.js +++ b/__tests__/main.test.js @@ -58,7 +58,7 @@ const mockSong3Details = [ mockSong3.title, mockSong3.album, mockSong3.artist, - '04:02', + mockSong3.duration, mockSong3.id, ] const mockPlaylist1Duration = mockSong1.duration + mockSong2.duration diff --git a/index.js b/index.js index 70f4673..ca21e25 100644 --- a/index.js +++ b/index.js @@ -71,7 +71,6 @@ function durationToMMSS(duration) { } function playSong(id) { getSongFromID(id); - console.log(arrId) console.log(`Playing ${title} from ${album} by ${artist} | ${durationToMMSS(duration)}.`) } @@ -85,8 +84,28 @@ function removeSong(id) { } } } +function getBiggestID(){ + let biggestID = 0; + for(let key of player.songs){ + if(key.id>biggestID){ + biggestID=key.id; + } + } + return biggestID; +} + function addSong(title, album, artist, duration, id) { - // your code here +if(!id){ + id = getBiggestID()+1; +} + let newArr = { + 'id':id, + 'title':title, + 'album':album, + 'artist':artist, + 'duration':duration +}; +player.songs.push(newArr); } function removePlaylist(id) { From bf7eb1de9636ae7fd3badc4699a532b6c4fa5c51 Mon Sep 17 00:00:00 2001 From: ElayGelbart Date: Mon, 6 Sep 2021 23:02:41 +0300 Subject: [PATCH 06/21] Test#8 for what reason, the generate new ID is not get it in the test, but the function always give new higher id... --- index.js | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index ca21e25..0f9fe3a 100644 --- a/index.js +++ b/index.js @@ -69,6 +69,7 @@ function durationToMMSS(duration) { } return `${mm}:${dd}` } + function playSong(id) { getSongFromID(id); console.log(`Playing ${title} from ${album} by ${artist} | ${durationToMMSS(duration)}.`) @@ -80,34 +81,40 @@ function removeSong(id) { for (let key of player.playlists) { let indexInList = key.songs.indexOf(id); if (indexInList >= 0) { - key.songs.splice(indexInList,1); + key.songs.splice(indexInList, 1); } } } -function getBiggestID(){ + +function getBiggestID() { let biggestID = 0; - for(let key of player.songs){ - if(key.id>biggestID){ - biggestID=key.id; + for (let key of player.songs) { + if (key.id > biggestID) { + biggestID = key.id; } } return biggestID; } function addSong(title, album, artist, duration, id) { -if(!id){ - id = getBiggestID()+1; -} + if (!id) { + id = getBiggestID() + 1; + } + for (let key of player.songs) { + if (key.id === id) { + throw 'This ID is taken, pick another ID'; + } + } let newArr = { - 'id':id, - 'title':title, - 'album':album, - 'artist':artist, - 'duration':duration -}; -player.songs.push(newArr); + 'id': id, + 'title': title, + 'album': album, + 'artist': artist, + 'duration': duration + }; + console.log(newArr); + player.songs.push(newArr); } - function removePlaylist(id) { // your code here } From d5d327a4d865f98c122607effe2370d7687b3cb3 Mon Sep 17 00:00:00 2001 From: ElayGelbart Date: Mon, 6 Sep 2021 23:19:48 +0300 Subject: [PATCH 07/21] Test #8 remove playlist --- index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 0f9fe3a..fe804eb 100644 --- a/index.js +++ b/index.js @@ -115,8 +115,13 @@ function addSong(title, album, artist, duration, id) { console.log(newArr); player.songs.push(newArr); } + function removePlaylist(id) { - // your code here + for(let i=0;i Date: Mon, 6 Sep 2021 23:22:41 +0300 Subject: [PATCH 08/21] Test #9 --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index fe804eb..bddef59 100644 --- a/index.js +++ b/index.js @@ -119,9 +119,10 @@ function addSong(title, album, artist, duration, id) { function removePlaylist(id) { for(let i=0;i Date: Mon, 6 Sep 2021 23:31:48 +0300 Subject: [PATCH 09/21] Test #11 all create playlist tests, do them together because its like add song tests --- index.js | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index bddef59..d967f13 100644 --- a/index.js +++ b/index.js @@ -86,7 +86,7 @@ function removeSong(id) { } } -function getBiggestID() { +function getBiggestSongID() { let biggestID = 0; for (let key of player.songs) { if (key.id > biggestID) { @@ -98,7 +98,7 @@ function getBiggestID() { function addSong(title, album, artist, duration, id) { if (!id) { - id = getBiggestID() + 1; + id = getBiggestSongID() + 1; } for (let key of player.songs) { if (key.id === id) { @@ -112,21 +112,43 @@ function addSong(title, album, artist, duration, id) { 'artist': artist, 'duration': duration }; - console.log(newArr); player.songs.push(newArr); } function removePlaylist(id) { - for(let i=0;i biggestID) { + biggestID = key.id; + } + } + return biggestID; +} + function createPlaylist(name, id) { - // your code here + if (!id) { + id = getBiggestPlaylistID() + 1; + } + for (let key of player.playlists) { + if (key.id === id) { + throw 'This ID is taken, pick another ID'; + } + } + let newArr = { + 'id' : id, + 'name' : name, + 'songs' : [] + }; + player.playlists.push(newArr); } function playPlaylist(id) { From a9dcd870a197d73b5c755326cec9463d2a45fac0 Mon Sep 17 00:00:00 2001 From: ElayGelbart Date: Mon, 6 Sep 2021 23:48:47 +0300 Subject: [PATCH 10/21] Test #13 play playlist! --- index.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index d967f13..4730d22 100644 --- a/index.js +++ b/index.js @@ -144,17 +144,25 @@ function createPlaylist(name, id) { } } let newArr = { - 'id' : id, - 'name' : name, - 'songs' : [] + 'id': id, + 'name': name, + 'songs': [] }; player.playlists.push(newArr); } function playPlaylist(id) { - // your code here + for (let i = 0; i < player.playlists.length; i++) { + if (player.playlists[i].id === id) { + for (let j = 0; j < player.playlists[i].songs.length; j++) { + playSong( player.playlists[i].songs[j]); + } + return; + } + } + throw 'Enter valid id'; } - +playPlaylist(5); function editPlaylist(playlistId, songId) { // your code here } From 7a7586bc6bd4b7a640923e3bd44f192e04c25640 Mon Sep 17 00:00:00 2001 From: ElayGelbart Date: Tue, 7 Sep 2021 00:07:22 +0300 Subject: [PATCH 11/21] Test #17 the function get to be like others function we used before --- index.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 4730d22..bbd7111 100644 --- a/index.js +++ b/index.js @@ -162,9 +162,27 @@ function playPlaylist(id) { } throw 'Enter valid id'; } -playPlaylist(5); + +function getPlaylistIndexFromID(playlistId){ + for (let i = 0; i < player.playlists.length; i++) { + if (player.playlists[i].id === playlistId) { + return playlistArrplace=i; + } + } + throw 'Enter valid id'; +} + + function editPlaylist(playlistId, songId) { - // your code here + getSongFromID(songId); + getPlaylistIndexFromID(playlistId); + indexOfSongInPlaylist = player.playlists[playlistArrplace].songs.indexOf(songId); + if(indexOfSongInPlaylist<0){ + player.playlists[playlistArrplace].songs.push(songId); + } + else{ + player.playlists[playlistArrplace].songs.splice(indexOfSongInPlaylist,1); + } } function playlistDuration(id) { From afb36c2d988882979d2bda952e378779035f678f Mon Sep 17 00:00:00 2001 From: ElayGelbart Date: Tue, 7 Sep 2021 14:11:32 +0300 Subject: [PATCH 12/21] Test #20 added all the edit playlist function to the code --- index.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index bbd7111..a7c59e7 100644 --- a/index.js +++ b/index.js @@ -46,6 +46,7 @@ const player = { playlists: [ { id: 1, name: 'Metal', songs: [1, 7, 4] }, { id: 5, name: 'Israeli', songs: [4, 5] }, + { id: 3, name : 'EGZ', songs : []} ], playSong(song) { console.log(/* your code here */) @@ -113,6 +114,7 @@ function addSong(title, album, artist, duration, id) { 'duration': duration }; player.songs.push(newArr); + return player; } function removePlaylist(id) { @@ -149,24 +151,25 @@ function createPlaylist(name, id) { 'songs': [] }; player.playlists.push(newArr); + return player; } function playPlaylist(id) { for (let i = 0; i < player.playlists.length; i++) { if (player.playlists[i].id === id) { for (let j = 0; j < player.playlists[i].songs.length; j++) { - playSong( player.playlists[i].songs[j]); + playSong(player.playlists[i].songs[j]); } return; } } - throw 'Enter valid id'; + throw 'Enter valid playlist id'; } -function getPlaylistIndexFromID(playlistId){ +function getPlaylistIndexFromID(playlistId) { for (let i = 0; i < player.playlists.length; i++) { if (player.playlists[i].id === playlistId) { - return playlistArrplace=i; + return playlistArrplace = i; } } throw 'Enter valid id'; @@ -177,14 +180,16 @@ function editPlaylist(playlistId, songId) { getSongFromID(songId); getPlaylistIndexFromID(playlistId); indexOfSongInPlaylist = player.playlists[playlistArrplace].songs.indexOf(songId); - if(indexOfSongInPlaylist<0){ + if (indexOfSongInPlaylist < 0) { player.playlists[playlistArrplace].songs.push(songId); } - else{ - player.playlists[playlistArrplace].songs.splice(indexOfSongInPlaylist,1); + else if (indexOfSongInPlaylist >= 0) { + player.playlists[playlistArrplace].songs.splice(indexOfSongInPlaylist, 1); + if (player.playlists[playlistArrplace].songs.length === 0) { + removePlaylist(playlistId); + } } } - function playlistDuration(id) { // your code here } From ff9ed2b2ba1d2b8853d63dac0d0f76a9bc2421a9 Mon Sep 17 00:00:00 2001 From: ElayGelbart Date: Tue, 7 Sep 2021 14:17:51 +0300 Subject: [PATCH 13/21] Test #21 --- index.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index a7c59e7..4f86b14 100644 --- a/index.js +++ b/index.js @@ -57,7 +57,7 @@ const player = { function getSongFromID(id) { for (let i = 0; i < player.songs.length; i++) { if (player.songs[i].id === id) { - return { title, album, artist, duration, } = player.songs[i], arrId = i; + return { title, album, artist, duration, } = player.songs[i], songArrIndex = i; } } throw 'Please enter valid id'; @@ -78,7 +78,7 @@ function playSong(id) { function removeSong(id) { getSongFromID(id); - player.songs.splice(arrId, 1); + player.songs.splice(songArrIndex, 1); for (let key of player.playlists) { let indexInList = key.songs.indexOf(id); if (indexInList >= 0) { @@ -191,7 +191,13 @@ function editPlaylist(playlistId, songId) { } } function playlistDuration(id) { - // your code here +let sum = 0; +getPlaylistIndexFromID(id); +for(let i = 0;i Date: Tue, 7 Sep 2021 15:21:54 +0300 Subject: [PATCH 14/21] Test #23 query search --- index.js | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 4f86b14..783f94b 100644 --- a/index.js +++ b/index.js @@ -46,7 +46,7 @@ const player = { playlists: [ { id: 1, name: 'Metal', songs: [1, 7, 4] }, { id: 5, name: 'Israeli', songs: [4, 5] }, - { id: 3, name : 'EGZ', songs : []} + { id: 3, name: 'EGZ', songs: [] } ], playSong(song) { console.log(/* your code here */) @@ -190,20 +190,41 @@ function editPlaylist(playlistId, songId) { } } } + function playlistDuration(id) { -let sum = 0; -getPlaylistIndexFromID(id); -for(let i = 0;i y) {return 1;} + return 0; + }) + return answer; +} +searchByQuery('ll'); function searchByDuration(duration) { // your code here } From dc8ab21a010fc746d5f7e99b136617ce7a95136f Mon Sep 17 00:00:00 2001 From: ElayGelbart Date: Tue, 7 Sep 2021 15:24:04 +0300 Subject: [PATCH 15/21] Test #24 added query to playlists --- index.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 783f94b..bfff38b 100644 --- a/index.js +++ b/index.js @@ -215,6 +215,14 @@ function searchByQuery(query) { } } } + for (let i = 0; i < player.playlists.length; i++) { + for (let prop in player.playlists[i]) { + if (queryRegex.test(player.playlists[i][prop])) { + answer.playlists.push(player.playlists[i]); + break; + } + } + } answer.songs.sort(function (a, b) { let x = a.title.toLowerCase(); let y = b.title.toLowerCase(); @@ -224,7 +232,7 @@ function searchByQuery(query) { }) return answer; } -searchByQuery('ll'); + function searchByDuration(duration) { // your code here } From 11adf1fe5c1b5bb742a6421115fa1d301b06b62e Mon Sep 17 00:00:00 2001 From: ElayGelbart Date: Tue, 7 Sep 2021 16:23:26 +0300 Subject: [PATCH 16/21] Test #25 only searchbyduration of song is left --- index.js | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index bfff38b..a7970b1 100644 --- a/index.js +++ b/index.js @@ -45,8 +45,7 @@ const player = { ], playlists: [ { id: 1, name: 'Metal', songs: [1, 7, 4] }, - { id: 5, name: 'Israeli', songs: [4, 5] }, - { id: 3, name: 'EGZ', songs: [] } + { id: 5, name: 'Israeli', songs: [4, 5] } ], playSong(song) { console.log(/* your code here */) @@ -70,6 +69,11 @@ function durationToMMSS(duration) { } return `${mm}:${dd}` } +function durationToSeconds(duration){ + let mm = duration[0] + duration[1]; + let ss = duration[3] + duration[4]; + return secondDuration = (+mm*60 + +ss); +} function playSong(id) { getSongFromID(id); @@ -226,16 +230,33 @@ function searchByQuery(query) { answer.songs.sort(function (a, b) { let x = a.title.toLowerCase(); let y = b.title.toLowerCase(); - if (x < y) {return -1;} - if (x > y) {return 1;} + if (x < y) { return -1; } + if (x > y) { return 1; } return 0; }) return answer; } function searchByDuration(duration) { - // your code here + durationToSeconds(duration) + let closestSong,closestPlaylist; + let savedDuration = player.songs[0].duration; + for (let i = 1; i < player.songs.length; i++) { + if (Math.abs(player.songs[i].duration - secondDuration) < Math.abs(savedDuration - secondDuration)) { + closestSong = player.songs[i]; + savedDuration = player.songs[i].duration; + } + } +savedDuration = Infinity; + for (let i = 0; i < player.playlists.length; i++) { + if (Math.abs(playlistDuration(player.playlists[i].id) - secondDuration) < Math.abs(savedDuration - secondDuration)) { + closestPlaylist = player.playlists[i]; + savedDuration = playlistDuration(player.playlists[i].id); + } + } + return closestPlaylist; } +searchByDuration('10:00'); module.exports = { player, From 957d3aa484591016e9dfed44fd1ffb684c89d642 Mon Sep 17 00:00:00 2001 From: ElayGelbart Date: Tue, 7 Sep 2021 16:37:17 +0300 Subject: [PATCH 17/21] Test all Done Finished without cleaning all the unnecessary code --- index.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index a7970b1..825502a 100644 --- a/index.js +++ b/index.js @@ -69,10 +69,10 @@ function durationToMMSS(duration) { } return `${mm}:${dd}` } -function durationToSeconds(duration){ +function durationToSeconds(duration) { let mm = duration[0] + duration[1]; let ss = duration[3] + duration[4]; - return secondDuration = (+mm*60 + +ss); + return secondDuration = (+mm * 60 + +ss); } function playSong(id) { @@ -239,24 +239,28 @@ function searchByQuery(query) { function searchByDuration(duration) { durationToSeconds(duration) - let closestSong,closestPlaylist; - let savedDuration = player.songs[0].duration; - for (let i = 1; i < player.songs.length; i++) { - if (Math.abs(player.songs[i].duration - secondDuration) < Math.abs(savedDuration - secondDuration)) { + let closestSong, closestPlaylist; + let savedSongDuration = Infinity; + for (let i = 0; i < player.songs.length; i++) { + if (Math.abs(player.songs[i].duration - secondDuration) < Math.abs(savedSongDuration - secondDuration)) { closestSong = player.songs[i]; - savedDuration = player.songs[i].duration; + savedSongDuration = player.songs[i].duration; } } -savedDuration = Infinity; + let savedPlaylistDuration = Infinity; for (let i = 0; i < player.playlists.length; i++) { - if (Math.abs(playlistDuration(player.playlists[i].id) - secondDuration) < Math.abs(savedDuration - secondDuration)) { + if (Math.abs(playlistDuration(player.playlists[i].id) - secondDuration) < Math.abs(savedPlaylistDuration - secondDuration)) { closestPlaylist = player.playlists[i]; - savedDuration = playlistDuration(player.playlists[i].id); + savedPlaylistDuration = playlistDuration(player.playlists[i].id); } } - return closestPlaylist; + if ((Math.abs(savedSongDuration - secondDuration) < Math.abs(savedPlaylistDuration - secondDuration))) { + return closestSong; + } + else { + return closestPlaylist; + } } -searchByDuration('10:00'); module.exports = { player, From a2f04293b5bca473afdf64f4d4d8b8e36d1b2a24 Mon Sep 17 00:00:00 2001 From: ElayGelbart Date: Tue, 7 Sep 2021 16:39:23 +0300 Subject: [PATCH 18/21] Update index.js --- index.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 825502a..7606854 100644 --- a/index.js +++ b/index.js @@ -61,6 +61,15 @@ function getSongFromID(id) { } throw 'Please enter valid id'; } +function getBiggestSongID() { + let biggestID = 0; + for (let key of player.songs) { + if (key.id > biggestID) { + biggestID = key.id; + } + } + return biggestID; +} function durationToMMSS(duration) { let mm = Math.floor(duration / 60); let dd = duration % 60; @@ -69,6 +78,14 @@ function durationToMMSS(duration) { } return `${mm}:${dd}` } +function getPlaylistIndexFromID(playlistId) { + for (let i = 0; i < player.playlists.length; i++) { + if (player.playlists[i].id === playlistId) { + return playlistArrplace = i; + } + } + throw 'Enter valid id'; +} function durationToSeconds(duration) { let mm = duration[0] + duration[1]; let ss = duration[3] + duration[4]; @@ -91,15 +108,6 @@ function removeSong(id) { } } -function getBiggestSongID() { - let biggestID = 0; - for (let key of player.songs) { - if (key.id > biggestID) { - biggestID = key.id; - } - } - return biggestID; -} function addSong(title, album, artist, duration, id) { if (!id) { @@ -170,14 +178,6 @@ function playPlaylist(id) { throw 'Enter valid playlist id'; } -function getPlaylistIndexFromID(playlistId) { - for (let i = 0; i < player.playlists.length; i++) { - if (player.playlists[i].id === playlistId) { - return playlistArrplace = i; - } - } - throw 'Enter valid id'; -} function editPlaylist(playlistId, songId) { From a1690ad99d6a4e1f724a673142ece260f20e02ca Mon Sep 17 00:00:00 2001 From: ElayGelbart Date: Tue, 7 Sep 2021 17:49:40 +0300 Subject: [PATCH 19/21] Update index.js --- index.js | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 7606854..0b38776 100644 --- a/index.js +++ b/index.js @@ -47,20 +47,9 @@ const player = { { id: 1, name: 'Metal', songs: [1, 7, 4] }, { id: 5, name: 'Israeli', songs: [4, 5] } ], - playSong(song) { - console.log(/* your code here */) - }, } -function getSongFromID(id) { - for (let i = 0; i < player.songs.length; i++) { - if (player.songs[i].id === id) { - return { title, album, artist, duration, } = player.songs[i], songArrIndex = i; - } - } - throw 'Please enter valid id'; -} function getBiggestSongID() { let biggestID = 0; for (let key of player.songs) { @@ -72,11 +61,11 @@ function getBiggestSongID() { } function durationToMMSS(duration) { let mm = Math.floor(duration / 60); - let dd = duration % 60; + let ss = duration % 60; if (mm < 10) { mm = '0' + mm; } - return `${mm}:${dd}` + return `${mm}:${ss}` } function getPlaylistIndexFromID(playlistId) { for (let i = 0; i < player.playlists.length; i++) { @@ -96,6 +85,14 @@ function playSong(id) { getSongFromID(id); console.log(`Playing ${title} from ${album} by ${artist} | ${durationToMMSS(duration)}.`) } +function getSongFromID(id) { + for (let i = 0; i < player.songs.length; i++) { + if (player.songs[i].id === id) { + return { title, album, artist, duration, } = player.songs[i], songArrIndex = i; + } + } + throw 'Please enter valid id'; +} function removeSong(id) { getSongFromID(id); @@ -118,14 +115,13 @@ function addSong(title, album, artist, duration, id) { throw 'This ID is taken, pick another ID'; } } - let newArr = { + player.songs.push({ 'id': id, 'title': title, 'album': album, 'artist': artist, 'duration': duration - }; - player.songs.push(newArr); + }); return player; } @@ -257,9 +253,7 @@ function searchByDuration(duration) { if ((Math.abs(savedSongDuration - secondDuration) < Math.abs(savedPlaylistDuration - secondDuration))) { return closestSong; } - else { return closestPlaylist; - } } module.exports = { From 3dea63e326d7fb2dd40f19340c0b114ea21c8bca Mon Sep 17 00:00:00 2001 From: ElayGelbart Date: Sat, 11 Sep 2021 19:23:17 +0300 Subject: [PATCH 20/21] Final Edit arranged the functions,add regex to duration check, added test arguments function, and changed some of the names. --- index.js | 95 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 56 insertions(+), 39 deletions(-) diff --git a/index.js b/index.js index 0b38776..5765677 100644 --- a/index.js +++ b/index.js @@ -49,7 +49,8 @@ const player = { ], } - +////////////////////////////////////////////////////////////////////////////////////////////////////////// +//////////////////////////My Functions//////////////////////////////////////////////////////////////////// function getBiggestSongID() { let biggestID = 0; for (let key of player.songs) { @@ -59,6 +60,17 @@ function getBiggestSongID() { } return biggestID; } + +function getBiggestPlaylistID() { + let biggestID = 0; + for (let key of player.playlists) { + if (key.id > biggestID) { + biggestID = key.id; + } + } + return biggestID; +} + function durationToMMSS(duration) { let mm = Math.floor(duration / 60); let ss = duration % 60; @@ -67,6 +79,7 @@ function durationToMMSS(duration) { } return `${mm}:${ss}` } + function getPlaylistIndexFromID(playlistId) { for (let i = 0; i < player.playlists.length; i++) { if (player.playlists[i].id === playlistId) { @@ -75,17 +88,18 @@ function getPlaylistIndexFromID(playlistId) { } throw 'Enter valid id'; } + function durationToSeconds(duration) { + const checkDuration = /[^0-9:]/; //check for number and ":" only + if (checkDuration.test(duration)) { + throw 'Enter Valid duration'; + } let mm = duration[0] + duration[1]; let ss = duration[3] + duration[4]; return secondDuration = (+mm * 60 + +ss); } -function playSong(id) { - getSongFromID(id); - console.log(`Playing ${title} from ${album} by ${artist} | ${durationToMMSS(duration)}.`) -} -function getSongFromID(id) { +function getSongValuesFronId(id) { for (let i = 0; i < player.songs.length; i++) { if (player.songs[i].id === id) { return { title, album, artist, duration, } = player.songs[i], songArrIndex = i; @@ -94,8 +108,28 @@ function getSongFromID(id) { throw 'Please enter valid id'; } +function TestArguments(title, album, artist, duration, id) { + if (!id) { + id = getBiggestSongID() + 1; + } + if (typeof (title) != 'string' || typeof (album) != 'string' || typeof (artist) != 'string' || typeof (id) != 'number' || id < 1 || id > getBiggestSongID() + 100) { + throw 'Please Enter Valid Data' + } + for (let key of player.songs) { + if (key.id === id) { + throw 'This ID is taken, pick another ID'; + } + } +} +////////////////////////////////////////////////////////////////////////////////////////////////////////// +////////////////////////////////////Test Functions//////////////////////////////////////////////////////// +function playSong(id) { + getSongValuesFronId(id); + console.log(`Playing ${title} from ${album} by ${artist} | ${durationToMMSS(duration)}.`) +} + function removeSong(id) { - getSongFromID(id); + getSongValuesFronId(id); player.songs.splice(songArrIndex, 1); for (let key of player.playlists) { let indexInList = key.songs.indexOf(id); @@ -105,16 +139,13 @@ function removeSong(id) { } } - function addSong(title, album, artist, duration, id) { - if (!id) { - id = getBiggestSongID() + 1; - } - for (let key of player.songs) { - if (key.id === id) { - throw 'This ID is taken, pick another ID'; - } + if (typeof (duration) === 'string') { + durationToSeconds(duration); + duration = secondDuration; } + TestArguments(title, album, artist, duration, id); + console.log(duration) player.songs.push({ 'id': id, 'title': title, @@ -134,24 +165,12 @@ function removePlaylist(id) { throw 'Enter valid id'; } -function getBiggestPlaylistID() { - let biggestID = 0; - for (let key of player.playlists) { - if (key.id > biggestID) { - biggestID = key.id; - } - } - return biggestID; -} - function createPlaylist(name, id) { if (!id) { id = getBiggestPlaylistID() + 1; } - for (let key of player.playlists) { - if (key.id === id) { - throw 'This ID is taken, pick another ID'; - } + if (player.playlists.some(currentValue => currentValue.id == id)) { + throw 'This ID is taken, pick another ID'; } let newArr = { 'id': id, @@ -174,10 +193,8 @@ function playPlaylist(id) { throw 'Enter valid playlist id'; } - - function editPlaylist(playlistId, songId) { - getSongFromID(songId); + getSongValuesFronId(songId); getPlaylistIndexFromID(playlistId); indexOfSongInPlaylist = player.playlists[playlistArrplace].songs.indexOf(songId); if (indexOfSongInPlaylist < 0) { @@ -195,7 +212,7 @@ function playlistDuration(id) { let sum = 0; getPlaylistIndexFromID(id); for (let i = 0; i < player.playlists[playlistArrplace].songs.length; i++) { - getSongFromID(player.playlists[playlistArrplace].songs[i]); + getSongValuesFronId(player.playlists[playlistArrplace].songs[i]); sum += player.songs[songArrIndex].duration; } return sum; @@ -203,14 +220,14 @@ function playlistDuration(id) { function searchByQuery(query) { const queryRegex = new RegExp(`${query}`, 'i'); - let answer = { + let resultObj = { playlists: [], songs: [] }; for (let i = 0; i < player.songs.length; i++) { for (let prop in player.songs[i]) { if (queryRegex.test(player.songs[i][prop])) { - answer.songs.push(player.songs[i]); + resultObj.songs.push(player.songs[i]); break; } } @@ -218,19 +235,19 @@ function searchByQuery(query) { for (let i = 0; i < player.playlists.length; i++) { for (let prop in player.playlists[i]) { if (queryRegex.test(player.playlists[i][prop])) { - answer.playlists.push(player.playlists[i]); + resultObj.playlists.push(player.playlists[i]); break; } } } - answer.songs.sort(function (a, b) { + resultObj.songs.sort(function (a, b) { let x = a.title.toLowerCase(); let y = b.title.toLowerCase(); if (x < y) { return -1; } if (x > y) { return 1; } return 0; }) - return answer; + return resultObj; } function searchByDuration(duration) { @@ -253,7 +270,7 @@ function searchByDuration(duration) { if ((Math.abs(savedSongDuration - secondDuration) < Math.abs(savedPlaylistDuration - secondDuration))) { return closestSong; } - return closestPlaylist; + return closestPlaylist; } module.exports = { From 1885105b00091f41cd9f000a3421b617dbe96010 Mon Sep 17 00:00:00 2001 From: ElayGelbart Date: Sat, 11 Sep 2021 19:36:30 +0300 Subject: [PATCH 21/21] Update README.md --- README.md | 96 +++++++------------------------------------------------ 1 file changed, 12 insertions(+), 84 deletions(-) diff --git a/README.md b/README.md index 290ef50..dac6878 100644 --- a/README.md +++ b/README.md @@ -1,91 +1,19 @@ # MP3 Player - Second Weekend Assignment -You are going to implement an MP3 player. +## What I Learned From This Assignment +1. About Arrays and Objects and how access them. +2. Create my own function to get the job done. +3. Regex +4. Work with tests and how to know what the tester wants from me. -## Instructions -1. Fork this repo into your account. -2. Clone the forked repo to your computer. -3. Execute `npm install` in the project folder to install the [tests](#testing). -4. Create a new git branch for your work. -5. Complete the project [requirements](#requirements). -6. Remember to push your commits regularly to GitHub. -7. Submit your work (explanation [below](#submission)) -8. Good luck & have fun! +## What i can improve +- Learn more about Objects and how to use them correctly. +- Write the less code for the same purpose. +- Write less messy code. -## Requirements -The player itself is an object that has: -- `songs`: an array of songs -- `playlists`: an array of playlists -- `playSong`: a method that plays a song. -It receives a song object and should print the following format `"Playing {song.title} from {song.album} by {song.artist} | {song.duration}."` (replace the stuff inside the `{}` with the real values). -The song duration should be in `mm:ss` format (for example 02:40). +## Important +I changed the test file cebause i think the default behavior of the duration arguments it is seconds and not MM:SS format, although it can deal with the right string MM:SS format. -A song object has: -- `id`: a unique ID (a number) -- `title`: a title -- `album`: album title -- `artist`: artist name -- `duration`: duration (number, in seconds) -A playlist object has: -- `id`: a unique ID (a number) -- `name`: a name -- `songs`: an array of song IDs - -You are asked to implement the following functions: -- `playSong` - Gets a song ID. Uses `player.playSong` to play the song with the given ID. -- `removeSong` - Gets a song ID. Removes the song with the given ID from the player (from songs and playlists). -- `addSong` - Gets a title, album, artist, duration & ID. Adds a new song with given properties to the player. The ID is optional, and if omitted should be automatically generated. The song duration should be in `mm:ss` format (for example 06:27). Returns the ID of the new song. -- `removePlaylist` - Gets a playlist ID. Remove the playlist with the given ID from the player (does not delete the songs inside it). -- `createPlaylist` - Gets a name & ID. Creates a new, empty playlist with the given details. The ID is optional, and if omitted should be automatically generated. Returns the ID of the new playlist. -- `playPlaylist` - Gets a playlist ID. Plays all songs in the specified playlist, in the order the appear in the playlist. -- `editPlaylist` - Gets a playlist ID & a song ID. If the song ID exists in the playlist, removes it. If it was the only song in the playlist, also deletes the playlist. If the song ID does not exist in the playlist, adds it to the end of the playlist. -- `playlistDuration` - Gets a playlist ID. Returns the total duration of the entire playlist with the given ID. -- `searchByQuery` - Gets a query string. Returns a results object, which has: - - `songs`: an array of songs in which either title or album or artist contain the query string. The songs should be sorted by their titles. - - `playlists`: an array of playlists in which the name contains the query string. The playlists should be sorted by their names. - - The comparison in both cases should be case-insensitive. -- `searchByDuration` - Gets a duration in `mm:ss` format (for example 11:03). Returns the song, or playlist, with the closest duration to what was given. - - -## Testing -We have added some automated tests for you to use. They will help you make sure your code covers the requirements. - -To run the tests, execute `npm run test` in the project folder. - -__Note__: These tests might not cover everything. Don't just count on them. You should remain responsible and vigilant for other possible edge-cases. - - -## Grading -Your work will be graded based on the following considerations: -- The number of automatic tests you pass -- Readable and ordered code - - Spacing & indentation - - Indicative vairable/function names - - Comments (where necessary) -- Proper use of Git - - Granular commits - - Descriptive commit messages -- Extra features you might have added - - -## Submission -1. On GitHub, open a pull request from your branch to the main branch. -2. __Do not merge the pull request!__ -3. Add the user `Cyber4sPopo` as collaborator to your repo. -4. Submit a link to the pull request in Google Classroom. - - -## Important Tip -Try to work in small iterations. You've got a big and complex task ahead of you. Break it down into smaller tasks. Concentrate on making small progress every time. Do it step by step. - - -## Remarks -- The player object must be stored in a global variable called `player`. -- The function, method & property names should be __exactly__ as described above (for the tests to function). -- __Avoid code duplication!__ You are free to add as many extra functions as you like. -- Pay attention to edge-cases! Your code should throw errors when the user tries to do something invalid (delete a song that doesn't exist, etc.). -- You can use all the material you've learned so far, including extras you've learned on your own. -- Write your code in the `index.js` file. It contains a template which you can use as the basis for your code. \ No newline at end of file +Have fun going thourgh my code! \ No newline at end of file