From ca27b794ff60644b0d540505cf02aa21158dc4eb Mon Sep 17 00:00:00 2001 From: ArnonAsquira Date: Mon, 6 Sep 2021 00:20:24 +0300 Subject: [PATCH 01/14] Adding the playSong method and an assistance function to convert the seconds vaue of a song's duration to the correct duration format --- index.js | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 10f4784..0e8282e 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,27 @@ +//assistance functions +function secondsToMinutesConvertor(songDuration){ + let durationInMinutes = songDuration / 60; + let minutes = 0; + let seconds = 0; + let lengthFormat = 0; + minutes = Math.floor(durationInMinutes); + if(minutes < 10){ + minutes = "0" + minutes.toString(); + }else{ + minutes = minutes.tostring(); + } + seconds = (Math.round((durationInMinutes - minutes) * 60)); + if( seconds < 10){ + seconds = "0" + seconds.toString(); + }else{ + seconds = seconds.toString(); + } + lengthFormat = minutes + ":" + seconds + return lengthFormat +} + + + const player = { songs: [ { @@ -48,10 +72,23 @@ const player = { { id: 5, name: 'Israeli', songs: [4, 5] }, ], playSong(song) { - console.log(/* your code here */) + song = player.songs.filter(songObject => { + if(songObject.id === song){ + return songObject; + } + }) + console.log(song); + if(song.length == false){ + console.log("undefined id") + return "undefined id"; + } + song = song[0]; + song.duration = secondsToMinutesConvertor(song.duration); + console.log("Playing " + song.title + " from " + song.album + " by " + song.artist + " | " + song.duration + ".") + return("Playing " + song.title + " from " + song.album + " by " + song.artist + " | " + song.duration + ".") }, } - +player.playSong(0) function playSong(id) { // your code here } From ae56ff7550be04977097e6f27145bde0e785fe9c Mon Sep 17 00:00:00 2001 From: ArnonAsquira Date: Mon, 6 Sep 2021 13:14:17 +0300 Subject: [PATCH 02/14] Adding the "removeSong" unction and adding another external assistance function to find the song object of a requested sing id --- index.js | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 0e8282e..0541b73 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,6 @@ //assistance functions + +//converts seconds to the required minute fomat function secondsToMinutesConvertor(songDuration){ let durationInMinutes = songDuration / 60; let minutes = 0; @@ -19,7 +21,19 @@ function secondsToMinutesConvertor(songDuration){ lengthFormat = minutes + ":" + seconds return lengthFormat } - +//recieves a song's id an returns that song's object +function getSongObjectById(id){ + let song = player.songs.filter(songObject => { + if(songObject.id === id){ + return songObject; + } + }) + if(song.length == false){ + throw "undefined id"; + } + song = song[0]; + return song; +} const player = { @@ -71,30 +85,32 @@ const player = { { id: 1, name: 'Metal', songs: [1, 7, 4] }, { id: 5, name: 'Israeli', songs: [4, 5] }, ], - playSong(song) { - song = player.songs.filter(songObject => { - if(songObject.id === song){ - return songObject; - } - }) - console.log(song); - if(song.length == false){ - console.log("undefined id") - return "undefined id"; - } - song = song[0]; + playSong(id) { + let song = getSongObjectById(id); song.duration = secondsToMinutesConvertor(song.duration); - console.log("Playing " + song.title + " from " + song.album + " by " + song.artist + " | " + song.duration + ".") return("Playing " + song.title + " from " + song.album + " by " + song.artist + " | " + song.duration + ".") }, } -player.playSong(0) + +// playSong outside function function playSong(id) { - // your code here + console.log(player.playSong(id)) } +playSong(5); function removeSong(id) { - // your code here + let indexCounter = 0; + player.songs.forEach(song => { + indexCounter ++; + if(song.id === id){ + player.songs.splice(player.songs[indexCounter] ,1); + }else{ + indexCounter ++; + } + if(indexCounter === player.songs.length){ + throw "Undefined ID" + } + }) } function addSong(title, album, artist, duration, id) { From a48bcbf154933e5616ed522a5856ad3cbcd7b43f Mon Sep 17 00:00:00 2001 From: ArnonAsquira Date: Mon, 6 Sep 2021 14:37:17 +0300 Subject: [PATCH 03/14] Removing song from playlist added to the removeSong function + adjustments and debugging --- index.js | 73 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 59 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 0541b73..dd11661 100644 --- a/index.js +++ b/index.js @@ -7,17 +7,25 @@ function secondsToMinutesConvertor(songDuration){ let seconds = 0; let lengthFormat = 0; minutes = Math.floor(durationInMinutes); - if(minutes < 10){ - minutes = "0" + minutes.toString(); + if(minutes === 0){ + minutes = "00" }else{ - minutes = minutes.tostring(); - } + if(minutes < 10){ + minutes = "0" + minutes.toString(); + }else{ + minutes = minutes.toString(); + } +} seconds = (Math.round((durationInMinutes - minutes) * 60)); - if( seconds < 10){ - seconds = "0" + seconds.toString(); - }else{ - seconds = seconds.toString(); - } + if(seconds === 0){ + seconds = "00" + }else { + if( seconds < 10){ + seconds = "0" + seconds.toString(); + }else{ + seconds = seconds.toString(); + } +} lengthFormat = minutes + ":" + seconds return lengthFormat } @@ -101,20 +109,57 @@ playSong(5); function removeSong(id) { let indexCounter = 0; player.songs.forEach(song => { - indexCounter ++; if(song.id === id){ player.songs.splice(player.songs[indexCounter] ,1); }else{ indexCounter ++; - } - if(indexCounter === player.songs.length){ - throw "Undefined ID" + if(indexCounter === player.songs.length){ + throw "Undefined ID" + } } }) + player.playlists.forEach(playlist => { + let playlistIndexCounter = 0; + let songIndexCounter = 0; + playlist.songs.forEach(song => { + if (song === id){ + player.playlists[playlistIndexCounter].songs.splice(songIndexCounter, 1); + }else{ + songIndexCounter ++; + } + }) + }) } +let idCounter = 8; function addSong(title, album, artist, duration, id) { - // your code here + let newSongId = 0; + let indexCounter = 0; + if(id == undefined){ + newSongId = idCounter; + }else{ + player.songs.forEach(song => { + if(song.id === id){ + throw "taken ID" + }else{ + indexCounter ++; + if(indexCounter === player.songs.length){ + newSongId = id; + } + } + }); + } + idCounter ++; + duration = secondsToMinutesConvertor(duration); + let newSong = { + "title" : title, + "album" : album, + "artist" : artist, + "duration" : duration, + "id" : newSongId + } + player.songs.push(newSong); + return newSongId } function removePlaylist(id) { From dfe169b5ee1d1628a168c8a0d03e251f087ebec7 Mon Sep 17 00:00:00 2001 From: ArnonAsquira Date: Mon, 6 Sep 2021 15:31:45 +0300 Subject: [PATCH 04/14] Adding the removePlaylist function + function adjustments and debugging --- index.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index dd11661..9b10e65 100644 --- a/index.js +++ b/index.js @@ -110,7 +110,7 @@ function removeSong(id) { let indexCounter = 0; player.songs.forEach(song => { if(song.id === id){ - player.songs.splice(player.songs[indexCounter] ,1); + player.songs.splice(indexCounter ,1); }else{ indexCounter ++; if(indexCounter === player.songs.length){ @@ -150,20 +150,30 @@ function addSong(title, album, artist, duration, id) { }); } idCounter ++; - duration = secondsToMinutesConvertor(duration); + duration = secondsToMinutesConvertor(duration); let newSong = { + "id" : newSongId, "title" : title, "album" : album, "artist" : artist, - "duration" : duration, - "id" : newSongId + "duration" : duration } player.songs.push(newSong); return newSongId } function removePlaylist(id) { - // your code here + let indexCounter = 0; + player.playlists.forEach(playlist => { + if(playlist.id === id){ + player.playlists.splice(indexCounter ,1); + }else{ + indexCounter ++; + } + if(indexCounter === player.playlists.length){ + throw "Undefined ID" + } + }) } function createPlaylist(name, id) { From 22a32b2296ca6add1e197b132b85e55fc89b3f63 Mon Sep 17 00:00:00 2001 From: ArnonAsquira Date: Mon, 6 Sep 2021 16:15:03 +0300 Subject: [PATCH 05/14] Adding the createPlaylist function, And creating an external function to generate new Id's, yet the test doesn't seem to pick up on it. i will fix it later --- index.js | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 9b10e65..af67c37 100644 --- a/index.js +++ b/index.js @@ -42,6 +42,27 @@ function getSongObjectById(id){ song = song[0]; return song; } +// generat new ID function +function generatId(id, playlistsOrSongs, songsOrPlaylistsIdCounter){ + let newId = 0; + let indexCounter = 0; + if(!id){ + newId = songsOrPlaylistsIdCounter; + }else{ + player[playlistsOrSongs].forEach(song => { + if(song.id === id){ + throw "taken ID" + }else{ + indexCounter ++; + if(indexCounter === player[playlistsOrSongs].length){ + newId = id; + } + } + }); + } + songsOrPlaylistsIdCounter++; + return newId +} const player = { @@ -175,10 +196,19 @@ function removePlaylist(id) { } }) } - +let playlistIdCounter = 6; function createPlaylist(name, id) { - // your code here + let playListId = generatId(id, "playlists", playlistIdCounter); + console.log(playListId); + let newPlaylist = { + "id" : playListId, + "name" : name, + "songs" : [] + } + console.log(newPlaylist); + player.playlists.push(newPlaylist); } +createPlaylist("mosh"); function playPlaylist(id) { // your code here From a6430e60fba9eb52f5e3d168a7cf189b9996abcd Mon Sep 17 00:00:00 2001 From: ArnonAsquira Date: Mon, 6 Sep 2021 17:02:32 +0300 Subject: [PATCH 06/14] Addign the playPlaylist function --- index.js | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index af67c37..36e86ad 100644 --- a/index.js +++ b/index.js @@ -154,23 +154,7 @@ function removeSong(id) { let idCounter = 8; function addSong(title, album, artist, duration, id) { - let newSongId = 0; - let indexCounter = 0; - if(id == undefined){ - newSongId = idCounter; - }else{ - player.songs.forEach(song => { - if(song.id === id){ - throw "taken ID" - }else{ - indexCounter ++; - if(indexCounter === player.songs.length){ - newSongId = id; - } - } - }); - } - idCounter ++; + let newSongId = generatId(id, "songs", idCounter); duration = secondsToMinutesConvertor(duration); let newSong = { "id" : newSongId, @@ -205,13 +189,17 @@ function createPlaylist(name, id) { "name" : name, "songs" : [] } - console.log(newPlaylist); player.playlists.push(newPlaylist); + return playListId } -createPlaylist("mosh"); function playPlaylist(id) { - // your code here + let PlaylsitById = player.playlists.filter(playlist =>{ + if(playlist.id === id){ + return playlist; + } + }) + PlaylsitById[0].songs.forEach(song => playSong(song)); } function editPlaylist(playlistId, songId) { From a0f1112ae9d8a5dcf5df6c012285a9e16bd5007b Mon Sep 17 00:00:00 2001 From: ArnonAsquira Date: Mon, 6 Sep 2021 22:38:53 +0300 Subject: [PATCH 07/14] Adding the editPlaylist function --- index.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 36e86ad..440bad6 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,7 @@ //assistance functions +const { pipelinePrimaryTopicReference } = require("@babel/types"); + //converts seconds to the required minute fomat function secondsToMinutesConvertor(songDuration){ let durationInMinutes = songDuration / 60; @@ -63,7 +65,15 @@ function generatId(id, playlistsOrSongs, songsOrPlaylistsIdCounter){ songsOrPlaylistsIdCounter++; return newId } - +// get playlist by ID +function getPlaylistById(id){ + let playlistById = player.playlists.filter(playlist =>{ + if(playlist.id === id){ + return playlist; + } + }) + return playlistById[0]; + } const player = { songs: [ @@ -203,7 +213,42 @@ function playPlaylist(id) { } function editPlaylist(playlistId, songId) { - // your code here + let playlistIndexCounter = 0; + player.playlists.forEach(playlist =>{ + let songsIndexCounter = 0; + if(playlist.id === playlistId){ + playlist.songs.forEach(song => { + if(songId === song){ + if(playlist.songs.length === 1){ + player.playlists.splice(playlistIndexCounter, 1) + }else{ + playlist.songs.splice(songsIndexCounter, 1); + } + }else{ + songsIndexCounter ++; + if(songsIndexCounter === playlist.songs.length){ + let indexCounter = 0; + player.songs.forEach(song => { + if(song.id === songId){ + player.playlists[playlistIndexCounter].songs.push(songId); + }else{ + indexCounter ++; + if(indexCounter === player.songs.length){ + throw "non existant song ID" + } + } + }) + } + } + }) + }else{ + playlistIndexCounter ++; + if(playlistIndexCounter === player.playlists.length){ + throw "non existant playlist ID" + } + } + }) + } function playlistDuration(id) { From a43e7c1a4ca40883a010ab8e11ace77a55124df1 Mon Sep 17 00:00:00 2001 From: ArnonAsquira Date: Mon, 6 Sep 2021 22:55:04 +0300 Subject: [PATCH 08/14] adding a string format duration to seconds in the addSong function after i realized the exact intention of this test i modified the relevent function --- index.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 440bad6..894daaf 100644 --- a/index.js +++ b/index.js @@ -30,6 +30,13 @@ function secondsToMinutesConvertor(songDuration){ } lengthFormat = minutes + ":" + seconds return lengthFormat +} + // converts string minutes format to secoonds + function convertToseconds(durationInMinutes){ + let minutes = Number(durationInMinutes.split("").slice(0, 2).join("")); + let seconds = Number(durationInMinutes.split("").slice(3, 5).join("")); + let totalTime = (minutes * 60) + seconds; + return totalTime } //recieves a song's id an returns that song's object function getSongObjectById(id){ @@ -136,7 +143,7 @@ function playSong(id) { console.log(player.playSong(id)) } playSong(5); - +//remove song function function removeSong(id) { let indexCounter = 0; player.songs.forEach(song => { @@ -161,11 +168,11 @@ function removeSong(id) { }) }) } - +//add song function let idCounter = 8; function addSong(title, album, artist, duration, id) { let newSongId = generatId(id, "songs", idCounter); - duration = secondsToMinutesConvertor(duration); + duration = convertToseconds(duration); let newSong = { "id" : newSongId, "title" : title, @@ -176,7 +183,7 @@ function addSong(title, album, artist, duration, id) { player.songs.push(newSong); return newSongId } - +//remove playlist function function removePlaylist(id) { let indexCounter = 0; player.playlists.forEach(playlist => { @@ -190,6 +197,7 @@ function removePlaylist(id) { } }) } +//create playlist function let playlistIdCounter = 6; function createPlaylist(name, id) { let playListId = generatId(id, "playlists", playlistIdCounter); From f5a0f379b3189acd01d84df6b6537c129503b3c5 Mon Sep 17 00:00:00 2001 From: ArnonAsquira Date: Mon, 6 Sep 2021 23:09:47 +0300 Subject: [PATCH 09/14] Adding the playlistDuration function --- index.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 894daaf..c560ac5 100644 --- a/index.js +++ b/index.js @@ -74,14 +74,20 @@ function generatId(id, playlistsOrSongs, songsOrPlaylistsIdCounter){ } // get playlist by ID function getPlaylistById(id){ + let indexCounter = 0; let playlistById = player.playlists.filter(playlist =>{ if(playlist.id === id){ return playlist; + }else{ + indexCounter ++; + if(indexCounter === player.playlists.length){ + throw "non existant ID" + } } }) return playlistById[0]; } - +// main work const player = { songs: [ { @@ -220,6 +226,7 @@ function playPlaylist(id) { PlaylsitById[0].songs.forEach(song => playSong(song)); } +//edit playlist function function editPlaylist(playlistId, songId) { let playlistIndexCounter = 0; player.playlists.forEach(playlist =>{ @@ -260,7 +267,15 @@ function editPlaylist(playlistId, songId) { } function playlistDuration(id) { - // your code here + let requestedPlaylist = getPlaylistById(id); + let songsLengthsArray = requestedPlaylist.songs.map(song => { + return (getSongObjectById(song).duration); + }) + let totalDuration = (songsLengthsArray.reduce((acc, value) => { + acc += value; + return acc; + })) + return totalDuration; } function searchByQuery(query) { From 2c6026ceff22225abf9f3fd5742994a44c91f8fb Mon Sep 17 00:00:00 2001 From: ArnonAsquira Date: Tue, 7 Sep 2021 00:06:30 +0300 Subject: [PATCH 10/14] adding the search by query function + debugging and adjustments --- index.js | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index c560ac5..9fd508a 100644 --- a/index.js +++ b/index.js @@ -197,11 +197,11 @@ function removePlaylist(id) { player.playlists.splice(indexCounter ,1); }else{ indexCounter ++; - } - if(indexCounter === player.playlists.length){ - throw "Undefined ID" - } - }) + if(indexCounter === player.playlists.length){ + throw "Undefined ID" + } + } + }) } //create playlist function let playlistIdCounter = 6; @@ -277,9 +277,33 @@ function playlistDuration(id) { })) return totalDuration; } - +//search by query function function searchByQuery(query) { - // your code here + let matchingSongs = player.songs.filter(song => { + for(let property in song){ + if (song[property].toString().toLowerCase().includes(query.toLowerCase())){ + return song; + } + } + }) + let matchingPlaylists = player.playlists.filter(playlist => { + if(playlist.name.toLowerCase().includes(query.toLowerCase())){ + return playlist; + } + }) + matchingSongs = matchingSongs.sort((a, b) => { + if(b["title"] < a["title"]){ + return 1; + } + if(b["title"] > a["title"]){ + return -1 + } + return 0; + }); + return { + "songs" : matchingSongs, + "playlists" : matchingPlaylists + } } function searchByDuration(duration) { From dc39ce65beb9ff647232597c3d38afabf29d6821 Mon Sep 17 00:00:00 2001 From: ArnonAsquira Date: Tue, 7 Sep 2021 15:44:37 +0300 Subject: [PATCH 11/14] Adding the searchBYduration function only for songs at this point. playlist search to be added in the future --- index.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 9fd508a..1fc1400 100644 --- a/index.js +++ b/index.js @@ -305,9 +305,18 @@ function searchByQuery(query) { "playlists" : matchingPlaylists } } - +// search by duratiopn function function searchByDuration(duration) { - // your code here + let totalTime = convertToseconds(duration); + let closestTime = Math.abs(totalTime - player.songs[0].duration); + let closestSong = player.songs[0]; + player.songs.forEach(song =>{ + if(Math.abs(totalTime - song.duration) < closestTime){ + closestTime = Math.abs(totalTime - song.duration); + closestSong = song; + } + }) + return closestSong; } module.exports = { From afec16da9ab08989ef2541ed7f5cae2a72fa97d4 Mon Sep 17 00:00:00 2001 From: ArnonAsquira Date: Tue, 7 Sep 2021 16:51:58 +0300 Subject: [PATCH 12/14] Adding the playlist search in the searchByDuration function --- index.js | 49 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 1fc1400..66c6f36 100644 --- a/index.js +++ b/index.js @@ -87,6 +87,33 @@ function getPlaylistById(id){ }) return playlistById[0]; } + //closest song or playlist function +function initialClosest (songsOrPlaylists , totalTime){ + if(songsOrPlaylists === "playlists"){ + return Math.abs(totalTime - playlistDuration(player.playlists[0]["id"])) + }else{ + return Math.abs(totalTime - player[songsOrPlaylists][0].duration) + } +} +//find closest song +function findClosest(songsOrPlaylists, closestTime, totalTime){ + let closest = player[songsOrPlaylists][0]; + player[songsOrPlaylists].forEach(song =>{ + if(songsOrPlaylists === "playlists"){ + let songDuration = playlistDuration(song["id"]); + if(Math.abs(totalTime - songDuration) < closestTime){ + closestTime = Math.abs(totalTime - songDuration); + closest = song; + } + }else{ + if(Math.abs(totalTime - song.duration) < closestTime){ + closestTime = Math.abs(totalTime - song.duration); + closest = song; + } + } + }) + return closest; +} // main work const player = { songs: [ @@ -143,7 +170,6 @@ const player = { return("Playing " + song.title + " from " + song.album + " by " + song.artist + " | " + song.duration + ".") }, } - // playSong outside function function playSong(id) { console.log(player.playSong(id)) @@ -265,7 +291,7 @@ function editPlaylist(playlistId, songId) { }) } - +// playlistDuration function function playlistDuration(id) { let requestedPlaylist = getPlaylistById(id); let songsLengthsArray = requestedPlaylist.songs.map(song => { @@ -308,17 +334,16 @@ function searchByQuery(query) { // search by duratiopn function function searchByDuration(duration) { let totalTime = convertToseconds(duration); - let closestTime = Math.abs(totalTime - player.songs[0].duration); - let closestSong = player.songs[0]; - player.songs.forEach(song =>{ - if(Math.abs(totalTime - song.duration) < closestTime){ - closestTime = Math.abs(totalTime - song.duration); - closestSong = song; - } - }) - return closestSong; + let closestSongtTime = initialClosest("songs", totalTime); + let closestPlaylistTime = initialClosest("playlists", totalTime); + let closestSong = findClosest("songs", closestSongtTime, totalTime); + let closestPlaylist = findClosest("playlists", closestPlaylistTime, totalTime); + if(closestSongtTime > closestPlaylistTime){ + return closestPlaylist; + }else{ + return closestSong; + } } - module.exports = { player, playSong, From ed0ba6d311623ac46283caa2ccaff5ec8c70c53a Mon Sep 17 00:00:00 2001 From: ArnonAsquira Date: Thu, 9 Sep 2021 16:53:59 +0300 Subject: [PATCH 13/14] creating a non existent id checker function in order to make the editPlaylist function cleaner --- index.js | 75 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/index.js b/index.js index 66c6f36..76252e4 100644 --- a/index.js +++ b/index.js @@ -114,6 +114,20 @@ function findClosest(songsOrPlaylists, closestTime, totalTime){ }) return closest; } +//check for non existent ID +function IDchecker(id, playlistOrSong){ + let indexCounter = 0; + player[playlistOrSong].forEach(object => { + if (object.id === id){ + indexCounter = -1; + } else{ + indexCounter ++; + if(indexCounter === player[playlistOrSong].length){ + throw "non existent id" + } + } + }) +} // main work const player = { songs: [ @@ -253,43 +267,38 @@ function playPlaylist(id) { } //edit playlist function -function editPlaylist(playlistId, songId) { +function editPlaylist(playlistId, songId){ + IDchecker(playlistId, "playlists"); + IDchecker(songId, "songs"); let playlistIndexCounter = 0; player.playlists.forEach(playlist =>{ - let songsIndexCounter = 0; - if(playlist.id === playlistId){ - playlist.songs.forEach(song => { - if(songId === song){ - if(playlist.songs.length === 1){ - player.playlists.splice(playlistIndexCounter, 1) - }else{ - playlist.songs.splice(songsIndexCounter, 1); - } - }else{ - songsIndexCounter ++; - if(songsIndexCounter === playlist.songs.length){ - let indexCounter = 0; - player.songs.forEach(song => { - if(song.id === songId){ - player.playlists[playlistIndexCounter].songs.push(songId); - }else{ - indexCounter ++; - if(indexCounter === player.songs.length){ - throw "non existant song ID" - } - } - }) - } + let songsIndexCounter = 0; + if(playlist.id === playlistId){ + playlist.songs.forEach(song => { + if(songId === song){ + if(playlist.songs.length === 1){ + player.playlists.splice(playlistIndexCounter, 1) + }else{ + playlist.songs.splice(songsIndexCounter, 1); + } + }else{ + songsIndexCounter ++; + if(songsIndexCounter === playlist.songs.length){ + let indexCounter = 0; + player.songs.forEach(song => { + if(song.id === songId){ + player.playlists[playlistIndexCounter].songs.push(songId); + }else{ + indexCounter ++; + } + }) } - }) - }else{ - playlistIndexCounter ++; - if(playlistIndexCounter === player.playlists.length){ - throw "non existant playlist ID" } - } - }) - + }) + }else{ + playlistIndexCounter ++; + } +}) } // playlistDuration function function playlistDuration(id) { From 93fb1ea4540cb4a65f4fc9cca4ebfacf74adcf10 Mon Sep 17 00:00:00 2001 From: ArnonAsquira Date: Sun, 12 Sep 2021 11:43:33 +0300 Subject: [PATCH 14/14] Update index.js --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 76252e4..3499b6b 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,7 @@ const { pipelinePrimaryTopicReference } = require("@babel/types"); -//converts seconds to the required minute fomat +//converts seconds to the required minute format function secondsToMinutesConvertor(songDuration){ let durationInMinutes = songDuration / 60; let minutes = 0; @@ -256,7 +256,7 @@ function createPlaylist(name, id) { player.playlists.push(newPlaylist); return playListId } - +// playPLaylist function function playPlaylist(id) { let PlaylsitById = player.playlists.filter(playlist =>{ if(playlist.id === id){