.
GIVEN the collection
object as shown, and
GIVEN you have three variables id
, prop
and value
that are the arguments to the function, and
GIVEN you can do lookups on the object by doing collection[id].prop
,
THEN
If prop isn’t “tracks” and value isn’t empty (“”), update or set the value for that record album’s property.
IF prop is not "tracks" AND value is not "" THEN
SET collection[id].prop TO value
ENDIF
If prop is “tracks” but the album doesn’t have a “tracks” property, create an empty array before adding the new value to the album’s corresponding property.
If prop is “tracks” and value isn’t empty (“”), push the value onto the end of the album’s existing tracks array
IF prop is "tracks" AND the collection does not have a property "tracks" THEN
SET collection[id].tracks to []
PUSH value TO collection[id].tracks
ENDIF
IF prop is "tracks" AND the collection has a property "tracks" THEN
PUSH value TO collection[id].tracks
ENDIF
If value is empty (“”), delete the given prop property from the album.
IF value is "" THEN
DELETE collection[id].prop
ENDIF
Then
RETURN collection
So, translating that pseudocode to JS:
function updateRecords(id, prop, value) {
if (prop !== "tracks" && value !== "") {
collection[id].prop = value;
} else if (prop === "tracks" && collection.hasOwnProperty("tracks") === false) {
collection[id].prop = [];
collection[id].prop.push(value);
} else if (prop === "tracks" && collection.hasOwnProperty("tracks") === true) {
collection[id].prop.push(value);
} else if (value === "") {
delete collection[id].prop;
}
return collection;
}
That is just the instructions, in the order they were written, translated to JavaScript.
There is a flaw in the logic, which is important. If value
is “”, then the property gets deleted, but if prop
is “tracks”, I don’t check that, so the function could fail to work: the tracks logic will happen before I get a chance to delete. So then I need to change the order (putting that condition first should cause all the tests to pass), or nest the if statements. There is also duplication, (which is not hugely important) but can be cleaned up. And you get to something close to that solution you posted
Edit: also, I used a function that JS provides called hasOwnProperty
. This returns true or false if an object has the property you pass to the function or not. There’s a hint for what to Google for in the description anyway:
…but the album doesn’t have a “tracks” property,
& you could Google “check if a javascript object has a property” and most of the results would mention hasOwnProperty
If you mean this:
if (collection[id][prop])
So anything in those brackets in an if statement JS will convert to either true
or false
. And if the prop is not in the collection, it’s value will be undefined. And when JS convert that, it converts to false. Otherwise it will be true, and the code inside that block will run.
Note this is not a great way to check things: it’s very easy to accidentally convert a value to false when you just wanted to check it was true and vice versa IMO always try to do something in the if statement that you know always either returns true or false (like using ===
or >
or <
or whatever).