-
Notifications
You must be signed in to change notification settings - Fork 0
Challenge 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.
- 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
-
Change the code below
// Only change code below this lineand up to// Alter values below to test your code -
Take note that you are editing the inside of the
updateRecordsfunction -
For the given
idparameter, which is associated to thecollectionobject:- If the
valueparameter isn't an empty string, update (or set) thevalueparameter for thepropparameter - If the
propparameter is equal to"tracks"and thevalueisn't an empty string, push thevalueonto the end of thetracksarray - If
valueis an empty string, delete thatpropfrom the object
- If the
-
Finally, return the
collectionobject
- Use an
else ifstatement to check the needed steps.
- The second step listed in the instructions should be first in your
else ifstatement.
- To access the value of a key in this object, you will use
collection[id][prop]
Solution ahead!
function updateRecords(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;
}
- First checks if
propis equal totracksAND ifvalueisn't a blank string. If both tests pass,valueis pushed into thetracksarray. - If that first check doesn't pass, it next checks only if
valueisn'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 thepropalready exists. - If both these checks fail (meaning
valuemust be an empty string), then the key (prop) is removed from the object.
Example Run
-
updateRecords(5439, "artist", "ABBA");runs -
propis equal to "artist", not "tracks", so the first part of theelse ifstatement fails -
valueis not a blank string, so the second part of the else if statement passes -
artist: "ABBA"is added to the5439id
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.)
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links