Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Last Element with at

Working With Arrayseasy

Course · Section 11: Working With Arrays · Lecture 150: The New at Method

Implement `lastEl(arr)` returning the last element using the `at` method.

Sample tests

Input: lastEl([1,2,3])
Output: 3
Input: lastEl([5])
Output: 5

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • arr[arr.length] is undefined; the last index is length - 1.
Approach & explanation (try first)

at accepts negative indices, so at(-1) is the last element.

Loading...
⌘/Ctrl + Enter

Run your code to see results.