Record Collection
Problem Explanation
This problem is hard if you are new to JavaScript or programming, so don’t worry if you get stuck or need to ask for help. Asking for help is better than looking at the answer.
It is important to understand the overall goal of this function and then write code that supports that goal.
The id parameter corresponds to a specific album object in the records object.
You need to update this album object based upon the value and prop parameters:
- Your function must always return the entire
recordsobject. - If
valueis an empty string, delete the givenpropproperty from the album. - If
propisn’ttracksandvalueisn’t an empty string, assign thevalueto that album’sprop. - If
propistracksand value isn’t an empty string, add thevalueto the end of the album’stracksarray. You need to create this array first if the album does not have atracksproperty.
Finally, the problem wants you to return the records object.
Hints
Hint 1
Use an if...else if chain to check the different values of prop and value.
Hint 2
To access the value of a key in this object, you will use bracket notation: records[id][prop].
Hint 3
You can’t push to an array that doesn’t exist. If the "tracks" array does not exist yet, you need to create the "tracks" array before pushing value onto it.
Solutions
Solution 1 (Click to Show/Hide)
function updateRecords(records, id, prop, value) {
if (value === "") {
delete records[id][prop];
} else if (prop !== "tracks" && value !== "") {
records[id][prop] = value;
} else if (prop === "tracks" && value !== "") {
if (records[id].hasOwnProperty("tracks") === false) {
records[id][prop] = [];
}
records[id][prop].push(value);
}
return records;
}
Code Explanation
This version of the code explicitly handles every possible case with separate if clauses.
- First it checks if the
valueis an empty string. If it is, then thepropis deleted. - Then, if
propis not"tracks"and thevalueis not an empty string. Thepropis set to thevalue. - If that check doesn’t pass, it next checks if
propis equal totracks, thevalueis not an empty string, and the record does not have a tracks array. The"tracks"array is initialized with the only contents beingvalue. - It next checks if
propis equal totracks, thevalueis not an empty string. The"tracks"array must exist because the case above was not true. Thevalueis pushed onto the end of the"tracks"array.
Lastly, the entire records object is returned.
Solution 2 (Click to Show/Hide)
function updateRecords(records, id, prop, value) {
if (value === '') {
delete records[id][prop];
} else if (prop === "tracks") {
records[id][prop] = records[id][prop] || []; // shortcircuit evaluation
records[id][prop].push(value);
} else {
records[id][prop] = value;
}
return records;
}
Code Explanation
This solution re-orders the code to make the if clauses simpler.
- First checks if
valueis a blank string. If so, then the key (prop) is removed from the object. - If that first check doesn’t pass, then we know that
valueis not a blank string. It next checks ifpropis equal to"tracks". The"tracks"array is initialized if it does not exist, and thenvalueis pushed into thetracksarray. (This step uses shortcircuit evaluation) - If both these checks fail (meaning
valueis not an empty string andpropis not"tracks"), then either a new key (prop) and value (value) are added to the object, or an existing key is updated if thepropalready exists.
Lastly, the entire records object is returned.
Example Run
updateRecords(5439, "artist", "ABBA");runs.valueis not a blank string, so the first condition of theelse ifstatement fails.propis equal to"artist", not"tracks", so the second condition of theelse ifstatement fails.- in the ‘else’ clause,
artist: "ABBA"is added to the5439id.
Relevant Links
- fCC’s challenge: Accessing Objects Properties with Bracket Notation
- fCC’s challenge: Add New Properties to a JavaScript Object
- fCC’s challenge: Delete Properties from a JavaScript Object
- fCC’s challenge: Accessing Nested Objects
- “Array.prototype.push()” - MDN JavaScript reference
- “delete operator” - MDN JavaScript reference
- shortcircuit evaluation
Solution 3 (Click to Show/Hide)
function updateRecords(records, id, prop, value) {
// Access target album in record collection
const album = records[id];
// Update the album
if (value === "") {
delete album[prop];
} else if (prop !== "tracks") {
album[prop] = value;
} else {
album["tracks"] = album["tracks"] || [];
album["tracks"].push(value);
}
// Return the full collection
return records;
}
Code Explanation
This solution uses the fact that objects are passed into functions as references to slightly simplify the solution syntax.