From 250cf900d1ae8093ed50d8af95b729b8664681b1 Mon Sep 17 00:00:00 2001 From: MaorKurztag21 Date: Fri, 10 Sep 2021 04:46:48 +0300 Subject: [PATCH 01/13] add "playSong" function and 2 external External functions: "mmssDuration" and "getSongById" --- __tests__/main.test.js | 33 +----------------------- index.js | 57 +++++++++++++++++++++++++++--------------- 2 files changed, 38 insertions(+), 52 deletions(-) diff --git a/__tests__/main.test.js b/__tests__/main.test.js index bf3746e..0456aae 100644 --- a/__tests__/main.test.js +++ b/__tests__/main.test.js @@ -1,5 +1,4 @@ const deepClone = require('just-clone') - const { player, playSong, @@ -13,7 +12,6 @@ const { searchByDuration, searchByQuery, } = require('../index') - const mockSong1 = { id: 5, title: 'As a Stone', @@ -48,12 +46,10 @@ const mockPlaylist1 = { songs: [mockSong1.id, mockSong2.id], } const mockPlaylist2 = { id: 3, name: 'Metal', songs: [mockSong1.id] } - const mockPlayer = { songs: [mockSong1, mockSong2, mockSong4], playlists: [mockPlaylist1], } - const mockSong3Details = [ mockSong3.title, mockSong3.album, @@ -62,17 +58,14 @@ const mockSong3Details = [ mockSong3.id, ] const mockPlaylist1Duration = mockSong1.duration + mockSong2.duration - const mockNonExistentSongId = 2 const mockNonExistentPlaylistId = 1 - describe('Player Tests', () => { beforeEach(() => { player.songs = deepClone(mockPlayer.songs) player.playlists = deepClone(mockPlayer.playlists) jest.clearAllMocks() }) - it('playSong should console.log in the correct format', () => { const spy = jest.spyOn(console, 'log') playSong(mockSong1.id) @@ -80,7 +73,6 @@ describe('Player Tests', () => { `Playing As a Stone from Show Us What You Got by Full Trunk | 04:19.` ) }) - it('playSong should throw for non-existent ID', () => { expect(() => playSong(mockNonExistentSongId)).toThrow() }) @@ -89,40 +81,32 @@ describe('Player Tests', () => { removeSong(mockSong1.id) expect(player.songs).toEqual([mockSong2, mockSong4]) }) - it('removeSong should remove the song from all playlists', () => { removeSong(mockSong1.id) expect(player.playlists[0].songs).toEqual([mockSong2.id]) }) - it('removeSong should throw for non-existent ID', () => { expect(() => removeSong(mockNonExistentSongId)).toThrow() }) - it('addSong should add a new song to the player', () => { addSong(...mockSong3Details) expect(player.songs).toEqual([...mockPlayer.songs, mockSong3]) }) - it('addSong should generate a new unique ID when it is not supplied', () => { const newSongId = addSong(...mockSong3Details.slice(0, -1)) expect(newSongId).toBeDefined() expect(mockPlayer.songs.map(song => song.id).includes(newSongId)).toBe(false) }) - it('addSong should throw for an ID that is taken', () => { expect(() => addSong(...mockSong3Details.slice(0, -1), mockSong1.id)).toThrow() }) - it('removePlaylist should remove a playlist from the player', () => { removePlaylist(mockPlaylist1.id) expect(player.playlists.length).toBe(0) }) - it('removePlaylist should throw for non-existent ID', () => { expect(() => removePlaylist(mockNonExistentPlaylistId)).toThrow() }) - it('createPlaylist should add a new playlist to the player', () => { createPlaylist(mockPlaylist2.name, mockPlaylist2.id) expect(player.playlists).toEqual([ @@ -130,27 +114,22 @@ describe('Player Tests', () => { { ...mockPlaylist2, songs: [] }, ]) }) - it('createPlaylist should generate a new unique ID when it is not supplied', () => { const newPlaylistId = createPlaylist(mockPlaylist2.name) expect(newPlaylistId).toBeDefined() expect(mockPlayer.playlists.map(p => p.id).includes(newPlaylistId)).toBe(false) }) - it('createPlaylist should throw for an ID that is taken', () => { expect(() => createPlaylist(mockPlaylist2.name, mockPlaylist1.id)).toThrow() }) - it('playPlaylist should play all songs inside it', () => { const spy = jest.spyOn(console, 'log') playPlaylist(mockPlaylist1.id) expect(spy).toHaveBeenCalledTimes(mockPlaylist1.songs.length) }) - it('playPlaylist should throw for non-existent ID', () => { expect(() => playPlaylist(mockNonExistentPlaylistId)).toThrow() }) - it('editPlaylist should add a song to a playlist when it is not initially there', () => { editPlaylist(mockPlaylist1.id, mockSong4.id) expect(player.playlists[0].songs).toEqual([ @@ -158,56 +137,46 @@ describe('Player Tests', () => { mockSong4.id, ]) }) - it('editPlaylist should remove a song from a playlist when it was initially there', () => { editPlaylist(mockPlaylist1.id, mockSong1.id) expect(player.playlists[0].songs).toEqual([mockSong2.id]) }) - it('editPlaylist should throw for non-existent playlist ID', () => { expect(() => editPlaylist(mockPlaylist2.id, mockSong1.id)).toThrow() }) - it('editPlaylist should throw for non-existent song ID', () => { expect(() => editPlaylist(mockPlaylist1.id, mockSong3.id)).toThrow() }) - it('editPlaylist should remove a playlist if it has been emptied of songs', () => { mockPlaylist1.songs.forEach((song) => editPlaylist(mockPlaylist1.id, song)) expect(player.playlists.length).toBe(0) }) - it('playlistDuration should return the sum of durations of all songs inside it', () => { expect(playlistDuration(mockPlaylist1.id)).toBe(mockPlaylist1Duration) }) - it('searchByQuery should be case-insensitive', () => { expect(searchByQuery('t')).toEqual({ songs: [mockSong1, mockSong4], playlists: [], }) }) - it('searchByQuery should consider all song attributes, and sort results alphanumerically', () => { expect(searchByQuery('ll')).toEqual({ songs: [mockSong2, mockSong1], playlists: [], }) }) - it('searchByQuery should find matching playlists', () => { expect(searchByQuery('Israeli')).toEqual({ songs: [], playlists: [mockPlaylist1], }) }) - it('searchByDuration should find the closest song', () => { expect(searchByDuration('04:23')).toEqual(mockSong1) expect(searchByDuration('04:27')).toEqual(mockSong2) }) - it('searchByDuration should find the closest playlist', () => { expect(searchByDuration('10:00')).toEqual(mockPlaylist1) }) -}) +}) \ No newline at end of file diff --git a/index.js b/index.js index 10f4784..49eccd5 100644 --- a/index.js +++ b/index.js @@ -48,50 +48,67 @@ 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)} | ${mmssDuration(song.duration)}.`); + } } - -function playSong(id) { - // your code here +const mmssDuration=(duration)=> { + let minutes = Math.floor(duration / 60); + let seconds = duration % 60; + if (minutes < 10) { + minutes = "0" + minutes; + } + if (seconds < 10) { + seconds = "0" + seconds; + } + return minutes.toString() + ':' + seconds.toString(); } +const getSongById= (id)=>{ + /*function getSongById(id) {*/ + for (let num in player.songs) { + if (player.songs[num].id === id){ + return player.songs[num]; + }} + throw new Error("No song was found"); + } + function playSong(id) { + if(getSongById(id)===undefined){ + throw " ${id} ID not exists"; + } + return player.playSong(getSongById(id)); +} function removeSong(id) { - // your code here } -function addSong(title, album, artist, duration, id) { - // your code here + +function addSong(title, album, artist, duration, id = Math.floor(Math.random() * 100) + 1){ + } function removePlaylist(id) { - // your code here } -function createPlaylist(name, id) { - // your code here -} +function createPlaylist(name, id ) { + + } + +//createPlaylist function playPlaylist(id) { - // your code here } function editPlaylist(playlistId, songId) { - // your code here + } function playlistDuration(id) { - // your code here } + function searchByQuery(query) { - // your code here } - function searchByDuration(duration) { - // your code here } - module.exports = { player, playSong, @@ -104,4 +121,4 @@ module.exports = { playlistDuration, searchByQuery, searchByDuration, -} +} \ No newline at end of file From d2fc3913fcff8bf69d232fc34b537ed5870f0218 Mon Sep 17 00:00:00 2001 From: MaorKurztag21 Date: Fri, 10 Sep 2021 04:48:19 +0300 Subject: [PATCH 02/13] Add "removeSong" function --- index.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/index.js b/index.js index 49eccd5..cb7dbb7 100644 --- a/index.js +++ b/index.js @@ -78,6 +78,16 @@ const getSongById= (id)=>{ return player.playSong(getSongById(id)); } function removeSong(id) { + if(getSongById(id)===undefined){ + throw new Error("${id} ID not valid"); + } +player.songs.splice(getSongById(id),1); + for(let num1 in player.playlists){ + for(let num2 in player.playlists[num1].songs){ + if(player.playlists[num1].songs[num2] ===id){ + player.playlists[num1].songs.splice(num2,1); + } + }} } From b1b02cf7d79eb0351fd72ed6ad987ee0d4f321b7 Mon Sep 17 00:00:00 2001 From: MaorKurztag21 Date: Fri, 10 Sep 2021 04:53:04 +0300 Subject: [PATCH 03/13] Add "addSong" function and 2 external External functions: "idExist" and "secondsFormat" --- index.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index cb7dbb7..153578d 100644 --- a/index.js +++ b/index.js @@ -89,10 +89,30 @@ player.songs.splice(getSongById(id),1); } }} } +const idExist=(id)=> { + for (let num in player.songs) { + if (player.songs[num].id === id) + return true; + } + return false; +} +const secondsFormat= (duration)=>{ + let sum = duration.split(":"); + let minute = parseInt(sum[0]) * 60; + let second = parseInt(sum[1]); + return minute + second; +} -function addSong(title, album, artist, duration, id = Math.floor(Math.random() * 100) + 1){ +function addSong(title, album, artist, duration, id = Math.floor(Math.random() * 100) + 1){ + if(idExist(id)){ + throw new Error("${id} ID already exists"); + } + else{ + player.songs.push({id: id, title: title,album: album,artist: artist,duration:secondsFormat(duration)}); + return id; + } } function removePlaylist(id) { From 85a0585d8a2a667edff67047b89b71789aab94ad Mon Sep 17 00:00:00 2001 From: MaorKurztag21 Date: Fri, 10 Sep 2021 04:56:09 +0300 Subject: [PATCH 04/13] Add "removePlaylist" and 1 external External function: "getPlaylistById" --- index.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 153578d..a2ffbd5 100644 --- a/index.js +++ b/index.js @@ -114,8 +114,20 @@ function addSong(title, album, artist, duration, id = Math.floor(Math.random() * return id; } } +const getPlaylistById=(id) =>{ + for (let num in player.playlists) { + if (player.playlists[num].id === id) + return player.playlists[num]; + } + throw new Error("${id} ID not exists"); +} function removePlaylist(id) { + if(getPlaylistById===undefined){ + throw new Error("${id} ID not exists"); + } + let playlistIndex = player.playlists.indexOf(getPlaylistById(id)); + player.playlists.splice(playlistIndex, 1); } @@ -123,7 +135,7 @@ function createPlaylist(name, id ) { } -//createPlaylist + function playPlaylist(id) { } From 9660452e951851c466060b6d7df223ababe3ed12 Mon Sep 17 00:00:00 2001 From: MaorKurztag21 Date: Fri, 10 Sep 2021 04:58:36 +0300 Subject: [PATCH 05/13] Add "createPlaylist" function and external External function: "playlistIdExist" --- index.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index a2ffbd5..c817bd0 100644 --- a/index.js +++ b/index.js @@ -129,10 +129,23 @@ function removePlaylist(id) { let playlistIndex = player.playlists.indexOf(getPlaylistById(id)); player.playlists.splice(playlistIndex, 1); } +const playlistIdExist= (id)=>{ + for (let i = 0; i < player.playlists.length; i++) { + if (player.playlists[i].id === id) + return true; + } + return false; +} +function createPlaylist(name, id = Math.floor(Math.random() * 100) + 1 ) { + if(playlistIdExist(id)){ + throw new Error("${id} ID already exists"); + } + else{ + player.playlists.push({id: id,name: name,songs:[]}); + return id; + } -function createPlaylist(name, id ) { - } From e4acae6ca78aa5e3e03423fa7390d80343a3ce7f Mon Sep 17 00:00:00 2001 From: MaorKurztag21 Date: Fri, 10 Sep 2021 04:59:47 +0300 Subject: [PATCH 06/13] Add "playPlaylist" function --- index.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/index.js b/index.js index c817bd0..0668ecd 100644 --- a/index.js +++ b/index.js @@ -150,6 +150,14 @@ function createPlaylist(name, id = Math.floor(Math.random() * 100) + 1 ) { function playPlaylist(id) { + if(getPlaylistById(id)===undefined){ + throw new error("${id} ID not exists"); + } + let playlist = getPlaylistById(id); + for(let num = 0; num < playlist.songs.length; num++){ + playSong(playlist.songs[num]); + } + } function editPlaylist(playlistId, songId) { From 39919070c612f1d9e50db9d8bd95c0797c342962 Mon Sep 17 00:00:00 2001 From: MaorKurztag21 Date: Fri, 10 Sep 2021 15:43:42 +0300 Subject: [PATCH 07/13] Add "editPlaylist" function and 1 external External function: "songInPlaylist" --- index.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 0668ecd..cc6d2c4 100644 --- a/index.js +++ b/index.js @@ -157,11 +157,33 @@ function playPlaylist(id) { for(let num = 0; num < playlist.songs.length; num++){ playSong(playlist.songs[num]); } - } - +const songInPlaylist=(songId, playlistId)=>{ + let playlist = getPlaylistById(playlistId); + for (let num in playlist.songs) { + if (playlist.songs[num] === songId) + return num; + } + return -1; +} function editPlaylist(playlistId, songId) { + let playlist = getPlaylistById(playlistId); + let songIn= songInPlaylist(songId, playlistId); + if(idExist(songId) && playlistIdExist(playlistId)){ + if(songIn === -1) { + playlist.songs.push(songId); + } + else if(playlist.songs.length > 1){ + playlist.songs.splice(songIn,1); + } + else{ + removePlaylist(playlist.id); + } + } + else{ + throw new Error("${id} ID not exists"); + } } function playlistDuration(id) { From 65680a44a7d02c2f9f315cfc53c696bced78d1f8 Mon Sep 17 00:00:00 2001 From: MaorKurztag21 Date: Fri, 10 Sep 2021 15:45:26 +0300 Subject: [PATCH 08/13] Add "playlistDuration" function --- index.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/index.js b/index.js index cc6d2c4..aee2849 100644 --- a/index.js +++ b/index.js @@ -187,6 +187,12 @@ function editPlaylist(playlistId, songId) { } function playlistDuration(id) { + let playlist = getPlaylistById(id); + let result = 0; + for(let num in playlist.songs){ + result+= getSongById(playlist.songs[num]).duration; + } + return result; } From 1f2bafa0fccd9cebaae6ccfee8b2a1ed9c1e87d0 Mon Sep 17 00:00:00 2001 From: MaorKurztag21 Date: Fri, 10 Sep 2021 15:46:51 +0300 Subject: [PATCH 09/13] Add "searchByQuery" function --- index.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/index.js b/index.js index aee2849..666cd41 100644 --- a/index.js +++ b/index.js @@ -197,6 +197,32 @@ function playlistDuration(id) { function searchByQuery(query) { + const newObj = { songs: [], playlists: [] } + let myQuery = query; + for (let i in player.songs) { + if (player.songs[i].album.includes(myQuery) || player.songs[i].artist.includes(myQuery) || player.songs[i].title.includes(myQuery)) + { + newObj.songs.push(player.songs[i]) + } + } + newObj.songs.sort(function (first,second){ + if (first.title < second.title){ + return -1; } + else if (first.title > second.title){ + return 1; } + return 0; + }) + for (let num in player.playlists) { + if (player.playlists[num].name.includes(myQuery)) { + newObj.playlists.push(player.playlists[num]) + newObj.playlists.sort((first,second) => { + if (first.name < second.name){ + return -1; + } + }) + }} + + return newObj } function searchByDuration(duration) { } From 61f1888e9e73508369eaea43969219268b0d72da Mon Sep 17 00:00:00 2001 From: MaorKurztag21 Date: Fri, 10 Sep 2021 15:48:02 +0300 Subject: [PATCH 10/13] Add "searchByDuration" function --- index.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/index.js b/index.js index 666cd41..0e2ac23 100644 --- a/index.js +++ b/index.js @@ -225,6 +225,24 @@ function searchByQuery(query) { return newObj } function searchByDuration(duration) { + let mmss = secondsFormat(duration); + let closestSong; + let bestTime = 1000; + for (let num in player.songs) { + if (Math.abs(mmss - player.songs[num].duration) < bestTime) { + bestTime = Math.abs(mmss - player.songs[num].duration); + closestSong = player.songs[num]; + } + } + for (let playlist of player.playlists) { + if (Math.abs(mmss - playlistDuration(playlist.id)) < bestTime) { + closestSong = playlist; + bestTime = Math.abs(mmss - playlistDuration(playlist.id)); + } + } + + return closestSong; + } module.exports = { player, From f0b35e166be5a50c1ee147b838b167f4471b21e2 Mon Sep 17 00:00:00 2001 From: MaorKurztag21 Date: Fri, 10 Sep 2021 17:26:57 +0300 Subject: [PATCH 11/13] Final improvement and order --- index.js | 167 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 85 insertions(+), 82 deletions(-) diff --git a/index.js b/index.js index 0e2ac23..8a26bfb 100644 --- a/index.js +++ b/index.js @@ -51,6 +51,7 @@ const player = { console.log(`Playing ${song.title} from ${song.album} by ${(song.artist)} | ${mmssDuration(song.duration)}.`); } } + const mmssDuration=(duration)=> { let minutes = Math.floor(duration / 60); let seconds = duration % 60; @@ -64,64 +65,67 @@ const mmssDuration=(duration)=> { } const getSongById= (id)=>{ - /*function getSongById(id) {*/ for (let num in player.songs) { if (player.songs[num].id === id){ return player.songs[num]; }} throw new Error("No song was found"); } - function playSong(id) { - if(getSongById(id)===undefined){ - throw " ${id} ID not exists"; - } - return player.playSong(getSongById(id)); + + function playSong(id){ + if(getSongById(id)===undefined){ + throw " ${id} ID not exists"; + } + return player.playSong(getSongById(id)); } -function removeSong(id) { + +function removeSong(id){ if(getSongById(id)===undefined){ throw new Error("${id} ID not valid"); } -player.songs.splice(getSongById(id),1); + player.songs.splice(getSongById(id),1); for(let num1 in player.playlists){ for(let num2 in player.playlists[num1].songs){ if(player.playlists[num1].songs[num2] ===id){ player.playlists[num1].songs.splice(num2,1); } - }} + } + } } -const idExist=(id)=> { + +const checkSong=(id)=> { for (let num in player.songs) { if (player.songs[num].id === id) return true; } return false; } + const secondsFormat= (duration)=>{ let sum = duration.split(":"); let minute = parseInt(sum[0]) * 60; let second = parseInt(sum[1]); - return minute + second; } - function addSong(title, album, artist, duration, id = Math.floor(Math.random() * 100) + 1){ - if(idExist(id)){ - throw new Error("${id} ID already exists"); + if(checkSong(id)){ + throw new Error("${id} ID exists"); } else{ player.songs.push({id: id, title: title,album: album,artist: artist,duration:secondsFormat(duration)}); return id; } } + const getPlaylistById=(id) =>{ for (let num in player.playlists) { if (player.playlists[num].id === id) return player.playlists[num]; } - throw new Error("${id} ID not exists"); } + function removePlaylist(id) { if(getPlaylistById===undefined){ throw new Error("${id} ID not exists"); @@ -129,25 +133,25 @@ function removePlaylist(id) { let playlistIndex = player.playlists.indexOf(getPlaylistById(id)); player.playlists.splice(playlistIndex, 1); } -const playlistIdExist= (id)=>{ - for (let i = 0; i < player.playlists.length; i++) { - if (player.playlists[i].id === id) + +const checkPlaylist= (id)=>{ + for (let num in player.playlists) { + return player.playlists[num].id === id? true: false; + /* if (player.playlists[num].id === id) return true; } - return false; -} + return false;*/ +}} function createPlaylist(name, id = Math.floor(Math.random() * 100) + 1 ) { - if(playlistIdExist(id)){ - throw new Error("${id} ID already exists"); + if(checkPlaylist(id)){ + throw new Error("${id} ID exists"); } else{ player.playlists.push({id: id,name: name,songs:[]}); return id; } - - } - +} function playPlaylist(id) { if(getPlaylistById(id)===undefined){ @@ -158,89 +162,88 @@ function playPlaylist(id) { playSong(playlist.songs[num]); } } + const songInPlaylist=(songId, playlistId)=>{ let playlist = getPlaylistById(playlistId); for (let num in playlist.songs) { - if (playlist.songs[num] === songId) - return num; + return playlist.songs[num] === songId? num:-1; } - return -1; } -function editPlaylist(playlistId, songId) { +function editPlaylist(playlistId, songId){ let playlist = getPlaylistById(playlistId); let songIn= songInPlaylist(songId, playlistId); - - if(idExist(songId) && playlistIdExist(playlistId)){ - if(songIn === -1) { - playlist.songs.push(songId); - } - else if(playlist.songs.length > 1){ - playlist.songs.splice(songIn,1); - } + if(checkSong(songId) && checkPlaylist(playlistId)){ + if(songIn === -1) { + playlist.songs.push(songId); + } + else if(playlist.songs.length > 1){ + playlist.songs.splice(songIn,1); + } else{ removePlaylist(playlist.id); - } - } - else{ - throw new Error("${id} ID not exists"); + } + } + else{ + throw new Error("${id} ID not exists"); } } -function playlistDuration(id) { +function playlistDuration(id){ let playlist = getPlaylistById(id); - let result = 0; - for(let num in playlist.songs){ - result+= getSongById(playlist.songs[num]).duration; - } + let result = 0; + for(let num in playlist.songs){ + result+= getSongById(playlist.songs[num]).duration; + } return result; } - function searchByQuery(query) { const newObj = { songs: [], playlists: [] } - let myQuery = query; - for (let i in player.songs) { - if (player.songs[i].album.includes(myQuery) || player.songs[i].artist.includes(myQuery) || player.songs[i].title.includes(myQuery)) - { - newObj.songs.push(player.songs[i]) - } - } - newObj.songs.sort(function (first,second){ - if (first.title < second.title){ - return -1; } - else if (first.title > second.title){ - return 1; } - return 0; - }) - for (let num in player.playlists) { - if (player.playlists[num].name.includes(myQuery)) { - newObj.playlists.push(player.playlists[num]) - newObj.playlists.sort((first,second) => { - if (first.name < second.name){ - return -1; - } - }) - }} - + let myQuery = query; + for (let i in player.songs) { + if (player.songs[i].album.includes(myQuery) || player.songs[i].artist.includes(myQuery) || player.songs[i].title.includes(myQuery)){ + newObj.songs.push(player.songs[i]) + } + } + newObj.songs.sort(function (first,second){ + if (first.title < second.title){ + return -1; + } + return first.title > second.title? 1:0; + /*else if (first.title > second.title){ + return 1; + } + return 0;*/ + }) + for (let num in player.playlists) { + if (player.playlists[num].name.includes(myQuery)) { + newObj.playlists.push(player.playlists[num]) + newObj.playlists.sort((first,second) => { + if (first.name < second.name){ + return -1; + } + }) + } + } return newObj } + function searchByDuration(duration) { let mmss = secondsFormat(duration); - let closestSong; - let bestTime = 1000; + let closestSong; + let bestTime = 100000; for (let num in player.songs) { - if (Math.abs(mmss - player.songs[num].duration) < bestTime) { - bestTime = Math.abs(mmss - player.songs[num].duration); - closestSong = player.songs[num]; - } + if (Math.abs(mmss - player.songs[num].duration) < bestTime) { + bestTime = Math.abs(mmss - player.songs[num].duration); + closestSong = player.songs[num]; + } } for (let playlist of player.playlists) { - if (Math.abs(mmss - playlistDuration(playlist.id)) < bestTime) { - closestSong = playlist; - bestTime = Math.abs(mmss - playlistDuration(playlist.id)); - } + if (Math.abs(mmss - playlistDuration(playlist.id)) < bestTime) { + closestSong = playlist; + bestTime = Math.abs(mmss - playlistDuration(playlist.id)); + } } - return closestSong; } From b9e38a43626db0928c1b1e842bb83997774cd058 Mon Sep 17 00:00:00 2001 From: MaorKurztag21 Date: Sun, 12 Sep 2021 00:58:12 +0300 Subject: [PATCH 12/13] Last improvment --- index.js | 329 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 166 insertions(+), 163 deletions(-) diff --git a/index.js b/index.js index 8a26bfb..6c32faf 100644 --- a/index.js +++ b/index.js @@ -51,109 +51,107 @@ const player = { console.log(`Playing ${song.title} from ${song.album} by ${(song.artist)} | ${mmssDuration(song.duration)}.`); } } - const mmssDuration=(duration)=> { - let minutes = Math.floor(duration / 60); - let seconds = duration % 60; - if (minutes < 10) { - minutes = "0" + minutes; - } - if (seconds < 10) { - seconds = "0" + seconds; + let minutes = Math.floor(duration / 60); + let seconds = duration % 60; + if (minutes < 10) { + minutes = "0" + minutes; + } + if (seconds < 10) { + seconds = "0" + seconds; + } + return minutes.toString() + ':' + seconds.toString(); } - return minutes.toString() + ':' + seconds.toString(); -} -const getSongById= (id)=>{ + const getSongById= (id)=>{ for (let num in player.songs) { if (player.songs[num].id === id){ return player.songs[num]; - }} + } + } throw new Error("No song was found"); } - function playSong(id){ - if(getSongById(id)===undefined){ + function playSong(id) { + if(getSongById(id)===undefined){ throw " ${id} ID not exists"; } - return player.playSong(getSongById(id)); -} - -function removeSong(id){ - if(getSongById(id)===undefined){ - throw new Error("${id} ID not valid"); + return player.playSong(getSongById(id)); } + + function removeSong(id) { + if(getSongById(id)===undefined){ + throw new Error("${id} ID not valid"); + } player.songs.splice(getSongById(id),1); - for(let num1 in player.playlists){ - for(let num2 in player.playlists[num1].songs){ - if(player.playlists[num1].songs[num2] ===id){ - player.playlists[num1].songs.splice(num2,1); - } + for(let num1 in player.playlists){ + for(let num2 in player.playlists[num1].songs){ + if(player.playlists[num1].songs[num2] ===id){ + player.playlists[num1].songs.splice(num2,1); + } + } } - } -} - -const checkSong=(id)=> { - for (let num in player.songs) { - if (player.songs[num].id === id) - return true; } - return false; -} - -const secondsFormat= (duration)=>{ - let sum = duration.split(":"); - let minute = parseInt(sum[0]) * 60; - let second = parseInt(sum[1]); - return minute + second; -} - -function addSong(title, album, artist, duration, id = Math.floor(Math.random() * 100) + 1){ - if(checkSong(id)){ - throw new Error("${id} ID exists"); - } - else{ - player.songs.push({id: id, title: title,album: album,artist: artist,duration:secondsFormat(duration)}); - return id; + + const idExist=(id)=> { + for (let num in player.songs) { + if (player.songs[num].id === id) + return true; + } + return false; } -} -const getPlaylistById=(id) =>{ - for (let num in player.playlists) { - if (player.playlists[num].id === id) - return player.playlists[num]; + const secondsFormat= (duration)=>{ + let sum = duration.split(":"); + let minute = parseInt(sum[0]) * 60; + let second = parseInt(sum[1]); + + return minute + second; } - throw new Error("${id} ID not exists"); -} - -function removePlaylist(id) { - if(getPlaylistById===undefined){ + + function addSong(title, album, artist, duration, id = Math.floor(Math.random() * 100) + 1){ + if(idExist(id)){ + throw new Error("${id} ID already exists"); + } + else{ + player.songs.push({id: id, title: title,album: album,artist: artist,duration:secondsFormat(duration)}); + return id; + } + } + + const getPlaylistById=(id) =>{ + for (let num in player.playlists) { + if (player.playlists[num].id === id) + return player.playlists[num]; + } throw new Error("${id} ID not exists"); } - let playlistIndex = player.playlists.indexOf(getPlaylistById(id)); - player.playlists.splice(playlistIndex, 1); -} -const checkPlaylist= (id)=>{ - for (let num in player.playlists) { - return player.playlists[num].id === id? true: false; - /* if (player.playlists[num].id === id) - return true; + function removePlaylist(id) { + if(getPlaylistById===undefined){ + throw new Error("${id} ID not exists"); + } + let playlistIndex = player.playlists.indexOf(getPlaylistById(id)); + player.playlists.splice(playlistIndex, 1); } - return false;*/ -}} -function createPlaylist(name, id = Math.floor(Math.random() * 100) + 1 ) { - if(checkPlaylist(id)){ - throw new Error("${id} ID exists"); - } - else{ - player.playlists.push({id: id,name: name,songs:[]}); - return id; + const playlistIdExist= (id)=>{ + for (let i in player.playlists) { + return player.playlists[i].id === id? true:false; + } } -} - -function playPlaylist(id) { + + function createPlaylist(name, id = Math.floor(Math.random() * 100) + 1) { + if(playlistIdExist(id)){ + throw new Error("${id} ID already exists"); + } + else{ + player.playlists.push({id: id,name: name,songs:[]}); + return id; + } + } + + function playPlaylist(id) { if(getPlaylistById(id)===undefined){ throw new error("${id} ID not exists"); } @@ -161,81 +159,86 @@ function playPlaylist(id) { for(let num = 0; num < playlist.songs.length; num++){ playSong(playlist.songs[num]); } -} - -const songInPlaylist=(songId, playlistId)=>{ - let playlist = getPlaylistById(playlistId); - for (let num in playlist.songs) { - return playlist.songs[num] === songId? num:-1; } -} -function editPlaylist(playlistId, songId){ - let playlist = getPlaylistById(playlistId); - let songIn= songInPlaylist(songId, playlistId); - if(checkSong(songId) && checkPlaylist(playlistId)){ - if(songIn === -1) { - playlist.songs.push(songId); - } - else if(playlist.songs.length > 1){ - playlist.songs.splice(songIn,1); - } - else{ - removePlaylist(playlist.id); + + const songInPlaylist=(songId, playlistId)=>{ + let playlist = getPlaylistById(playlistId); + for (let num in playlist.songs) { + return playlist.songs[num] === songId? num:-1; } - } - else{ - throw new Error("${id} ID not exists"); - } -} -function playlistDuration(id){ - let playlist = getPlaylistById(id); - let result = 0; - for(let num in playlist.songs){ - result+= getSongById(playlist.songs[num]).duration; } - return result; -} - -function searchByQuery(query) { - const newObj = { songs: [], playlists: [] } - let myQuery = query; - for (let i in player.songs) { - if (player.songs[i].album.includes(myQuery) || player.songs[i].artist.includes(myQuery) || player.songs[i].title.includes(myQuery)){ - newObj.songs.push(player.songs[i]) + function editPlaylist(playlistId, songId) { + let playlist = getPlaylistById(playlistId); + let songIn= songInPlaylist(songId, playlistId); + if(idExist(songId) && playlistIdExist(playlistId)){ + if(songIn === -1) { + playlist.songs.push(songId); + } + else if(playlist.songs.length > 1){ + playlist.songs.splice(songIn,1); + } + else{ + removePlaylist(playlist.id); + } + } + else{ + throw new Error("${id} ID not exists"); + } + } + + function playlistDuration(id) { + let playlist = getPlaylistById(id); + let result = 0; + for(let num in playlist.songs){ + result+= getSongById(playlist.songs[num]).duration; } - } - newObj.songs.sort(function (first,second){ - if (first.title < second.title){ - return -1; - } - return first.title > second.title? 1:0; - /*else if (first.title > second.title){ - return 1; + return result; } - return 0;*/ - }) - for (let num in player.playlists) { - if (player.playlists[num].name.includes(myQuery)) { - newObj.playlists.push(player.playlists[num]) - newObj.playlists.sort((first,second) => { - if (first.name < second.name){ - return -1; - } - }) - } - } - return newObj -} - -function searchByDuration(duration) { - let mmss = secondsFormat(duration); - let closestSong; - let bestTime = 100000; - for (let num in player.songs) { - if (Math.abs(mmss - player.songs[num].duration) < bestTime) { - bestTime = Math.abs(mmss - player.songs[num].duration); - closestSong = player.songs[num]; + + function searchByQuery(query) { + const results = { songs: [], playlists: [] } + let myQuery = query; + for (let i in player.songs) { + if (player.songs[i].album.includes(myQuery) || player.songs[i].artist.includes(myQuery) || player.songs[i].title.includes(myQuery)) + { + results.songs.push(player.songs[i]) + } + } + results.songs.sort(function (first,second){ + if (first.title < second.title){ + return -1; + } + return first.title>second.title? 1:0; + + }) + for (let num in player.playlists) { + if (player.playlists[num].name.includes(myQuery)) { + results.playlists.push(player.playlists[num]) + results.playlists.sort((first,second) => { + if (first.name < second.name){ + return -1; + } + }) + } + return results + } + } + + function searchByDuration(duration) { + let mmss = secondsFormat(duration); + let closestSong; + let bestTime; + if (mmss > player.songs[0].duration){ + bestTime = mmss; + } + else{ + bestTime = player.songs[0].duration; + } + for(let song of player.songs){ + if(Math.abs(mmss - song.duration) < bestTime){ + closestSong = song; + bestTime = Math.abs(mmss - song.duration); } } for (let playlist of player.playlists) { @@ -245,18 +248,18 @@ function searchByDuration(duration) { } } return closestSong; - -} -module.exports = { - player, - playSong, - removeSong, - addSong, - removePlaylist, - createPlaylist, - playPlaylist, - editPlaylist, - playlistDuration, - searchByQuery, - searchByDuration, -} \ No newline at end of file + } + + module.exports = { + player, + playSong, + removeSong, + addSong, + removePlaylist, + createPlaylist, + playPlaylist, + editPlaylist, + playlistDuration, + searchByQuery, + searchByDuration, + } \ No newline at end of file From 9875d5fb7267a5ad68265e16daf164a90aadb698 Mon Sep 17 00:00:00 2001 From: MaorKurztag21 <89572753+MaorKurztag21@users.noreply.github.com> Date: Sun, 12 Sep 2021 10:58:41 +0300 Subject: [PATCH 13/13] Create main.yml --- .github/workflows/main.yml | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..f937a06 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,36 @@ +# This is a basic workflow to help you get started with Actions + +name: CI + +# Controls when the workflow will run +on: + # Triggers the workflow on push or pull request events but only for the master branch + push: + branches: [ master ] + pull_request: + branches: [ master ] + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + # This workflow contains a single job called "build" + build: + # The type of runner that the job will run on + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - uses: actions/checkout@v2 + + # Runs a single command using the runners shell + - name: Run a one-line script + run: echo Hello, world! + + # Runs a set of commands using the runners shell + - name: Run a multi-line script + run: | + echo Add other actions to build, + echo test, and deploy your project.