-
Notifications
You must be signed in to change notification settings - Fork 2
Challenge Record Collection
🚩 Remember to use Read-Search-Ask
if you get stuck. Try to pair program 👥 and write your own code 📝
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, check to see if the given element in the array has the property of "tracks". If the element has the property of "tracks", push the value onto the end of the "tracks" array. If the element does not have the property, create the property and value pair.
If value is blank, delete that prop.
Always return the entire collection object.
- 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
updateRecords
function. - For the given id parameter, which is associated to the collection object:
- If the value parameter isn't an empty string, update (or set) the value parameter for the prop parameter.
- If the prop parameter is equal to
"tracks"
and the value isn't an empty string, push the value onto the end of the tracks array. - If value is an empty string, delete that prop from the object.
- Finally, return the collection object.
- 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
Use an else if
statement to check the needed steps.
try to solve the problem now
The second step listed in the instructions should be first in your else if
statement.
try to solve the problem now
To access the value of a key in this object, you will use collection[id][prop]
.
try to solve the problem now
Solution ahead!
function updateRecords(id, prop, value) {
if (prop === "tracks" && value !== "") {
if(collection[id][prop]) {
collection[id][prop].push(value);
}
else {
collection[id][prop]=[value];
}
} else if (value !== "") {
collection[id][prop] = value;
} else {
delete collection[id][prop];
}
return collection;
}
🚀 Run Code
- First checks if prop is equal to tracks AND if value isn't a blank string. If both tests pass, value is pushed into the tracks 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 the prop 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
-
updateRecords(5439, "artist", "ABBA");
runs. -
prop is equal to "artist", not "tracks", so the first part of the
else if
statement fails. - value is not a blank string, so the second part of the else if statement passes.
-
artist: "ABBA"
is added to the5439
id
.
If you found this page useful, you may say thanks to the contributors by copying and pasting the following line in the main chat:
Thanks @leftynaut @DarrenJansen for your help with Checkpoint: Record Collection
⚠️ DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.- Add an explanation of your solution.
- Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. 🚥
- Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)
See 👉
Wiki Challenge Solution Template
for reference.
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