Tell us what’s happening:
I can not figure out what I am doing wrong . I have tried a few variations of my code, but I seem to keep getting the same failures
Your code so far
// 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 === "") {
return delete records[id][prop]
}
else if (prop !== 'tracks' && value !== "") {
return records[id][prop] = value
}
else if (records[id].hasOwnProperty('tracks') === false && prop === 'tracks') {
return records[id][prop] = [value]
}
else if (prop === 'tracks' && value !== "") {
return records[id][prop].push(value)
}
return records;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
This is one of the most difficult JavaScript lessons I encountered.
I read where many people have difficulty with it.
Once you get it, you will feel the great accomplishment of success.
Ok. So I took all of the return keywords away from the if/else statement and left the last one “return records” at the end of the function.
That worked, but I do not fully understand why. Could you help me understand why taking the return keyword from my if/else statements made it work please?
Tests are expecting the whole object to be returned. However each of the return in the if/else was not returning the whole object, but whatever the expression by the return was evaluating to.
For example, the push method returns the new length of the array, so return records[id][prop].push(value) was returning integer, instead of the expected object.