JavaScript required
We’re sorry, but Coda doesn’t work properly without JavaScript enabled.
Skip to content
Gallery
JS Fundamentals: Objects, Arrays, and Functions
Introduction
Content
Bonus: Merge Sort
More
Share
Explore
Objects & Arrays
Exercise: Object Getters & Setters
Tweet me that you did the exercise!
Prompt 1
Implement a method,
getValue
that takes
an
object and key name and returns the value associated with that key or a default value if supplied.
Definition:
getValue(object, key, [defaultValue])
Example Usage:
const
who = {name:
"Colonel Mustard"
};
getValue(who,
"name"
);
// => "Colonel Mustard"
getValue(who,
"story"
,
"We don't know yet."
);
// => "We don’t know yet";
Code here
Loading…
Solution
The solution is in the solution branch, which can be checked out in the
Repl.it
by selecting the version control tab on the right and then selecting the solution branch from the drop down menu.
If you are having trouble with
repl.it,
the solutions are also available below.
getValue Solution
Prompt 2
Implement
setValue
:
Sets the value at property of object. If the property doesn't exist, it's created.
Note: This method mutates the original object.
Definition:
setValue(object, key, value)
Example Usage:
const
who = {name:
"Colonel Mustard"
};
setValue(who,
"name"
,
"Miss Scarlett"
);
console.log(who);
// => {name: "Miss Scarlett"}
Code here
Loading…
setValue Solution
Next 👉
Arrays
©2021
Code and Coffee
for
Frontend Masters
Prompt 1
Code here
Solution
getValue Solution
Prompt 2
Code here
setValue Solution
Next 👉 Arrays
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
Ctrl
P
) instead.