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
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
214 changes: 198 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,49 +48,230 @@ const player = {
{ id: 5, name: 'Israeli', songs: [4, 5] },
],
playSong(song) {
console.log(/* your code here */)
},
const minutes = Math.floor(song.duration/60);
const minutesWithZero = minutes < 10 ? "0" + minutes : minutes;
console.log("Playing " + song.title + " from " + song.album + " by " + song.artist + " | " + minutesWithZero + ":" + song.duration%60 + ".");
}
}

function playSong(id) {
// your code here
const songs = player.songs;
for(let i=0; i<songs.length; i++)
{
if(id===songs[i].id)
{
player.playSong(songs[i]);
return;
}
}
throw new Error('Song id not found.');
}

function removeSong(id) {
// your code here
const songs = player.songs;
let isSongExist= false;
for(let i=0; i<songs.length; i++)
{
if(id===songs[i].id)
{
isSongExist=true;
songs.splice(i, 1);
}
}
if(!isSongExist) throw new Error('Song id not found.');

const playlists=player.playlists;
for(let j=0; j<playlists.length; j++)
{
const songIndex=playlists[j].songs.indexOf(id);
if(songIndex != -1)
{
playlists[j].songs.splice(songIndex,1)
}
}
}

function addSong(title, album, artist, duration, id) {
// your code here
let new_id = 0;
duration=duration.split(":");
duration= parseInt(duration[0]*60) + parseInt(duration[1]);
const songs = player.songs;
if(id)
{
for(let i=0; i<songs.length; i++)
{
if(id==songs[i].id)
{
throw new Error('ERROR: that id is taken.');
}
}
new_id= id;
}
else{
let max=1;
for(let j=0; j<songs.length; j++)
{
if(max<songs[j].id)
{
max=songs[j].id
}
}
new_id=max+1;
}
let newSong = {
id: new_id,
title: title,
album: album,
artist: artist,
duration: duration,
}
songs.push(newSong);
return new_id;
}

function removePlaylist(id) {
// your code here
const playlists=player.playlists;
let ifIdExist=false;
for(let i=0; i<playlists.length; i++)
{
if(playlists[i].id===id)
{
playlists.splice(i,1);
ifIdExist=true;
}

}
if(!ifIdExist) throw new Error('ERROR: that id is not found.');
}

function createPlaylist(name, id) {
// your code here
const playlists= player.playlists;
if(!id)
{
for(let i=0; i<playlists.length; i++)
{
if(Math.floor(Math.random())!==playlists[i].id)
{
id=Math.floor(Math.random());
}

}
let newPlaylist=
{ id: id, name: name, songs: [] };
playlists.push(newPlaylist);
return id;


}
}

function playPlaylist(id) {
// your code here
function playPlaylist(newId)
{
let yes=false;
const playlists=player.playlists;
for(j=0; j<playlists.length; j++)
{
if(newId===playlists[i].id)
{
yes=true;
}
}
for(let i=0; i<playlists[newId].songs.length; i++)
{
playSong(playlists[newId].songs[i]);
}
if(yes===false)
{
throw new Error('ERROR: that id is not found.');
}
}

function editPlaylist(playlistId, songId) {
// your code here
let songsList= [];
let currentPlaylist= {id: 0, name: '', songs: [0]};
currentPlaylist=null;
const playlists=player.playlists;
for(let i=0; i<playlists.length; i++)
{
if(playlists[i].id===playlistId)
{
currentPlaylist=playPlaylist[i];
songsList= playlists[i].songs;
break;
}
if(i==playlists.length-1)
{
throw new Error("Not found playlist");
return;
}
}
for(j=0; j<songsList.length; j++)
{
if(songId===songsList[j])
{
songsList.splice(j,1);
if(songsList.length===0)
{
playlists.splice(j,1);
}
return;
}
}
songsList.push(songId);
}

function playlistDuration(id) {
// your code here
function playlistDuration(playlistId) {
let totalDuration=0;
let songNow=0;
const playlists=player.playlists;
for(let j=0; j<playlists[playlistId].songs.length; j++)
{
songNow= playlists[playlistId].songs[j];
if(player.songs[id]===songNow)
{
totalDuration += player.songs[songNow].duration;
}
}
return totalDuration;
}

function searchByQuery(query) {
// your code here
let resultObjectSong = [
{
id: 0,
title: '',
album: '',
artist: '',
duration: 0,
},];
resultObjectSong.pop();
let resultObjectPlaylist =[
{ id: 0, name: '', songs: [0,1,2,3,4,5]},];
resultObjectPlaylist.pop();
for (let index = 0; index < player.songs.length; index++) {
if (player.songs[index].title.includes(query)) {
resultObjectSong.push(player.songs[index]);
}
else if (player.songs[index].album.includes(query)) {
resultObjectSong.push(player.songs[index]);
}
else if (player.songs[index].artist.includes(query)) {
resultObjectSong.push(player.songs[index]);
}
for (let j = 0; j < resultObjectSong.length; j++) {
player.playSong(resultObjectSong[j])
}
}
for (let t = 0; t < player.playlists.length; t++) {
if (player.playlists[t].name) {
}
}
}

function searchByDuration(duration) {
// your code here
}

function searchByDuration(duration)
{
}

module.exports = {
player,
Expand All @@ -105,3 +286,4 @@ module.exports = {
searchByQuery,
searchByDuration,
}

Loading