After many days of pulling my hair out, I apparently solved the challenge with the following:
function updateRecords(id, prop, value) {
if (value==""){delete collection[id][prop];}
else if (collection[id].hasOwnProperty("tracks")&&prop=="tracks"){collection[id][prop].push(value)}
else if (prop=="tracks"){collection[id][prop]=[value]}
else collection[id][prop]=value;
return collection;
}
But I am still confused about the solution provided by fCC. They use:
function updateRecords(id, prop, value) {
if(value === "") delete collection[id][prop];
else if(prop === "tracks") {
collection[id][prop] = collection[id][prop] || [ ];
collection[id][prop].push(value);
} else {
collection[id][prop] = value;
}
return collection;
}
First, why do they use “===” instead of “==”?
Second, I am confused about the line:
collection[id][prop] = collection[id][prop] || [ ];
I assume this is to handle the cases both where there is already a “tracks” property and where there is not.
I assume that the “collection[id][prop] = collection[id][prop]” part somehow handles the case where there already is a “tracks” property with an array with members (although I don’t know how that works, since fCC hasn’t explained that (ahem!)).
In ordinary language (and I know JS isn’t ordinary language) if someone says “do A or B” that doesn’t tell you what to do. So how does the console know not to set “tracks” to "[]"
if there already is an array with members as the value of “tracks”?