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: Array Getters and Setters
Tweet me that you did the exercise!
Prompt 1
Implement a method,
head
that gets the first element of array.
Definition:
head(array)
Example Usage:
const
suspects = [
"Colonel Mustard"
,
"Prof Plum"
];
console.
log
(head(suspects));
// => "Colonel Mustard"
Code here
Loading…
head Solution
Prompt 2
Implement
nthValue
:
Gets the element at index n of array. If n is negative, the nth element from the end is returned.
Definition:
nthValue(array, number)
Example Usage:
const
suspects = [
"Colonel Mustard"
,
"Miss Scarlett"
,
"Prof Plum"
];
console.log(nthValue(suspects,
0
));
// => "Colonel Mustard"
console.log(nthValue(suspects,
2
));
// => "Prof Plum"
console.log(nthValue(suspects,
-1
));
// => "Prof Plum"
Code here
Loading…
nthValue Solution
Next 👉
Memory Model
©2021
Code and Coffee
for
Frontend Masters
Prompt 1
Code here
head Solution
Prompt 2
Code here
nthValue Solution
Next 👉 Memory Model
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.