This is from “record collection”. FCC isn’t letting me ask from the “Get Help” button for some reason.
This code is from the 3rd solution. Can anyone explain this line? It seems to say if album[“tracks”] exists, continue on the next line and if album[“tracks”] doesn’t exist, create an empty array and continue on the next line.
But from what I have understood about || so far is that if either side is true it will run the next line of code. But in this case it seems to be actually doing something rather than just giving you a true or false answer, which is throwing me off. The full solution is pasted below. Thanks
album["tracks"] = album["tracks"] || [];
Full Solution:
function updateRecords(records, id, prop, value) {
// Access target album in record collection
const album = records[id];
if (value === "") {
delete album[prop];
} else if (prop !== "tracks") {
album[prop] = value;
} else {
album["tracks"] = album["tracks"] || [];
album["tracks"].push(value);
}
return records;
}
Still not getting it. In one of the test cases, album[“tracks”] doesn’t exist, so neither album[“tracks”] nor empty array can be true. So how is this working in that case?
The short-circuit OR evaluation doesn’t really care if the value on the right is true or false, or whatever. It only tests the value on the left. If that is “truthy” then it returns it and it never even touches the value on the right (that’s the short-circuit). But if the value on the left is “falsey” then it returns the value on the right, regardless of what its value is.
So is it basically saying: if album[“tracks”] doesn’t exist (isn’t true), create it and give it a value of empty array? One of the tests doesn’t have an album[“tracks”] property, and this line seems to create one and give it the value of an empty array, somehow.
Pretty close. It’s really saying: If album["tracks"] doesn’t have a “truthy” value then set it to an empty array.
The “truthy” is important here. If album["tracks"] has a value of 0 then it will be “falsey” and it will get set to an empty array. Not that it would have a value of 0 in this particular challenge. It’s just something you need to be aware of in general.
One of the test cases doesn’t have an album[“tracks”] property to set to an array. Is this statement creating that property and then giving that property that it created the value of an empty array? Or am I missing something?