From ffe281c89c3dca67a2481cf3628487f90553fe09 Mon Sep 17 00:00:00 2001 From: shacharmalka1 Date: Mon, 6 Sep 2021 13:14:19 +0300 Subject: [PATCH 01/15] added isIdExist function+removesong function isIdExist is checking if some id is exist in the array of songs. removeSongs is removeing the id from the array songs and playlist --- index.js | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 10f4784..33b9ef3 100644 --- a/index.js +++ b/index.js @@ -48,16 +48,50 @@ 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] + + '|' + + song[duration] + + '.' + ) }, } - -function playSong(id) { - // your code here +//function to check if id is exist in song +function isIdExist(playerSong, id) { + for (let i = 0; i < playerSong; i++) { + if (songs[i].id === id) { + return true + } + } + return false } +function playSong(id) {} + function removeSong(id) { - // your code here + if (!isIdExist(player.songs, id)) { + throw new error('ID is not exist') + } + + for (let i = 0; i < player.songs.length; i++) { + //remove id from songs + if (player.songs[i].id === id) { + player.songs.splice(i, 1) + } + } + for (let j = 0; j < player.playlists.length; j++) { + //remove id from playlist + for (let k = 0; k < player.playlists.songs.length; k++) { + if (player.playlists[j].songs[k] === id) { + player.playlists[j].songs.splice(k, 1) + } + } + } } function addSong(title, album, artist, duration, id) { From 8bc6d9f452e43518e2ec7caaa80c60ca40dc72eb Mon Sep 17 00:00:00 2001 From: shacharmalka1 Date: Mon, 6 Sep 2021 13:47:29 +0300 Subject: [PATCH 02/15] function removePlaylist the function is remove the playlist with the id argument --- index.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 33b9ef3..c6bf997 100644 --- a/index.js +++ b/index.js @@ -63,8 +63,8 @@ const player = { } //function to check if id is exist in song function isIdExist(playerSong, id) { - for (let i = 0; i < playerSong; i++) { - if (songs[i].id === id) { + for (let i = 0; i < playerSong.length; i++) { + if (playerSong[i].id === id) { return true } } @@ -99,7 +99,10 @@ function addSong(title, album, artist, duration, id) { } function removePlaylist(id) { - // your code here + if (!isIdExist(player.playlists, id)) throw new Error('ID is not found') + 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 1b0b63a0a3881366bf38f93849aac216499e2cdd Mon Sep 17 00:00:00 2001 From: shacharmalka1 Date: Mon, 6 Sep 2021 13:49:29 +0300 Subject: [PATCH 03/15] fix the function removeSong --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index c6bf997..e9246d7 100644 --- a/index.js +++ b/index.js @@ -86,7 +86,7 @@ function removeSong(id) { } for (let j = 0; j < player.playlists.length; j++) { //remove id from playlist - for (let k = 0; k < player.playlists.songs.length; k++) { + for (let k = 0; k < player.playlists[j].songs.length; k++) { if (player.playlists[j].songs[k] === id) { player.playlists[j].songs.splice(k, 1) } From 472cf7aecd9fba30b6051059f70977104b985ab3 Mon Sep 17 00:00:00 2001 From: shacharmalka1 Date: Mon, 6 Sep 2021 14:10:09 +0300 Subject: [PATCH 04/15] added function duratinFormat function duratinFormat is present the duration according to the format --- index.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/index.js b/index.js index e9246d7..cab0bc5 100644 --- a/index.js +++ b/index.js @@ -61,6 +61,20 @@ const player = { ) }, } + +function duratinFormat(duration) { + let minutes = Math.floor(duration / 60) + let seconds = duration % 60 + if (minutes < 10 && seconds < 10) { + return '0' + minutes + ':0' + seconds + } else if (minutes < 10) { + return '0' + minutes + ':' + seconds + } else if (seconds < 10) { + return minutes + ':0' + seconds + } + return minutes + ':' + seconds +} + //function to check if id is exist in song function isIdExist(playerSong, id) { for (let i = 0; i < playerSong.length; i++) { From 1855151d73e0b7b1df3849e43b198447fc8f27a8 Mon Sep 17 00:00:00 2001 From: shacharmalka1 Date: Mon, 6 Sep 2021 14:34:10 +0300 Subject: [PATCH 05/15] playSong format :+1: --- index.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index cab0bc5..c29483e 100644 --- a/index.js +++ b/index.js @@ -50,19 +50,19 @@ const player = { playSong(song) { console.log( 'Playing ' + - song[title] + - 'from ' + - song[album] + - 'by ' + - song[artist] + - '|' + - song[duration] + + song.title + + ' from ' + + song.album + + ' by ' + + song.artist + + ' | ' + + durationFormat(song.duration) + '.' ) }, } -function duratinFormat(duration) { +function durationFormat(duration) { let minutes = Math.floor(duration / 60) let seconds = duration % 60 if (minutes < 10 && seconds < 10) { @@ -85,7 +85,14 @@ function isIdExist(playerSong, id) { return false } -function playSong(id) {} +function playSong(id) { + if (!isIdExist(player.songs, id)) throw new Error('ID is not found') + for (let i = 0; i < player.songs.length; i++) { + if (player.songs[i].id === id) { + console.log(player.playSong(player.songs[i])) + } + } +} function removeSong(id) { if (!isIdExist(player.songs, id)) { From 75d992b74c6f4d819e95838d1a487df26f53e4ec Mon Sep 17 00:00:00 2001 From: shacharmalka1 Date: Mon, 6 Sep 2021 15:15:28 +0300 Subject: [PATCH 06/15] added finction maxId return the max id --- index.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index c29483e..5adb709 100644 --- a/index.js +++ b/index.js @@ -63,6 +63,7 @@ const player = { } function durationFormat(duration) { + //presen according the mm:ss format let minutes = Math.floor(duration / 60) let seconds = duration % 60 if (minutes < 10 && seconds < 10) { @@ -84,6 +85,16 @@ function isIdExist(playerSong, id) { } return false } +//return the max id from tae array songs +function maxId() { + let max = player.songs[0].id + for (let i = 0; i < player.songs.length; i++) { + if (max < player.songs[i].id) { + max = arr[i].id + } + } + return max +} function playSong(id) { if (!isIdExist(player.songs, id)) throw new Error('ID is not found') @@ -116,7 +127,13 @@ function removeSong(id) { } function addSong(title, album, artist, duration, id) { - // your code here + player.songs.push({ + title: title, + album: album, + artist: artist, + duration: durationFormat(duration), + id: id, + }) } function removePlaylist(id) { From cec9202834d30e04e90ff1e6fb9a327e34b4cf50 Mon Sep 17 00:00:00 2001 From: shacharmalka1 Date: Mon, 6 Sep 2021 15:29:29 +0300 Subject: [PATCH 07/15] add function newId+function addSong newId is return the max id+1 addSong is pushing to the array songs --- index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 5adb709..eb7d61f 100644 --- a/index.js +++ b/index.js @@ -96,6 +96,11 @@ function maxId() { return max } +//create new id +function newId() { + return maxId() + 1 +} + function playSong(id) { if (!isIdExist(player.songs, id)) throw new Error('ID is not found') for (let i = 0; i < player.songs.length; i++) { @@ -126,7 +131,8 @@ function removeSong(id) { } } -function addSong(title, album, artist, duration, id) { +function addSong(title, album, artist, duration, id = newId()) { + if (isIdExist(player.songs, id)) throw new Error('ID is already exist') player.songs.push({ title: title, album: album, From 79bdf7649ad0095571d73893a1961a5f9ddd9ae8 Mon Sep 17 00:00:00 2001 From: shacharmalka1 Date: Mon, 6 Sep 2021 16:11:45 +0300 Subject: [PATCH 08/15] function createPlaylist pushing to the array playlist --- index.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index eb7d61f..d4df5f2 100644 --- a/index.js +++ b/index.js @@ -62,8 +62,8 @@ const player = { }, } +//presen according the mm:ss format function durationFormat(duration) { - //presen according the mm:ss format let minutes = Math.floor(duration / 60) let seconds = duration % 60 if (minutes < 10 && seconds < 10) { @@ -100,6 +100,7 @@ function maxId() { function newId() { return maxId() + 1 } +/////////////////////////////////////////////////////////////////////////// function playSong(id) { if (!isIdExist(player.songs, id)) throw new Error('ID is not found') @@ -133,13 +134,9 @@ function removeSong(id) { function addSong(title, album, artist, duration, id = newId()) { if (isIdExist(player.songs, id)) throw new Error('ID is already exist') - player.songs.push({ - title: title, - album: album, - artist: artist, - duration: durationFormat(duration), - id: id, - }) + duration = durationFormat(duration) + player.songs.push({ title, album, artist, duration, id }) + return id } function removePlaylist(id) { @@ -149,8 +146,10 @@ function removePlaylist(id) { } } -function createPlaylist(name, id) { - // your code here +function createPlaylist(name, id = newId()) { + if (isIdExist(player.playlists, id)) throw new Error('ID is already exist') + player.playlists.push({ name, id }) + return id } function playPlaylist(id) { From ade32a961f4ce10fe388dede75f153f20f295aaf Mon Sep 17 00:00:00 2001 From: shacharmalka1 Date: Mon, 6 Sep 2021 17:30:21 +0300 Subject: [PATCH 09/15] function playPlaylist used with the function playSong --- index.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d4df5f2..9b86222 100644 --- a/index.js +++ b/index.js @@ -85,6 +85,7 @@ function isIdExist(playerSong, id) { } return false } + //return the max id from tae array songs function maxId() { let max = player.songs[0].id @@ -106,7 +107,7 @@ function playSong(id) { if (!isIdExist(player.songs, id)) throw new Error('ID is not found') for (let i = 0; i < player.songs.length; i++) { if (player.songs[i].id === id) { - console.log(player.playSong(player.songs[i])) + player.playSong(player.songs[i]) } } } @@ -153,7 +154,16 @@ function createPlaylist(name, id = newId()) { } function playPlaylist(id) { - // your code here + if (!isIdExist(player.playlists, id)) + throw new Error('ID already exist, change the ID or omit it') + 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 id } function editPlaylist(playlistId, songId) { From 273e50e12d5e324375c973ab7fee8b1e5f2d7301 Mon Sep 17 00:00:00 2001 From: shacharmalka1 Date: Mon, 6 Sep 2021 18:00:47 +0300 Subject: [PATCH 10/15] function editPlaylist checking if id is existing in songs and in playlist if id is existing but not in the songs array so added the id to the songs array --- index.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 9b86222..909355a 100644 --- a/index.js +++ b/index.js @@ -167,7 +167,24 @@ function playPlaylist(id) { } function editPlaylist(playlistId, songId) { - // your code here + let count = 0 + if (!isIdExist(player.songs, songId)) + throw new Error('ID is not exist, change the ID or omit it') + if (!isIdExist(player.playlists, playlistId)) + throw new Error('ID is not exist, change the ID or omit it') + for (let i = 0; i < player.playlists.length; i++) { + for (let j = 0; j < player.playlists[i].songs.length; j++) { + //console.log(player.playlists[i].songs[j]) + if (player.playlists[i].songs[j] === songId) { + count++ + removeSong(songId) + } + } + if (count === 0) { + player.playlists[i].songs.push(songId) + } + count = 0 + } } function playlistDuration(id) { From 9469e6fe4471dac00fb1380a2ebcb420b366a17e Mon Sep 17 00:00:00 2001 From: shacharmalka1 Date: Tue, 7 Sep 2021 11:26:10 +0300 Subject: [PATCH 11/15] add function oppositDuration+changes in createPlaylist function oppositDuration is changes the duration from the format to seconds change the push to the array --- index.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 909355a..dff549b 100644 --- a/index.js +++ b/index.js @@ -101,6 +101,16 @@ function maxId() { function newId() { return maxId() + 1 } + +//converting mm:ss to seconds +function oppositDuration(duration) { + duration = duration.split(':') + console.log(duration) + let minutes = parseInt(duration[0]) * 60 + let seconds = parseInt(duration[1]) + return minutes + seconds +} + /////////////////////////////////////////////////////////////////////////// function playSong(id) { @@ -135,7 +145,7 @@ function removeSong(id) { function addSong(title, album, artist, duration, id = newId()) { if (isIdExist(player.songs, id)) throw new Error('ID is already exist') - duration = durationFormat(duration) + duration = oppositDuration(duration) player.songs.push({ title, album, artist, duration, id }) return id } @@ -149,7 +159,7 @@ function removePlaylist(id) { function createPlaylist(name, id = newId()) { if (isIdExist(player.playlists, id)) throw new Error('ID is already exist') - player.playlists.push({ name, id }) + player.playlists.push({ name, id, songs: [] }) return id } From 0a5bdcf696f9a20548f42601cab44c0100311508 Mon Sep 17 00:00:00 2001 From: shacharmalka1 Date: Tue, 7 Sep 2021 11:51:11 +0300 Subject: [PATCH 12/15] function playlistDuration Returns the total duration of the entire playlist with the given ID. --- index.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index dff549b..e3c63d2 100644 --- a/index.js +++ b/index.js @@ -198,7 +198,18 @@ function editPlaylist(playlistId, songId) { } function playlistDuration(id) { - // your code here + let save = 0 + let sum = 0 + for (let i = 0; i < player.playlists.length; i++) { + if (id === player.playlists[i].id) + for (let j = 0; j < player.playlists[i].songs.length; j++) { + save = player.playlists[i].songs[j] + for (let t = 0; t < player.songs.length; t++) { + if (player.songs[t].id === save) sum += player.songs[t].duration + } + } + } + return sum } function searchByQuery(query) { From 368efcab76221638e50671ea2353276dd52522bb Mon Sep 17 00:00:00 2001 From: shacharmalka1 Date: Tue, 7 Sep 2021 21:45:51 +0300 Subject: [PATCH 13/15] function findPlaylistId+ function removeSongsFromPlaylist+changes editPlaylist function findPlaylistId find the playlist with the id that she is get function removeSongsFromPlaylist remove the song only from the playlist editPlaylist is working --- index.js | 57 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index e3c63d2..80210eb 100644 --- a/index.js +++ b/index.js @@ -111,6 +111,27 @@ function oppositDuration(duration) { return minutes + seconds } +//Get a playlist id and return +function findPlaylistId(id) { + let correctPlaylist + for (let i = 0; i < player.playlists.length; i++) { + if (id === player.playlists[i].id) correctPlaylist = player.playlists[i] + } + return correctPlaylist +} + +//this function remove a song only from the playlist and not from the songs object +function removeSongsFromPlaylist(id) { + for (let j = 0; j < player.playlists.length; j++) { + // remove from playlists + for (let k = 0; k < player.playlists[j].songs.length; k++) { + if (player.playlists[j].songs[k] === id) { + player.playlists[j].songs.splice(k, 1) + } + } + } +} + /////////////////////////////////////////////////////////////////////////// function playSong(id) { @@ -177,23 +198,25 @@ function playPlaylist(id) { } function editPlaylist(playlistId, songId) { - let count = 0 if (!isIdExist(player.songs, songId)) - throw new Error('ID is not exist, change the ID or omit it') + throw new Error("ID isn't exist, change the ID") if (!isIdExist(player.playlists, playlistId)) - throw new Error('ID is not exist, change the ID or omit it') - for (let i = 0; i < player.playlists.length; i++) { - for (let j = 0; j < player.playlists[i].songs.length; j++) { - //console.log(player.playlists[i].songs[j]) - if (player.playlists[i].songs[j] === songId) { - count++ - removeSong(songId) - } + throw new Error("ID isn't exist, change the ID") + let correctPlaylist = findPlaylistId(playlistId) + //runs on the playlists + for (let j = 0; j < correctPlaylist.songs.length; j++) { + //runs on the songs array in the playlist + if (songId === correctPlaylist.songs[j]) { + //If the song ID exists in the playlist + removeSongsFromPlaylist(songId) + //removes it + } else { + correctPlaylist.songs.push(songId) } - if (count === 0) { - player.playlists[i].songs.push(songId) + if (correctPlaylist.songs.length === 0) { + //If it was the only song in the playlist + removePlaylist(correctPlaylist.id) } - count = 0 } } @@ -217,7 +240,13 @@ function searchByQuery(query) { } function searchByDuration(duration) { - // your code here + let min = Math.abs(player.songs[0].duration - oppositDuration(duration)) + for (let i = 1; i < player.songs.length; i++) { + if (Math.abs(oppositDuration(duration) - player.songs[i].duration) < min) { + min = player.songs[i] + } + } + return min } module.exports = { From 201b19329d2cf7fb1b4f0e7203cf3c8134856854 Mon Sep 17 00:00:00 2001 From: shacharmalka1 Date: Tue, 7 Sep 2021 21:53:13 +0300 Subject: [PATCH 14/15] function arrLengthmPlaylist+function arrLengthSongs+function searchByDuration arrLengthmPlaylist return array with the minimum range and the object same for arrLengthSongs searchByDuration return the object with the minimum range from the playlist and songs --- index.js | 50 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 80210eb..840d70e 100644 --- a/index.js +++ b/index.js @@ -132,6 +132,43 @@ function removeSongsFromPlaylist(id) { } } +//gets song duartion return array of [closet-duration-seconds,closet-duration-playlist] +function arrLengthmPlaylist(duration) { + let arr = [] + let minDuration = duration, + index = 0 + for (let i = 0; i < player.playlists.length; i++) { + if ( + minDuration > + Math.abs(duration - playlistDuration(player.playlists[i].id)) + ) { + minDuration = Math.abs( + duration - playlistDuration(player.playlists[i].id) + ) + index = i + } + } + arr.push(minDuration) + arr.push(player.playlists[index]) + return arr +} + +//gets song duartion return array of [closet-duration-seconds,closet-duration-song] +function arrLengthSongs(duration) { + let arr = [] + let minDuration = duration, + index = 0 //{id: 3,title: "Thunderstruck", album: "The Razors Edge"} + for (let i = 0; i < player.songs.length; i++) { + if (minDuration > Math.abs(duration - player.songs[i].duration)) { + minDuration = Math.abs(duration - player.songs[i].duration) + index = i + } + } + arr.push(minDuration) + arr.push(player.songs[index]) + return arr +} + /////////////////////////////////////////////////////////////////////////// function playSong(id) { @@ -240,13 +277,12 @@ function searchByQuery(query) { } function searchByDuration(duration) { - let min = Math.abs(player.songs[0].duration - oppositDuration(duration)) - for (let i = 1; i < player.songs.length; i++) { - if (Math.abs(oppositDuration(duration) - player.songs[i].duration) < min) { - min = player.songs[i] - } - } - return min + duration = oppositDuration(duration) + let arrSongs = arrLengthSongs(duration) + let arrPlaylist = arrLengthmPlaylist(duration) + console.log(arrSongs) + console.log(arrPlaylist) + return arrSongs[0] < arrPlaylist[0] ? arrSongs[1] : arrPlaylist[1] } module.exports = { From cd470dd56c59061bf6bc35d9d6b03c1574172e89 Mon Sep 17 00:00:00 2001 From: shacharmalka1 Date: Tue, 7 Sep 2021 22:20:27 +0300 Subject: [PATCH 15/15] function searchByQuery results create array with array of songs and playlist and return this array --- index.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 840d70e..e008910 100644 --- a/index.js +++ b/index.js @@ -273,7 +273,30 @@ function playlistDuration(id) { } function searchByQuery(query) { - // your code here + const results = { songs: [], playlists: [] } + let querylower = query.toLowerCase() + for (let i = 0; i < player.songs.length; i++) { + if ( + player.songs[i].album.toLowerCase().includes(querylower) || + player.songs[i].artist.toLowerCase().includes(querylower) || + player.songs[i].title.toLowerCase().includes(querylower) + ) { + results.songs.push(player.songs[i]) + results.songs.sort((a, b) => { + if (a['title'].toLowerCase() < b['title'].toLowerCase()) return -1 + }) + } + } + + for (let j = 0; j < player.playlists.length; j++) { + if (player.playlists[j].name.toLowerCase().includes(querylower)) { + results.playlists.push(player.playlists[j]) + results.playlists.sort((a, b) => { + if (a['name'].toLowerCase() < b['name'].toLowerCase()) return -1 + }) + } + } + return results } function searchByDuration(duration) {