From 08bcbafb378aff45e66f9a4553d17b20fc3fced7 Mon Sep 17 00:00:00 2001 From: Or Rosman <42673555+orrosman@users.noreply.github.com> Date: Mon, 6 Sep 2021 16:27:35 +0300 Subject: [PATCH 01/15] Added playSong, getSongById and convertDuration fuctions --- __tests__/main.test.js | 4 ++-- index.js | 30 ++++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/__tests__/main.test.js b/__tests__/main.test.js index bf3746e..2f99af9 100644 --- a/__tests__/main.test.js +++ b/__tests__/main.test.js @@ -73,7 +73,7 @@ describe('Player Tests', () => { jest.clearAllMocks() }) - it('playSong should console.log in the correct format', () => { + it.only('playSong should console.log in the correct format', () => { const spy = jest.spyOn(console, 'log') playSong(mockSong1.id) expect(spy).toHaveBeenCalledWith( @@ -81,7 +81,7 @@ describe('Player Tests', () => { ) }) - it('playSong should throw for non-existent ID', () => { + it.only('playSong should throw for non-existent ID', () => { expect(() => playSong(mockNonExistentSongId)).toThrow() }) diff --git a/index.js b/index.js index 10f4784..06472f6 100644 --- a/index.js +++ b/index.js @@ -48,12 +48,37 @@ 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)} | ${convertDuration(song.duration)}.`) }, } +function convertDuration(duration) { + let min = Math.floor(duration / 60); + let sec = duration % 60; + + if(min < 10){ + min = "0" + String(min); + } + if (sec < 10) { + sec = "0" + String(sec); + } + + return min+':'+sec +} + +function getSongById(id){ + for (let i = 0; i < player.songs.length; i++) { + if(player.songs[i].id == id) + return player.songs[i] + } + + throw new Error("No such ID"); +} + function playSong(id) { - // your code here + let song = getSongById(id); + + return player.playSong(song); } function removeSong(id) { @@ -92,6 +117,7 @@ function searchByDuration(duration) { // your code here } + module.exports = { player, playSong, From 1c0aed90f4ed284b7f932b05421e7dfa9a48f236 Mon Sep 17 00:00:00 2001 From: Or Rosman <42673555+orrosman@users.noreply.github.com> Date: Mon, 6 Sep 2021 17:39:30 +0300 Subject: [PATCH 02/15] Added removeSong function --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index b512c09..82fdcf5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +__tests__ \ No newline at end of file From 140f38f4ef191c6c0e5c384173f2b273b15b971f Mon Sep 17 00:00:00 2001 From: Or Rosman <42673555+orrosman@users.noreply.github.com> Date: Mon, 6 Sep 2021 17:40:07 +0300 Subject: [PATCH 03/15] Added removeSong function --- __tests__/main.test.js | 4 ++-- index.js | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/__tests__/main.test.js b/__tests__/main.test.js index 2f99af9..bf3746e 100644 --- a/__tests__/main.test.js +++ b/__tests__/main.test.js @@ -73,7 +73,7 @@ describe('Player Tests', () => { jest.clearAllMocks() }) - it.only('playSong should console.log in the correct format', () => { + it('playSong should console.log in the correct format', () => { const spy = jest.spyOn(console, 'log') playSong(mockSong1.id) expect(spy).toHaveBeenCalledWith( @@ -81,7 +81,7 @@ describe('Player Tests', () => { ) }) - it.only('playSong should throw for non-existent ID', () => { + it('playSong should throw for non-existent ID', () => { expect(() => playSong(mockNonExistentSongId)).toThrow() }) diff --git a/index.js b/index.js index 06472f6..bbe9aa8 100644 --- a/index.js +++ b/index.js @@ -82,7 +82,18 @@ function playSong(id) { } function removeSong(id) { - // your code here + //remove sng from songs list + let songIndex = player.songs.indexOf(getSongById(id)) + player.songs.splice(songIndex,1); + + //remove song from all playlists + for(let i=0; i Date: Tue, 7 Sep 2021 14:55:30 +0300 Subject: [PATCH 04/15] Added addSong and convertToSeconds functions --- index.js | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index bbe9aa8..0a49a3d 100644 --- a/index.js +++ b/index.js @@ -66,6 +66,14 @@ function convertDuration(duration) { return min+':'+sec } +function convertToSeconds(duration){ + let arr = duration.split(":") + let min = parseInt(arr[0]) * 60 + let sec = parseInt(arr[1]) + + return min+sec +} + function getSongById(id){ for (let i = 0; i < player.songs.length; i++) { if(player.songs[i].id == id) @@ -75,6 +83,14 @@ function getSongById(id){ throw new Error("No such ID"); } +function idExist(id) { + for (let i = 0; i < player.songs.length; i++) { + if (player.songs[i].id == id) + return true + } + return false +} + function playSong(id) { let song = getSongById(id); @@ -96,8 +112,18 @@ function removeSong(id) { } } -function addSong(title, album, artist, duration, id) { - // your code here +function addSong(title, album, artist, duration, id = Math.floor(Math.random() * 1000) + 1) { + if(!idExist(id)){ + player.songs.push({id: id, + title: title, + album: album, + artist: artist, + duration: convertToSeconds(duration)}) + return id + } + else{ + throw new Error("This ID already exists") + } } function removePlaylist(id) { From 78d1a2828877dd4265a6955b55e69eaed59dbec0 Mon Sep 17 00:00:00 2001 From: Or Rosman <42673555+orrosman@users.noreply.github.com> Date: Tue, 7 Sep 2021 15:06:16 +0300 Subject: [PATCH 05/15] Added removePlaylist and getPlaylistById functions --- index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 0a49a3d..ae79ea5 100644 --- a/index.js +++ b/index.js @@ -82,6 +82,14 @@ function getSongById(id){ throw new Error("No such ID"); } +function getPlaylistById(id) { + for (let i = 0; i < player.playlists.length; i++) { + if (player.playlists[i].id == id) + return player.playlists[i] + } + + throw new Error("No such ID"); +} function idExist(id) { for (let i = 0; i < player.songs.length; i++) { @@ -127,7 +135,8 @@ function addSong(title, album, artist, duration, id = Math.floor(Math.random() * } function removePlaylist(id) { - // your code here + let playlistIndex = player.playlists.indexOf(getPlaylistById(id)) + player.playlists.splice(playlistIndex, 1); } function createPlaylist(name, id) { From 58e1b8b588ed9aa8f26cdc0d05541ca7fad02931 Mon Sep 17 00:00:00 2001 From: Or Rosman <42673555+orrosman@users.noreply.github.com> Date: Tue, 7 Sep 2021 15:16:13 +0300 Subject: [PATCH 06/15] Added createPlaylist and playlistIdExist functions --- index.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index ae79ea5..53ddbb6 100644 --- a/index.js +++ b/index.js @@ -82,6 +82,7 @@ function getSongById(id){ throw new Error("No such ID"); } + function getPlaylistById(id) { for (let i = 0; i < player.playlists.length; i++) { if (player.playlists[i].id == id) @@ -91,13 +92,20 @@ function getPlaylistById(id) { throw new Error("No such ID"); } -function idExist(id) { +function songIdExist(id) { for (let i = 0; i < player.songs.length; i++) { if (player.songs[i].id == id) return true } return false } +function playlistIdExist(id) { + for (let i = 0; i < player.playlists.length; i++) { + if (player.playlists[i].id == id) + return true + } + return false +} function playSong(id) { let song = getSongById(id); @@ -121,7 +129,7 @@ function removeSong(id) { } function addSong(title, album, artist, duration, id = Math.floor(Math.random() * 1000) + 1) { - if(!idExist(id)){ + if(!songIdExist(id)){ player.songs.push({id: id, title: title, album: album, @@ -139,8 +147,16 @@ function removePlaylist(id) { player.playlists.splice(playlistIndex, 1); } -function createPlaylist(name, id) { - // your code here +function createPlaylist(name, id = Math.floor(Math.random() * 1000) + 1) { + if (!playlistIdExist(id)){ + player.playlists.push({id: id, + name: name, + songs:[]}); + return id; + } + else{ + throw new Error("This ID already exists") + } } function playPlaylist(id) { From 07c7d9a9f497904962dd016ba03f7305732d9bef Mon Sep 17 00:00:00 2001 From: Or Rosman <42673555+orrosman@users.noreply.github.com> Date: Tue, 7 Sep 2021 15:19:47 +0300 Subject: [PATCH 07/15] Added playPlaylist function --- index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 53ddbb6..7b07cda 100644 --- a/index.js +++ b/index.js @@ -160,7 +160,10 @@ function createPlaylist(name, id = Math.floor(Math.random() * 1000) + 1) { } function playPlaylist(id) { - // your code here + let playlist = getPlaylistById(id); + for(let i=0; i Date: Tue, 7 Sep 2021 16:04:30 +0300 Subject: [PATCH 08/15] Added editPlaylist and findSongInPlaylist functions --- index.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 7b07cda..41442ad 100644 --- a/index.js +++ b/index.js @@ -166,8 +166,33 @@ function playPlaylist(id) { } } +function findSongInPlaylist(songId, playlistId){ + let playlist = getPlaylistById(playlistId) + for (let index = 0; index < playlist.songs.length; index++) { + if(playlist.songs[index] == songId) + return index + } + return -1 +} + function editPlaylist(playlistId, songId) { - // your code here + let songIndex = findSongInPlaylist(songId, playlistId) + let playlist = getPlaylistById(playlistId) + + if(songIdExist(songId) && playlistIdExist(playlistId)){ //checks if both song and playlist exist + if(songIndex == -1) { //checks if the song is not in the list + playlist.songs.push(songId); + } + else if(playlist.songs.length > 1){ //checks if the song is not the only song in the playlist + playlist.songs.splice(songIndex,1) + } + else{ + removePlaylist(playlist.id) + } + } + else{ + throw new Error("No such ID"); + } } function playlistDuration(id) { From c85a1bf28dd6cc113cb82187e35e8ec54af93931 Mon Sep 17 00:00:00 2001 From: Or Rosman <42673555+orrosman@users.noreply.github.com> Date: Tue, 7 Sep 2021 16:07:54 +0300 Subject: [PATCH 09/15] Added playlistDuration function --- index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 41442ad..1accfc2 100644 --- a/index.js +++ b/index.js @@ -196,7 +196,13 @@ function editPlaylist(playlistId, songId) { } function playlistDuration(id) { - // your code here + let playlist = getPlaylistById(id) + let sum = 0 + + for(let i=0; i Date: Tue, 7 Sep 2021 17:20:43 +0300 Subject: [PATCH 10/15] Added searchByQuery function --- index.js | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 1accfc2..555454f 100644 --- a/index.js +++ b/index.js @@ -206,7 +206,42 @@ function playlistDuration(id) { } function searchByQuery(query) { - // your code here + let results={ + songs :[], + playlists : [] + } + + //search for songs containing query + for(let song of player.songs){ + if(song.title.includes(query) || song.album.includes(query) || song.artist.includes(query)){ + results.songs.push(song) + } + } + //sort songs array by title + results.songs.sort(function (a, b) { + if (a.title < b.title){ + return -1; } + else if (a.title > b.title){ + return 1; } + return 0; + }) + + //search for songs containing query + for(let playlist of player.playlists){ + if(playlist.name.includes(query)){ + results.playlists.push(playlist) + } + } + //sort playlist array by name + results.playlists.sort(function (a, b) { + if (a.name < b.name){ + return -1; } + if (a.name > b.name){ + return 1; } + return 0; + }) + + return results } function searchByDuration(duration) { From 159ef80f80d67ba950f49260aa29f002545e684b Mon Sep 17 00:00:00 2001 From: Or Rosman <42673555+orrosman@users.noreply.github.com> Date: Tue, 7 Sep 2021 18:22:11 +0300 Subject: [PATCH 11/15] Added searchByDuration function --- index.js | 158 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 93 insertions(+), 65 deletions(-) diff --git a/index.js b/index.js index 555454f..7f027e7 100644 --- a/index.js +++ b/index.js @@ -52,61 +52,6 @@ const player = { }, } -function convertDuration(duration) { - let min = Math.floor(duration / 60); - let sec = duration % 60; - - if(min < 10){ - min = "0" + String(min); - } - if (sec < 10) { - sec = "0" + String(sec); - } - - return min+':'+sec -} - -function convertToSeconds(duration){ - let arr = duration.split(":") - let min = parseInt(arr[0]) * 60 - let sec = parseInt(arr[1]) - - return min+sec -} - -function getSongById(id){ - for (let i = 0; i < player.songs.length; i++) { - if(player.songs[i].id == id) - return player.songs[i] - } - - throw new Error("No such ID"); -} - -function getPlaylistById(id) { - for (let i = 0; i < player.playlists.length; i++) { - if (player.playlists[i].id == id) - return player.playlists[i] - } - - throw new Error("No such ID"); -} - -function songIdExist(id) { - for (let i = 0; i < player.songs.length; i++) { - if (player.songs[i].id == id) - return true - } - return false -} -function playlistIdExist(id) { - for (let i = 0; i < player.playlists.length; i++) { - if (player.playlists[i].id == id) - return true - } - return false -} - function playSong(id) { let song = getSongById(id); @@ -166,15 +111,6 @@ function playPlaylist(id) { } } -function findSongInPlaylist(songId, playlistId){ - let playlist = getPlaylistById(playlistId) - for (let index = 0; index < playlist.songs.length; index++) { - if(playlist.songs[index] == songId) - return index - } - return -1 -} - function editPlaylist(playlistId, songId) { let songIndex = findSongInPlaylist(songId, playlistId) let playlist = getPlaylistById(playlistId) @@ -245,7 +181,99 @@ function searchByQuery(query) { } function searchByDuration(duration) { - // your code here + let time = convertToSeconds(duration); + let closestObj; + let deltaTime; + + //set delta time according to bigger delta + if (time > player.songs[0].duration){ + deltaTime = time; + } + else{ + deltaTime = player.songs[0].duration; + } + + //search in songs + for(let song of player.songs){ + if(Math.abs(time - song.duration) < deltaTime){ + closestObj = song; + deltaTime = Math.abs(time - song.duration); + } + } + + //search in playlists + for (let playlist of player.playlists) { + if (Math.abs(time - playlistDuration(playlist.id)) < deltaTime) { + closestObj = playlist; + deltaTime = Math.abs(time - playlistDuration(playlist.id)); + } + } + + return closestObj +} + +function convertDuration(duration) { + let min = Math.floor(duration / 60); + let sec = duration % 60; + + if (min < 10) { + min = "0" + String(min); + } + if (sec < 10) { + sec = "0" + String(sec); + } + + return min + ':' + sec +} + +function convertToSeconds(duration) { + let arr = duration.split(":") + let min = parseInt(arr[0]) * 60 + let sec = parseInt(arr[1]) + + return min + sec +} + +function getSongById(id) { + for (let i = 0; i < player.songs.length; i++) { + if (player.songs[i].id == id) + return player.songs[i] + } + + throw new Error("No such ID"); +} + +function getPlaylistById(id) { + for (let i = 0; i < player.playlists.length; i++) { + if (player.playlists[i].id == id) + return player.playlists[i] + } + + throw new Error("No such ID"); +} + +function songIdExist(id) { + for (let i = 0; i < player.songs.length; i++) { + if (player.songs[i].id == id) + return true + } + return false +} +function playlistIdExist(id) { + for (let i = 0; i < player.playlists.length; i++) { + if (player.playlists[i].id == id) + return true + } + return false +} + +function findSongInPlaylist(songId, playlistId) { + let playlist = getPlaylistById(playlistId) + for (let index = 0; index < playlist.songs.length; index++) { + if (playlist.songs[index] == songId) + return index + } + return -1 } From e708ab5d87c405124d927b0cc6851c559410dd75 Mon Sep 17 00:00:00 2001 From: Or Rosman <42673555+orrosman@users.noreply.github.com> Date: Tue, 7 Sep 2021 21:21:22 +0300 Subject: [PATCH 12/15] Added createPlaylistByArtist bonus function --- .vscode/settings.json | 10 ++++++++++ index.js | 9 +++++++++ 2 files changed, 19 insertions(+) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0b92e5b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,10 @@ +{ + "cSpell.words": [ + "Godtfolk", + "Jinjer", + "Sabaton", + "Shiroyama", + "Songleikr", + "Vinda" + ] +} \ No newline at end of file diff --git a/index.js b/index.js index 7f027e7..783031c 100644 --- a/index.js +++ b/index.js @@ -276,6 +276,15 @@ function findSongInPlaylist(songId, playlistId) { return -1 } +function createPlaylistByArtist(artist){ + let playlist = getPlaylistById(createPlaylist(`${artist} Playlist`)) + for(let song of player.songs){ + if(song.artist == artist){ + playlist.songs.push(song) + } + } + return playlist +} module.exports = { player, From 1c290d38b28eb8a9cb8c185ea1bb6d6c6a7d408b Mon Sep 17 00:00:00 2001 From: Or Rosman <42673555+orrosman@users.noreply.github.com> Date: Tue, 7 Sep 2021 21:27:10 +0300 Subject: [PATCH 13/15] Added repeatSong bonus function --- index.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/index.js b/index.js index 783031c..9add1c7 100644 --- a/index.js +++ b/index.js @@ -286,6 +286,12 @@ function createPlaylistByArtist(artist){ return playlist } +function repeatSong(repeats,songId){ + for(let i=1; i<=repeats; i++){ + playSong(songId) + } +} + module.exports = { player, playSong, From dbbdb759b23dfdc0bbd63f39683418e675ecd560 Mon Sep 17 00:00:00 2001 From: Or Rosman <42673555+orrosman@users.noreply.github.com> Date: Tue, 7 Sep 2021 21:31:07 +0300 Subject: [PATCH 14/15] Added repeatPlaylist bonus function --- index.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/index.js b/index.js index 9add1c7..dcda93b 100644 --- a/index.js +++ b/index.js @@ -292,6 +292,12 @@ function repeatSong(repeats,songId){ } } +function repeatPlaylist(repeats,playlistId){ + for(let i=1; i<=repeats; i++){ + playPlaylist(playlistId) + } +} + module.exports = { player, playSong, From 693f508e2fdc6fe86a6e83547e60425daf434e49 Mon Sep 17 00:00:00 2001 From: Or Rosman <42673555+orrosman@users.noreply.github.com> Date: Sun, 12 Sep 2021 17:50:42 +0300 Subject: [PATCH 15/15] fixing workdlow --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index dcda93b..d6d35d1 100644 --- a/index.js +++ b/index.js @@ -259,6 +259,7 @@ function songIdExist(id) { } return false } + function playlistIdExist(id) { for (let i = 0; i < player.playlists.length; i++) { if (player.playlists[i].id == id)