diff --git a/index.js b/index.js index 10f4784..d7bf5d5 100644 --- a/index.js +++ b/index.js @@ -48,48 +48,233 @@ 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 + + " | " + + durationFormat(song.duration) + +".") }, } function playSong(id) { - // your code here -} -function removeSong(id) { - // your code here + if (!(checkId(player.songs,id))) throw "ERROR: id doesn't exict."; + else for(let i = 0 ; i < player.songs.length ; i ++){ + if (player.songs[i].id === id) + return player.playSong(player.songs[i]); + } } -function addSong(title, album, artist, duration, id) { - // your code here +function durationFormat(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; + else return minutes+":"+seconds; } -function removePlaylist(id) { - // your code here +function checkId(songs,id){ + for (let i = 0 ; i < songs.length ; i ++){ + if (id === songs[i].id) + return true; + } + return false; } -function createPlaylist(name, id) { - // your code here +function playlistDuration(id) { + + let correctPlaylist = findPlaylistById(id) //correctPlaylist contain the wanted playlist + let save = 0, + sum = 0 + for (let i = 0; i < correctPlaylist.songs.length; i++) { + save = correctPlaylist.songs[i] + for (let j = 0; j < player.songs.length; j++) { + if (player.songs[j].id === save) sum += player.songs[j].duration + } + } + return sum } -function playPlaylist(id) { - // your code here +function findPlaylistById(id) { + let correctPlaylist + for (let i = 0; i < player.playlists.length; i++) { + if (id === player.playlists[i].id) correctPlaylist = player.playlists[i] + } + return correctPlaylist } function editPlaylist(playlistId, songId) { - // your code here + if (!checkId(player.playlists, playlistId)){ + throw "ID of the playlist doesn't exist." + } + if (!checkId(player.songs, songId)){ + throw "ID of the song doesn't exist." + } + for (let i = 0; i < player.playlists.length; i++) { + if (playlistId === player.playlists[i].id){ + for (let x = 0; x < player.playlists[i].songs.length; x++) { + if (songId === player.playlists[i].songs[x]){ + player.playlists[i].songs.splice(x,1); + } + else + player.playlists[i].songs.push(songId); + } + if (player.playlists[i].songs.length === 0) + removePlaylist(player.playlists[i].id); + } + } } -function playlistDuration(id) { - // your code here +function newId(arr){ + let max = arr[0]; + for (let i = 0; i < arr.length; i++) { + if (max < arr[i].id) + max = arr[i].id; + } + return max+1; +} + +function oppDuration(duration){ + duration = duration.split(':') + let minutes = parseInt(duration[0]) * 60 + let seconds = parseInt(duration[1]) + return minutes + seconds +} + +function addSong(title, album, artist, duration, id = newId(player.songs)) { + if (checkId(player.songs, id)){ + throw "ID exists" + } + duration= oppDuration(duration); + let newSong = {id: id, title: title, album: album, artist: artist, duration: duration}; + player.songs.push(newSong) + return id; +} + +function removeSong(id) { + if (!checkId(player.songs, id)){ + throw "ID doesn't exist."; + } + for (let i = 0; i { + if (a.name.toUpperCase() < b.name.toUpperCase()) return -1 + }) + } + } + for (let i = 0; i < player.songs.length; i++) { + if ( + player.songs[i].album.toUpperCase().includes(tempQuery) || + player.songs[i].artist.toUpperCase().includes(tempQuery) || + player.songs[i].title.toUpperCase().includes(tempQuery) + ) { + results.songs.push(player.songs[i]) + results.songs.sort((a, b) => { + if (a.title.toUpperCase() < b.title.toUpperCase()) return -1 + }) + } + } + return results } function searchByDuration(duration) { - // your code here + duration = oppDuration(duration) + let arrSongs = arrLengthSongs(duration) + let arrPlaylist = arrLengthPlaylist(duration) + return arrSongs[0] < arrPlaylist[0] ? arrSongs[1] : arrPlaylist[1] +} + +function arrLengthSongs(duration) { + let arr = [] + let minDuration = duration, + index = 0 + 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 arrLengthPlaylist(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 } module.exports = { @@ -104,4 +289,4 @@ module.exports = { playlistDuration, searchByQuery, searchByDuration, -} +}