Skip to content
JS Fundamentals: Objects, Arrays, and Functions
Share
Explore
Objects & Arrays

icon picker
Exercise: Array Getters and Setters

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 👉


©2021 for
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.