Tell us what’s happening:
I just need to satisfy the last part of this challenge:
After updateRecords(2468, "tracks", "Free")
, tracks
should have "1999"
as the first element.
It appears that when my code runs, the .push method doesn’t push the value onto the array, but rather replaces the string all together. I tested and checked the output in my browser console to confirm this. Can anyone tell me why?
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) {
if(prop != "tracks" && value != ""){
collection[id][prop] = value;
}
if(prop == "tracks" && typeof(collection[id][prop] === undefined)){
collection[id][prop] = [value];
}else if(prop == "tracks"){
collection[id][prop].push(value);
}
if(value == ""){
delete collection[id][prop];
}
return collection;
}
updateRecords(5439, "artist", "ABBA");
CONSOLE OUTPUT:
updateRecords(2468, “tracks”, “Free”);
{…}
1245: Object { artist: “Robert Palmer”, tracks: }
2468: {…}
album: “1999”
artist: “Prince”
tracks: Array [ “Free” ]
: Object { … }
2548: Object { album: “Slippery When Wet”, artist: “Bon Jovi”, tracks: (2) […] }
5439: Object { album: “ABBA Gold” }
: {…
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0
.
Challenge: Record Collection
Link to the challenge: