Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
__tests__
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"cSpell.words": [
"Godtfolk",
"Jinjer",
"Sabaton",
"Shiroyama",
"Songleikr",
"Vinda"
]
}
233 changes: 220 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<player.playlists.length; i++){ //iterate playlists
for(let j=0; j<player.playlists[i].songs.length; j++){ //iterate songs id in playlist
if(player.playlists[i].songs[j] == id){
player.playlists[i].songs.splice(j,1);
}
}
}
}

function addSong(title, album, artist, duration, id) {
// your code here
function addSong(title, album, artist, duration, id = Math.floor(Math.random() * 1000) + 1) {
if(!songIdExist(id)){
player.songs.push({id: id,
title: title,
album: album,
artist: artist,
duration: convertToSeconds(duration)})
return id
}
else{
throw new Error("This ID already exists")
}
}

function removePlaylist(id) {
// your code here
let playlistIndex = player.playlists.indexOf(getPlaylistById(id))
player.playlists.splice(playlistIndex, 1);
}

function createPlaylist(name, id) {
// your code here
function createPlaylist(name, id = Math.floor(Math.random() * 1000) + 1) {
if (!playlistIdExist(id)){
player.playlists.push({id: id,
name: name,
songs:[]});
return id;
}
else{
throw new Error("This ID already exists")
}
}

function playPlaylist(id) {
// your code here
let playlist = getPlaylistById(id);
for(let i=0; i<playlist.songs.length; i++){
playSong(playlist.songs[i])
}
}

function editPlaylist(playlistId, songId) {
// your code here
let songIndex = findSongInPlaylist(songId, playlistId)
let playlist = getPlaylistById(playlistId)

if(songIdExist(songId) && playlistIdExist(playlistId)){ //checks if both song and playlist exist
if(songIndex == -1) { //checks if the song is not in the list
playlist.songs.push(songId);
}
else if(playlist.songs.length > 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<playlist.songs.length; i++){
sum += getSongById(playlist.songs[i]).duration
}
return sum
}

function searchByQuery(query) {
// your code here
let results={
songs :[],
playlists : []
}

//search for songs containing query
for(let song of player.songs){
if(song.title.includes(query) || song.album.includes(query) || song.artist.includes(query)){
results.songs.push(song)
}
}
//sort songs array by title
results.songs.sort(function (a, b) {
if (a.title < b.title){
return -1; }
else if (a.title > 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 = {
Expand Down