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 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 10f4784..d6d35d1 100644 --- a/index.js +++ b/index.js @@ -48,48 +48,255 @@ 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 playSong(id) { - // your code here + let song = getSongById(id); + + return player.playSong(song); } 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 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) { - // your code here + let playlist = getPlaylistById(id) + let sum = 0 + + for(let i=0; i 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) { - // 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 +} + +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 +} + +function repeatSong(repeats,songId){ + for(let i=1; i<=repeats; i++){ + playSong(songId) + } +} + +function repeatPlaylist(repeats,playlistId){ + for(let i=1; i<=repeats; i++){ + playPlaylist(playlistId) + } } module.exports = {