This is not for a job interview, don't worry :)
I am curious as to why these two functions return differently:
var redundantSlice1 = function() {
return arguments[0].slice(Array.prototype.slice.call(arguments).slice(1).join())
};
var redundantSlice2 = function() {
return arguments[0].slice(1,2)
};
redundantSlice1([1,2,3], 1, 2) // [1,2,3]
redundantSlice2([1,2,3], 1, 2) // [2]
redundantSlice2 returns what I expect, but the 1,2 are hardcoded, this is what I am trying to avoid.
Why does the first function have what I expect to be 1,2 as undefined?
.joinreturns a string. So the first example is equivalent toarr.slice("1,2"). That's very different fromarr.slice(1, 2). I'm not sure how.slicetreats invalid arguments (apparently it treats it like0or nothing was passed).nullandundefined.function.apply- but as slice only ever takes 0, 1 or 2 arguments, I'd define your wrapper function asfunction(array, begin, end)then simply return[].slice.call(array, begin, end)- that's just as much a re-implementation as you've written