-
Notifications
You must be signed in to change notification settings - Fork 341
Lesson Review Record Collection
Created by Rafase282
Github | FreeCodeCamp | CodePen | LinkedIn | Website | E-Mail
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.
- Waypoint: Accessing Objects Properties with Bracket Notation
- Waypoint: Add New Properties to a JavaScript Object
- Waypoint: Delete Properties from a JavaScript Object
- [Waypoint: Accessing Nested Objects in JSON] (http://www.freecodecamp.com/challenges/waypoint-accessing-nested-objects-in-json)
-
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
- Use an
else if
statement to check the needed steps.
- The second step listed in the instructions should be first in your
else if
statement.
- To access the value of a key in this object, you will use
collection[id][prop]
Solution ahead!
// Setup
var collection = {
2548: {
album: "Slippery When Wet",
artist: "Bon Jovi",
tracks: [
"Let It Rock",
"You Give Love a Bad Name"
]
},
2468: {
album: "1999",
artist: "Prince",
tracks: [
"1999",
"Little Red Corvette"
]
},
1245: {
artist: "Robert Palmer",
tracks: []
},
5439: {
album: "ABBA Gold"
}
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));
// Only change code below this line
function update(id, prop, value) {
if (value === '') {
delete collection[id][prop];
} else if (prop !== 'tracks') {
collection[id][prop] = value;
} else {
collection[id][prop].push(value);
}
return collection;
}
// Alter values below to test your code
update(5439, "artist", "ABBA");
Thanks for visiting, if you like this please feel free to star my repo, follow me or even contact me about contributing as it will be a lot of work and having help would be cool.
- HTML5 and CSS
- Responsive Design with Bootstrap
- Gear up for Success
- jQuery
- Basic JavaScript
- Object Oriented and Functional Programming
- Basic Algorithm Scripting
- Basic Front End Development Projects
- Intermediate Algorithm Scripting
- JSON APIs and Ajax
- Intermediate Front End Development Projects
- Claim Your Front End Development Certificate
- Upper Intermediate Algorithm Scripting
- Automated Testing and Debugging
- Advanced Algorithm Scripting
- AngularJS (Legacy Material)
- Git
- Node.js and Express.js
- MongoDB
- API Projects
- Dynamic Web Applications
- Claim Your Back End Development Certificate
- Greefield Nonprofit Project 1
- Greefield Nonprofit Project 2
- Legacy Nonprofit Project 1
- Legacy Nonprofit Project 2
- Claim your Full Stack Development Certification
- Whiteboard Coding Interview Training
- Critical Thinking Interview Training
- Mock Interview 1
- Mock Interview 2
- Mock Interview 3