Tell us what’s happening:
The following occurs when I run the test:
After updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me")
, tracks
should have the string Take a Chance on Me
as the last and only element.
But I am unsure why this is the case since I’d thought that .push()
method would always add an argument to the end of the targeted array.
I’d greatly appreciate any advice or suggestions, so thank you in advance!
Edit: Changed records[id].hasOwnProperty["tracks"]
to records[id].hasOwnProperty("tracks")
because .hasOwnProperty()
uses parentheses.
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 is an empty string, delete the given prop property from the album.
if (value === "") {
delete records[id][prop];
}
//If prop isn't tracks and value isn't an empty string, assign the value to that album's prop.
else if (prop !== "tracks" && value !== "") {
records[id][prop] = value;
}
//If prop is tracks and value isn't an empty string, you need to update the album's tracks array.
else if (prop === "tracks" && value !== "") {
// First, if the album does not have a tracks property, assign it an empty array.
if (records[id].hasOwnProperty("tracks") === "") {
records[id][prop] = [];
}
//Then add the value as the last item in the album's tracks array.
records[id][prop].push(value);
}
//Your function must always return the entire records object.
return records;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Challenge Information:
Basic JavaScript - Record Collection