Created
October 20, 2016 02:43
-
-
Save basekays/d2f6978a4daa666869ad7cc37b006768 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.