I created an array and I have to ask the user for new data, and then I have to add this new information in to my original array, but I'm having problems when I trying to add the new songs written by the user.
How can i add the new songs (songName and duration) to my original array?
this is my code:
let discos = [];
let disco1 = {
discoName: 'Disco Name ',
band: 'Catupecumachu',
code: 1,
songs: [
{
'songName': 'song1',
'duration': 200,
},
{
'songName': 'song2',
'duration': 180,
},
{
'songName': 'song3',
'duration': 90,
},
],
};
let disco2 = {
discoName: 'Disco Name',
band: 'U2',
code: 2,
songs: [
{
'songName': 'song1',
'duration': 200,
},
{
'songName': 'song2',
'duration': 180,
},
{
'songName': 'song3',
'duration': 90,
},
],
};
let discoUser = {
discoName: '',
band: '',
code: '',
songs: [
{
'songName': '',
'duration': '',
},
],
};
discos.push(disco1, disco2, discoUser);
do{
discoUser['discoName'] = prompt('discoName');
discoUser['band'] = prompt('band');
discoUser['code'] = prompt('code');
}while(confirm('Continue?'));
Following your code example, you could try this after the last prompt line you have :
const songName = prompt('song name');
const duration = prompt('duration');
discoUser.songs.push({songName,duration});
Just updating to show this tested on the developer tools console :
