Skip to content

Instantly share code, notes, and snippets.

@AliceWonderland
Forked from anonymous/5.6 My Slice.js
Created April 12, 2017 05:30
Show Gist options
  • Select an option

  • Save AliceWonderland/897c3d4a0eeab18a4e08c30a61dbbed4 to your computer and use it in GitHub Desktop.

Select an option

Save AliceWonderland/897c3d4a0eeab18a4e08c30a61dbbed4 to your computer and use it in GitHub Desktop.
5.6 My Slice created by smillaraaq - https://repl.it/HEZ7/1
function mySlice (arr,begin,end) {
var newArr = [];
var start = (begin)?begin:0;
start = (begin<0)?arr.length+begin:start;
var terminate = (end)?end:arr.length-1;
for (let i = start ; i <= terminate ; i++){
newArr.push(arr[i]);
}
return console.log(newArr);
}
mySlice([1,2,3]) // => [1,2,3]
mySlice([1,2,3], 1) // => [2,3]
mySlice([1,2,3], 1, 2) // => [2]
mySlice([1,2,3], -1) // => [3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment