LeetCode/problems/js/array-prototype-last.js
Matej Focko de8ef50ac8
problems(js): add “2619. Array Prototype Last”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-06-03 20:30:42 +02:00

13 lines
236 B
JavaScript

Array.prototype.last = function() {
if (!this.length) {
// empty array, ideally should throw an exception
return -1;
}
return this[this.length - 1];
};
/**
* const arr = [1, 2, 3];
* arr.last(); // 3
*/