From 7a54febdfca11ac822e3c7ae512e359c98608789 Mon Sep 17 00:00:00 2001 From: DanielShaked <89588973+DanielShaked@users.noreply.github.com> Date: Thu, 9 Sep 2021 16:38:56 +0300 Subject: [PATCH 01/11] playSong function I created the function playSong In addition I created 2 helo functions: 1. secToMmSs - format mm:ss 2. songById - find song index by the id. --- index.js | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 10f4784..6cfad1b 100644 --- a/index.js +++ b/index.js @@ -48,12 +48,13 @@ 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} | ${secToMmSs(song.duration)}.`) }, } function playSong(id) { - // your code here + + return player.playSong(songById(id)); } function removeSong(id) { @@ -105,3 +106,29 @@ module.exports = { searchByQuery, searchByDuration, } + +// help functions + +function songById(id) { + for (let i = 0; i < player.songs.length; i++) { + if (player.songs[i].id == id) { + return player.songs[i] + } + throw new Error("There is no song with such an Id"); + } +} + + +function secToMmSs(duration) { + let min = Math.floor(duration / 60); + let sec = duration - (min * 60); + + if (min < 10) { + min = "0" + String(min); + } + if (sec < 10) { + sec = "0" + String(sec); + } + + return min + ':' + sec +} From 802abeddad8637258007a09f67a3df5d422ada50 Mon Sep 17 00:00:00 2001 From: DanielShaked <89588973+DanielShaked@users.noreply.github.com> Date: Thu, 9 Sep 2021 19:32:18 +0300 Subject: [PATCH 02/11] removeSong Function Create a remove function from the player and playlist --- index.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 6cfad1b..1f8810c 100644 --- a/index.js +++ b/index.js @@ -58,7 +58,13 @@ function playSong(id) { } function removeSong(id) { - // your code here + let indexSong = player.songs.indexOf(songById(id)) + player.songs.splice(indexSong, 1); + + for (let i = 0; i < player.playlists.length;i++) + player.playlists[i].songs.splice(i, 1); + + } function addSong(title, album, artist, duration, id) { @@ -132,3 +138,11 @@ function secToMmSs(duration) { return min + ':' + sec } + + +function getIndexPl(id) { + for (let i = 0; i < player.playlists.length; i++) { + if (player.playlists[i].id == id) + return player.playlists[i]; + } +} \ No newline at end of file From 6fb160a62774269277664bab1c11496d8ea4a033 Mon Sep 17 00:00:00 2001 From: DanielShaked <89588973+DanielShaked@users.noreply.github.com> Date: Thu, 9 Sep 2021 22:25:13 +0300 Subject: [PATCH 03/11] addSong Function addSong function. also create mmToSec function, and generateid function --- index.js | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 1f8810c..496dbdc 100644 --- a/index.js +++ b/index.js @@ -60,17 +60,28 @@ function playSong(id) { function removeSong(id) { let indexSong = player.songs.indexOf(songById(id)) player.songs.splice(indexSong, 1); - - for (let i = 0; i < player.playlists.length;i++) - player.playlists[i].songs.splice(i, 1); + + for (let i = 0; i < player.playlists.length; i++) + player.playlists[i].songs.splice(i, 1); } function addSong(title, album, artist, duration, id) { - // your code here + if (checkIdAvilable(id) == false) throw ("This ID is in use!"); + if (id == undefined) id = generateId(id); + + player.songs.push({ + title: title, + album: album, + artist: artist, + duration: mmToSec(duration), + id: id + }) + return id } + function removePlaylist(id) { // your code here } @@ -145,4 +156,30 @@ function getIndexPl(id) { if (player.playlists[i].id == id) return player.playlists[i]; } +} + +function checkIdAvilable(id) { + for (let i = 0; i < player.songs.length; i++) { + if (player.songs[i].id != id) { + return true; + } + return false; + } +} + +function mmToSec(duration) { + let get = duration.split(':'); + let min = parseInt(get[0]) * 60; + let sec = parseInt(get[1]); + let totalSec = min + sec; + return totalSec; +} + +function generateId() { + id = Math.floor(Math.random() * 1000) + 1; + for (let i = 0; i < player.songs.length; i++) { + if (id !== player.songs[i].id) { + return id; + } + } } \ No newline at end of file From 01e6236ca195939e9a440e0e8ce1e2bc0af3fdae Mon Sep 17 00:00:00 2001 From: DanielShaked <89588973+DanielShaked@users.noreply.github.com> Date: Thu, 9 Sep 2021 22:38:18 +0300 Subject: [PATCH 04/11] removePlaylist Function create removePlaylist; also add throw new on getIndexPl --- index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 496dbdc..b45b692 100644 --- a/index.js +++ b/index.js @@ -63,8 +63,6 @@ function removeSong(id) { for (let i = 0; i < player.playlists.length; i++) player.playlists[i].songs.splice(i, 1); - - } function addSong(title, album, artist, duration, id) { @@ -83,7 +81,8 @@ function addSong(title, album, artist, duration, id) { function removePlaylist(id) { - // your code here + let indexPl = player.playlists.indexOf(getIndexPl(id)) + player.playlists.splice(indexPl, 1) } function createPlaylist(name, id) { @@ -156,6 +155,8 @@ function getIndexPl(id) { if (player.playlists[i].id == id) return player.playlists[i]; } + throw new Error("No playlist eith that ID!"); + } function checkIdAvilable(id) { From 6257deae301139461e6f9b6909b1adef9b6c343f Mon Sep 17 00:00:00 2001 From: DanielShaked <89588973+DanielShaked@users.noreply.github.com> Date: Fri, 10 Sep 2021 07:53:04 +0300 Subject: [PATCH 05/11] createPlaylist Function create a createPlaylist; also create a checkIdAvilablePl Function --- index.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index b45b692..d752c82 100644 --- a/index.js +++ b/index.js @@ -86,9 +86,18 @@ function removePlaylist(id) { } function createPlaylist(name, id) { - // your code here + if (checkIdAvilablePl(id) == false) throw ("This ID is in use!"); + if (id == undefined) id = generateId(id); + player.playlists.push({ + id: id, + name: name, + songs: [] + }) + return id; } + + function playPlaylist(id) { // your code here } @@ -166,8 +175,17 @@ function checkIdAvilable(id) { } return false; } + } +function checkIdAvilablePl(id) { + for (let i = 0; i < player.playlists.length; i++) { + if (player.playlists[i].id != id) { + return true; + } + return false; + } +} function mmToSec(duration) { let get = duration.split(':'); let min = parseInt(get[0]) * 60; From b0e1156809b37d3833db2266dc01571fcf927bd5 Mon Sep 17 00:00:00 2001 From: DanielShaked <89588973+DanielShaked@users.noreply.github.com> Date: Fri, 10 Sep 2021 12:31:42 +0300 Subject: [PATCH 06/11] playPlaylist Function create a playPlaylist Function, also do some visual changes and fixes --- index.js | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index d752c82..43e2154 100644 --- a/index.js +++ b/index.js @@ -99,7 +99,21 @@ function createPlaylist(name, id) { function playPlaylist(id) { - // your code here + for (let i of player.playlists) { + if (i.id === id) { + for (let j of i.songs) { + for (let i in player.songs) { + if (player.songs[i].id === j) { + player.playSong(player.songs[i]) + } + } + } + return + } + else { + throw 'have problem!'; + } + } } function editPlaylist(playlistId, songId) { @@ -139,11 +153,23 @@ function songById(id) { if (player.songs[i].id == id) { return player.songs[i] } - throw new Error("There is no song with such an Id"); + throw ("There is no song with such an Id"); } } +function getIndexPl(id) { + for (let i = 0; i < player.playlists.length; i++) { + if (player.playlists[i].id == id) + return player.playlists[i]; + } + throw ("No playlist with that ID!"); + +} + + + + function secToMmSs(duration) { let min = Math.floor(duration / 60); let sec = duration - (min * 60); @@ -159,15 +185,6 @@ function secToMmSs(duration) { } -function getIndexPl(id) { - for (let i = 0; i < player.playlists.length; i++) { - if (player.playlists[i].id == id) - return player.playlists[i]; - } - throw new Error("No playlist eith that ID!"); - -} - function checkIdAvilable(id) { for (let i = 0; i < player.songs.length; i++) { if (player.songs[i].id != id) { @@ -201,4 +218,5 @@ function generateId() { return id; } } -} \ No newline at end of file +} + From ae19115feb29fe0b9151300cecfb81b707c1fbb4 Mon Sep 17 00:00:00 2001 From: DanielShaked <89588973+DanielShaked@users.noreply.github.com> Date: Fri, 10 Sep 2021 18:40:26 +0300 Subject: [PATCH 07/11] editPlaylist Function create a editPlaylist function. also fixes some bugs. feel goood ;) --- index.js | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 43e2154..be90524 100644 --- a/index.js +++ b/index.js @@ -111,15 +111,28 @@ function playPlaylist(id) { return } else { - throw 'have problem!'; + throw new Error('There is a problem'); } } } function editPlaylist(playlistId, songId) { - // your code here + songById(songId); + let playlist = getIndexPl(playlistId); + let index = player.playlists.indexOf(playlist); + for (let i = 0; i < playlist.songs.length; i++) { + if (playlist.songs[i] === songId) { + player.playlists[index].songs.splice(i, 1) + if (player.playlists[index].songs.length === 0) { + removePlaylist(playlistId); + } + } else { + player.playlists[index].songs.push(songId); + } + } } + function playlistDuration(id) { // your code here } @@ -132,19 +145,6 @@ function searchByDuration(duration) { // your code here } -module.exports = { - player, - playSong, - removeSong, - addSong, - removePlaylist, - createPlaylist, - playPlaylist, - editPlaylist, - playlistDuration, - searchByQuery, - searchByDuration, -} // help functions @@ -153,8 +153,8 @@ function songById(id) { if (player.songs[i].id == id) { return player.songs[i] } - throw ("There is no song with such an Id"); } + throw new Error("There is no song with such an Id"); } @@ -220,3 +220,16 @@ function generateId() { } } +module.exports = { + player, + playSong, + removeSong, + addSong, + removePlaylist, + createPlaylist, + playPlaylist, + editPlaylist, + playlistDuration, + searchByQuery, + searchByDuration, +} From eb733ca887d1739e94b8291f7eaa08fbd8c3aa5f Mon Sep 17 00:00:00 2001 From: DanielShaked <89588973+DanielShaked@users.noreply.github.com> Date: Sat, 11 Sep 2021 10:26:54 +0300 Subject: [PATCH 08/11] playlistDuration Function create a new Function - playlistDuration. --- index.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index be90524..780f5eb 100644 --- a/index.js +++ b/index.js @@ -134,7 +134,14 @@ function editPlaylist(playlistId, songId) { function playlistDuration(id) { - // your code here + let sumDuration = 0; + let playlist = getIndexPl(id); + + for (let i = 0; i < playlist.songs.length; i++) { + sumDuration += songById(playlist.songs[i]).duration; + + } + return sumDuration; } function searchByQuery(query) { From 158d47c21b3669c74816a19ff3ac481a1b06477b Mon Sep 17 00:00:00 2001 From: DanielShaked <89588973+DanielShaked@users.noreply.github.com> Date: Sat, 11 Sep 2021 13:02:03 +0300 Subject: [PATCH 09/11] searchByQuery Function create searchByQuery Function only. --- index.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 780f5eb..2871088 100644 --- a/index.js +++ b/index.js @@ -145,9 +145,32 @@ function playlistDuration(id) { } function searchByQuery(query) { - // your code here + let caseIn = query.toLowerCase(); + let returnObj = { songs: [], playlists: [] } + + for (let i of player.songs) { + if (i.title.toLowerCase().includes(caseIn) || + i.album.toLowerCase().includes(caseIn) || + i.artist.toLowerCase().includes(caseIn)) { + returnObj.songs.push(i); + } + } + for (let j of player.playlists) { + if (j.name.toLowerCase().includes(caseIn)) { + returnObj.playlists.push(j); + } + //sorting songs & playlists + returnObj.songs.sort((a, b) => { if (a.title < b.title) return -1 }); + returnObj.playlists.sort((a, b) => { if (a.name < b.name) return -1 }); + return returnObj; + } } + + + + + function searchByDuration(duration) { // your code here } From 6ac789b8c5d14cb234441fe70f1e28359c9e3f22 Mon Sep 17 00:00:00 2001 From: DanielShaked <89588973+DanielShaked@users.noreply.github.com> Date: Sat, 11 Sep 2021 19:37:51 +0300 Subject: [PATCH 10/11] searchByDuration Function create searchByDuration. also: 1. Format code 2. fix bugs 3. Improving visibility --- index.js | 153 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 99 insertions(+), 54 deletions(-) diff --git a/index.js b/index.js index 2871088..566f10d 100644 --- a/index.js +++ b/index.js @@ -1,51 +1,57 @@ const player = { - songs: [ - { - id: 1, - title: 'Vortex', - album: 'Wallflowers', - artist: 'Jinjer', - duration: 242, - }, - { - id: 2, - title: 'Vinda', - album: 'Godtfolk', - artist: 'Songleikr', - duration: 160, - }, - { - id: 7, - title: 'Shiroyama', - album: 'The Last Stand', - artist: 'Sabaton', - duration: 213, - }, - { - id: 3, - title: 'Thunderstruck', - album: 'The Razors Edge', - artist: 'AC/DC', - duration: 292, - }, - { - id: 4, - title: 'All is One', - album: 'All is One', - artist: 'Orphaned Land', - duration: 270, - }, - { - id: 5, - title: 'As a Stone', - album: 'Show Us What You Got', - artist: 'Full Trunk', - duration: 259, - }, + songs: [{ + id: 1, + title: 'Vortex', + album: 'Wallflowers', + artist: 'Jinjer', + duration: 242, + }, + { + id: 2, + title: 'Vinda', + album: 'Godtfolk', + artist: 'Songleikr', + duration: 160, + }, + { + id: 7, + title: 'Shiroyama', + album: 'The Last Stand', + artist: 'Sabaton', + duration: 213, + }, + { + id: 3, + title: 'Thunderstruck', + album: 'The Razors Edge', + artist: 'AC/DC', + duration: 292, + }, + { + id: 4, + title: 'All is One', + album: 'All is One', + artist: 'Orphaned Land', + duration: 270, + }, + { + id: 5, + title: 'As a Stone', + album: 'Show Us What You Got', + artist: 'Full Trunk', + duration: 259, + }, ], - playlists: [ - { id: 1, name: 'Metal', songs: [1, 7, 4] }, - { id: 5, name: 'Israeli', songs: [4, 5] }, + playlists: [{ + id: 1, + name: 'Metal', + songs: [1, 7, 4] + }, + { + id: 5, + name: 'Israeli', + songs: [4, 5] + }, ], playSong(song) { console.log(`Playing ${song.title} from ${song.album} by ${song.artist} | ${secToMmSs(song.duration)}.`) @@ -109,8 +115,7 @@ function playPlaylist(id) { } } return - } - else { + } else { throw new Error('There is a problem'); } } @@ -146,7 +151,10 @@ function playlistDuration(id) { function searchByQuery(query) { let caseIn = query.toLowerCase(); - let returnObj = { songs: [], playlists: [] } + let returnObj = { + songs: [], + playlists: [] + } for (let i of player.songs) { if (i.title.toLowerCase().includes(caseIn) || @@ -160,8 +168,12 @@ function searchByQuery(query) { returnObj.playlists.push(j); } //sorting songs & playlists - returnObj.songs.sort((a, b) => { if (a.title < b.title) return -1 }); - returnObj.playlists.sort((a, b) => { if (a.name < b.name) return -1 }); + returnObj.songs.sort((a, b) => { + if (a.title < b.title) return -1 + }); + returnObj.playlists.sort((a, b) => { + if (a.name < b.name) return -1 + }); return returnObj; } } @@ -170,12 +182,44 @@ function searchByQuery(query) { - function searchByDuration(duration) { - // your code here + let convertedToSec = mmToSec(duration) + let checkMinSong = Math.abs(player.songs[0].duration - convertedToSec); + let checkMinPl = Math.abs(player.playlists[0].id - convertedToSec); + let songIn = 0; + let plIn = 0; + + + + for (let i = 0; i < player.songs.length; i++) { + if (Math.abs(player.songs[i].duration - convertedToSec) < checkMinSong) { + songIn = i; + checkMinSong = Math.abs(player.songs[i].duration - convertedToSec); + + } + } + for (let j = 0; j < player.playlists.length; j++) { + let playlistduration = playlistDuration(player.playlists[j].id); + if (Math.abs(playlistduration - convertedToSec) < checkMinPl) { + plIn = j; + checkMinPl = Math.abs(playlistduration - convertedToSec); + + } + } + if (checkMinSong < checkMinPl) { + return player.songs[songIn]; + console.log(`the closest song's duration to what you requested is ${player.songs[songIn]}.`) + } else { + return player.playlists[plIn]; + console.log(`the closest Playlist's duration to what you requested is ${player.playlists[plIn]}.`) + + } + } + + // help functions function songById(id) { @@ -233,6 +277,7 @@ function checkIdAvilablePl(id) { return false; } } + function mmToSec(duration) { let get = duration.split(':'); let min = parseInt(get[0]) * 60; @@ -262,4 +307,4 @@ module.exports = { playlistDuration, searchByQuery, searchByDuration, -} +} \ No newline at end of file From 7c5f4895839fa7048e761d4fe1c002420230a0c7 Mon Sep 17 00:00:00 2001 From: DanielShaked <89588973+DanielShaked@users.noreply.github.com> Date: Sun, 12 Sep 2021 11:44:38 +0300 Subject: [PATCH 11/11] Push commit ---> Actions --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 566f10d..f8aea11 100644 --- a/index.js +++ b/index.js @@ -220,7 +220,7 @@ function searchByDuration(duration) { -// help functions +// help functions: function songById(id) { for (let i = 0; i < player.songs.length; i++) {