you can’t use dot notation to access object properties with variables, dot notation will try to access the variable using that literal name. You need to use bracket notation to access object properties with variables.
tell us what was unclear, we can explain it in a different way (as many different ways as you need)
Thank you. I didn’t know that about dot notation. After changing the dot notation to bracket, it satisfies all conditions but two.
After updateRecords(collection, 5439, "tracks", "Take a Chance on Me"), tracks should have Take a Chance on Me as the last element.
After updateRecords(collection, 2468, "tracks", "Free"), tracks should have 1999 as the first element.
// tests completed
I also have doubt on the second if statement. I’m assuming object[id].hasOwnProperty("tracks") will return false, but what if it doesn’t? Is there a better way to do this?
function updateRecords(object, id, prop, value) {
if (prop !== "tracks" && value !== "") {
object[id][prop] = value;
}
if (prop === "tracks" && object[id].hasOwnProperty("tracks")) {
object[id][prop] = [];
object[id][prop].push(value);
}
if ( prop === "tracks" && value !== "") {
object[id][prop].push(value);
}
if (value === "") {
delete object[id][prop];
}
return object;
}
I also have doubt on the second if statement. I’m assuming object[id].hasOwnProperty("tracks") will return false, but what if it doesn’t? Is there a better way to do this?
I just checked the answers in the hint thread. And damn, this was solved with this single “!” before object[id].hasOwnProperty("tracks"). I understand it is clearing the doubt that I had (declaring specifically what to do when it DOES NOT return true). But we didn’t learn about using this in the curriculum…