Tell us what’s happening:
While working on the code below I continue to get the message “TypeError: Cannot create property ‘tracks’ on number ‘5439’”. I am not sure what I am doing wrong while trying to create the property. Can anyone please offer advice?
Your code so far
// Setup
var collection = {
2548: {
album: "Slippery When Wet",
artist: "Bon Jovi",
tracks: [
"Let It Rock",
"You Give Love a Bad Name"
]
},
2468: {
album: "1999",
artist: "Prince",
tracks: [
"1999",
"Little Red Corvette"
]
},
1245: {
artist: "Robert Palmer",
tracks: [ ]
},
5439: {
album: "ABBA Gold"
}
};
// Only change code below this line
function updateRecords(id, prop, value) {
//validate tracks is a property
if(id.hasOwnProperty('tracks') !== true){
id['tracks'] = [''];
}
//Add value to property
else if(value != '' && id.hasOwnProperty('tracks')){
id['tracks'].push(value);
}
//if value is empty, delete prop
if(id.prop.value == ''){
delete id[prop];
}
return collection;
}
updateRecords(5439, "artist", "ABBA");
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36
.
Challenge: Record Collection
Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection