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:
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”?
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
=== is strict equality - some says to always use that, and behave as if == doesn’t exist. == will behave weirdly depending on what you need to check
this uses a thing called short-circuit evaluation (in case you want to search it) a || b is evaluated in this way: first a is evaluated, if it’s truthy, it is what the whole expression evaluates to, if it’s falsy the whole expression evaluates to b
in the use case here, collection[id][prop] can be undefined (falsy) and so the expression is evaluated to the empty array, or an array with values (truthy) and the expression is evaluated to collection[id][prop]
you could do the same with a ternary operator collection[id][prop] = collection[id][prop] ? collection[id][prop] : []
or just with an if statement if (!collection[id][prop]) {collection[id][prop] = [];}
Strictly equal (===) is considered the better option than == because it avoids implicit type conversion. Very rarely will you find scenarios where == is appropriate. It will work fine in this challenge, but it’s considered less safe and if you are working on a team you will be asked to change == to === in code reviews.
This is just a convenient shorthand (aka “syntactic sugar”) that keeps collection[id][prop] the same if it is truthy or assigns an empty array to collection[id][prop] if it was falsy.
In programming we have a concept called “short circuiting”. For an OR (||) statement, if the as soon as one of the chunks is found to be truthy, it stops checking because the overall evaluation will be true. For an AND (&&) statement it will stop checking as soon as a falsy value is encountered. Both OR and AND evaluate from left to right.
a = true;
b = false;
if (a || b)... // the value of b doesn't matter and is not checked
if (b || a)... // the value of a is checked
if (a && b)... // the value of b is checked
if (b && a)... // the value of a doesn't matter and is not checked.