diff --git a/index.js b/index.js index 10f4784..fc7fbbe 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +"use strict" const player = { songs: [ { @@ -48,50 +49,207 @@ 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} | ` + +((Math.floor(song.duration/60))<10? "0": "")+`${Math.floor(song.duration/60)}:` +((song.duration%60)<10? "0": "")+`${song.duration%60}.`) + } } -function playSong(id) { - // your code here +function findSong(id){ + for(let song of player.songs){ + if (song.id===id){ + return song; + } + } + throw "ID not found"; +} + +function findPlaylist(id){ + for(let Playlist of player.playlists){ + if (Playlist.id===id){ + return Playlist; + } + } + throw "ID not found"; +} + +function playSong(id,auto=1) { + player.playSong(findSong(id)); + if(auto>1) playSong(player.songs[Math.floor(Math.random() * player.songs.length)].id,auto-1) } function removeSong(id) { - // your code here + player.songs.splice(player.songs.indexOf(findSong(id)),1); + for(let playlist of player.playlists){ + playlist.songs.splice(playlist.songs.indexOf(id),1) + } +} + +let newSongId=10; +function generateSongId(){ + newSongId+=1; + return newSongId; +} + +function durationToSeconds(duration){ + checkDurationInput(duration) + return((parseInt(duration[0])*10)+parseInt(duration[1]))*60 + +parseInt(duration[3]*10)+parseInt(duration[4]) +} + +function checkDurationInput(duration){ // checks digits(maximum is 59:59/minimum is 00:00),:,length. + if(0<=parseInt(duration[0])&&parseInt(duration[0])<6&&0<=parseInt(duration[1])&&parseInt(duration[1])<=9 + &&duration[2]===":"&&0<=parseInt(duration[3])&&parseInt(duration[3])<6&&0<=parseInt(duration[4]) + &&parseInt(duration[4])<=9&&duration.length===5){ + return true + }else throw 'duration is not in the correct format...(this is the format-"00:00")' } -function addSong(title, album, artist, duration, id) { - // your code here +function addSong(title, album, artist, duration, id=generateSongId()) { + try{ + findSong(id) + }catch(err) { + player.songs.push({"id":id, + "title": title, + "album": album, + "artist": artist, + "duration": durationToSeconds(duration), + }) + return id; + } + throw "ID taken" } function removePlaylist(id) { - // your code here + player.playlists.splice(player.playlists.indexOf(findPlaylist(id)),1); } -function createPlaylist(name, id) { - // your code here +let newPlaylistId=3 +function generatePlaylistId(){ + newPlaylistId+=1 + return newPlaylistId +} + +function createPlaylist(name, id=generatePlaylistId()) { + try{ + findPlaylist(id) + }catch(err) { + player.playlists.push({ "id":id , "name": name, "songs": [] }) + return id; + } + throw "ID taken" } function playPlaylist(id) { - // your code here + for(let songId of findPlaylist(id).songs){ + playSong(songId) + } } -function editPlaylist(playlistId, songId) { - // your code here +function editPlaylist(PlaylistId, songId) { // to check readability!! + let playlistIndex=player.playlists.indexOf(findPlaylist(PlaylistId)); + findSong(songId); + if(player.playlists[playlistIndex].songs.indexOf(songId)===(-1)){ + player.playlists[playlistIndex].songs.push(songId); + }else{ + player.playlists[playlistIndex].songs.splice(player.playlists[playlistIndex].songs.indexOf(songId),1); + } + if(player.playlists[playlistIndex].songs.length===0){ + removePlaylist(PlaylistId); + } } function playlistDuration(id) { - // your code here + let plDuration=0; + for(let songID of findPlaylist(id).songs){ + plDuration+=(findSong(songID).duration); + } + return (plDuration); } function searchByQuery(query) { - // your code here + const QUERY=query.toUpperCase(); + const queryObj={"songs":[],"playlists":[]} + for(let song of player.songs) { + if(song.title.toUpperCase().includes(QUERY) + ||song.artist.toUpperCase().includes(QUERY) + ||song.album.toUpperCase().includes(QUERY)) + { + queryObj.songs.push(song); + } + } + for(let playlist of player.playlists){ + if(playlist.name.toUpperCase().includes(QUERY)){ + queryObj.playlists.push(playlist); + } + } + queryObj.songs.sort(sortByTitle); + queryObj.playlists.sort(sortByName); + return queryObj; +} + +function sortByTitle(a, b) { + let titleA = a.title.toUpperCase(); + let titleB = b.title.toUpperCase(); + if (titleA < titleB) { + return -1; + } + if (titleA > titleB) { + return 1; + } +} + +function sortByName(a, b) { + let nameA = a.name.toUpperCase(); + let nameB = b.name.toUpperCase(); + if (nameA < nameB) { + return -1; + } + if (nameA > nameB) { + return 1; + } } function searchByDuration(duration) { - // your code here + checkDurationInput(duration); + let sec=durationToSeconds(duration); + let allDurationArr=[]; + let matchingArr=[]; //matching array that saves the song and playlist parallel(they will have the same index) to the duration array(allDurationArr) + let closestDuration=player.songs[0].duration; + for(let song of player.songs){ + allDurationArr.push(song.duration); + matchingArr.push(song); + } + for(let playlist of player.playlists){ + allDurationArr.push(playlistDuration(playlist.id)); + matchingArr.push(playlist); + } + for(let dur of allDurationArr){ + if(Math.abs(dur-sec)