Hello guys ! Completely stuck at this level ; completely new with JS.
Don’t understand why it’s not working. I might be of course completely wrong with the syntax
Blockquote
// Setup
var recordCollection = {
2548: {
albumTitle: 'Slippery When Wet',
artist: 'Bon Jovi',
tracks: ['Let It Rock', 'You Give Love a Bad Name']
},
2468: {
albumTitle: '1999',
artist: 'Prince',
tracks: ['1999', 'Little Red Corvette']
},
1245: {
artist: 'Robert Palmer',
tracks: []
},
5439: {
albumTitle: 'ABBA Gold'
}
};
// Only change code below this line
function updateRecords(records, id, prop, value) {
return records;
if (value === '') delete records[id][prop];
else if (prop === 'tracks') || (prop === 'artist') || (prop === 'albumTitle'){
records[id][prop] = records[id][prop] || []; }// this is called shortcircuit evaluation, see below for explanation
records[id][prop].push(value);
}
return records;
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
updateRecords(recordCollection, 5439, 'tracks', 'Take a Chance on Me');
updateRecords(recordCollection, 1245, 'tracks', 'Addicted to Love');
updateRecords(recordCollection, 1245, 'albumTitle', 'Riptide');
If someone would explain me, I would appreciate. Have a nice code. Thks Beatrice
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
// Setup
var recordCollection = {
2548: {
albumTitle: 'Slippery When Wet',
artist: 'Bon Jovi',
tracks: ['Let It Rock', 'You Give Love a Bad Name']
},
2468: {
albumTitle: '1999',
artist: 'Prince',
tracks: ['1999', 'Little Red Corvette']
},
1245: {
artist: 'Robert Palmer',
tracks: []
},
5439: {
albumTitle: 'ABBA Gold'
}
};
// Only change code below this line
function updateRecords(records, id, prop, value) {
//Ariel pointed this out, but nothing can happen
//after a return in a function
return records;
if (value === '') delete records[id][prop];
//the else if is wrapped incorrectly causing a
//syntax error
else if (prop === 'tracks') || (prop === 'artist') || (prop === 'albumTitle'){
// this is called shortcircuit evaluation, see below for explanation
records[id][prop] = records[id][prop] || [];
//In the case of || short circuiting the evaluation stops
//once a truthy value is hit or it continues all
//the way to the end if non are present
}
records[id][prop].push(value);
}
//this return is another syntax error a it is outside
//a function
return records;
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
updateRecords(recordCollection, 5439, 'tracks', 'Take a Chance on Me');
updateRecords(recordCollection, 1245, 'tracks', 'Addicted to Love');
updateRecords(recordCollection, 1245, 'albumTitle', 'Riptide');
I commented out your code to explain some thing that are wrong with it.