Checkpoint: Record Collection
Instructions
You are given a JSON object representing (a small part of) your record collection. Each album is identified by a unique id number and has several properties. Not all albums have complete information.
Write a function which takes an id, a property (prop), and a value.
For the given id in collection:
If value is non-blank (value !== ""), then update or set the value for the prop.
If the prop is "tracks" and value is non-blank, push the value onto the end of the tracks array.
If value is blank, delete that prop.
Always return the entire collection object.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Useful Links
- Challenge: Accessing Objects Properties with Bracket Notation
- Challenge: Add New Properties to a JavaScript Object
- Challenge: Delete Properties from a JavaScript Object
- Challenge: Accessing Nested Objects in JSON
Problem Explanation:
- Change the code below
// Only change code below this line
and up to// Alter values below to test your code
- Take note that you are editing the inside of the
update
function - For the given
id
parameter, which is associated to thecollection
object:- If the
value
parameter isn't an empty string, update (or set) thevalue
parameter for theprop
parameter - If the
prop
parameter is equal to"tracks"
and thevalue
isn't an empty string, push thevalue
onto the end of thetracks
array - If
value
is an empty string, delete thatprop
from the object
- If the
- Finally, return the
collection
object
Hint: 1
- Use an
else if
statement to check the needed steps.
Hint: 2
- The second step listed in the instructions should be first in your
else if
statement.
Hint: 3
- To access the value of a key in this object, you will use
collection[id][prop]
Spoiler Alert!
Solution ahead!
Code Solution:
function update(id, prop, value) {
if (prop === "tracks" && value !== "") {
collection[id][prop].push(value);
} else if (value !== ""){
collection[id][prop] = value;
} else {
delete collection[id][prop];
}
return collection;
}
Code Explanation:
- First checks if
prop
is equal totracks
AND ifvalue
isn't a blank string. If both tests pass,value
is pushed into thetracks
array. - If that first check doesn't pass, it next checks only if
value
isn't a blank string. If that test passes, either a new key (prop
) and value (value
) are added to the object, or an existing key is updated if theprop
already exists. - If both these checks fail (meaning
value
must be an empty string), then the key (prop
) is removed from the object.
Example Run
update(5439, "artist", "ABBA");
runsprop
is equal to "artist", not "tracks", so the first part of theelse if
statement failsvalue
is not a blank string, so the second part of the else if statement passesartist: "ABBA"
is added to the5439
id
Credits:
If you found this page useful, you can give thanks by copying and pasting this on the main chat: thanks @leftynaut for your help with Checkpoint: Record Collection
NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)