Hi, I’ve been trying to do Record Collection for a couple hours. My solution turned out very similar to all the other, actually working solutions, but it does’t work. I would appreciate if someone have helped me understand what I’m doing wrong,
// Setup
const 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) {
if (value=='') {
delete records[id][prop];
return records;
}
else if (prop!='tracks' && value!='') {
records[id][prop]=value;
return records;
}
else if (prop=='tracks' && hasOwnProperty(records[id])==false) {
records[id][prop]=[value];
return records;
}
else if (prop=='tracks' && value!='') {
records[id][prop].push(value);
return records;
}
return records;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
Hi. I have a question. I have answered this step employing if/else, however I wanted to also try using switch because I wanted to learn both before I proceed further. However, I can’t seem to get it right. I’ve rewritten my if/else code to almost the same exact code apart from using switch cases, but somehow it doesn’t work. I figured I might’ve botched some syntax. Can you please take a look and tell me where I might’ve made a mistake:
function updateRecords(records, id, prop, value) {
switch (records, id, prop, value) {
case prop != "tracks" && value != "":
records[id][prop] = value;
case prop == "tracks" && records[id].hasOwnProperty(prop) == false:
records[id][prop] = [];
records[id][prop].push(value);
case prop == "tracks" && value != "":
record[id][prop].push(value);
case value == "":
delete records[id][prop];
}
return records;
}
A switch is not a general purpose replacement for an if-else statement.
In a switch you match one variable against multiple possible values. You could do that here, but it won’t be one switch covering all of your current if else options.