Skip to content

Instantly share code, notes, and snippets.

@basekays
Created October 20, 2016 02:43
Show Gist options
  • Select an option

  • Save basekays/d2f6978a4daa666869ad7cc37b006768 to your computer and use it in GitHub Desktop.

Select an option

Save basekays/d2f6978a4daa666869ad7cc37b006768 to your computer and use it in GitHub Desktop.
var _ = {};
//last element//
_.last = function(array) {
var index = array.length-1;
return array[index];
}
//last i elements//
_.last = function(array, i) {
return array.splice(array.length - i, i);
}
@amy-chiu

Copy link
Copy Markdown

again, how about if the array's length is 0. or if 'i' is undefined?

also, I think you want slice here, not splice. From MDN: The splice() method changes the content of an array by removing existing elements and/or adding new elements. try using a solution with slice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment