You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
11 lines
323 B
JavaScript
11 lines
323 B
JavaScript
5 years ago
|
last = function(array, n) {
|
||
|
if (array == null)
|
||
|
return void 0;
|
||
|
if (n == null)
|
||
|
return array[array.length - 1];
|
||
|
return array.slice(Math.max(array.length - n, 0));
|
||
|
};
|
||
|
|
||
|
console.log(last([7, 9, 0, -2]));
|
||
|
console.log(last([7, 9, 0, -2],3));
|
||
|
console.log(last([7, 9, 0, -2],6));
|