problems(js): add “2619. Array Prototype Last”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
86599c22a7
commit
de8ef50ac8
1 changed files with 13 additions and 0 deletions
13
problems/js/array-prototype-last.js
Normal file
13
problems/js/array-prototype-last.js
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
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
|
||||||
|
*/
|
Loading…
Reference in a new issue