I can’t figure out why but my .push() is incorrect. The rest of the code passed when I ran it but this won’t work. I ran it in vscode and it deletes all the tracks and replaces it with the track value that you call the function with.
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 (value == ""){
delete collection[id][prop]
}else if (prop !== "" && value !== ""){
collection[id][prop] = value;
} else if (prop == "tracks" && value !== ""){
collection[id][prop] = collection[id][prop] || [] ;
collection[id][prop].push(value);
}
return collection;
}
updateRecords(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/83.0.4103.106 Safari/537.36.
This is your problem. Your third block will never execute because “tracks” is not an empty string.
As an aside, you never need the value !== "" that you have because you are checking for that in the initial if. None of your elses will execute if value is an empty string.
So basically I have to check if property is ‘track’ first before checking if property is not an empty string because the function will count it as not an empty string first in the original code?
You can either check if prop is “tracks” first and then else the logic for the more general case or you could nest an if (prop === "tracks") in the block that you had before it. The key here is the else. An else condition will only be checked if the preceding conditions are false.