When this method is called, it reverses the order of the items in the original array. Then then it returns that same, original array. No new arrays should need to be created to pass this kata.
However, I'm trying to figure out how this.push(arr.pop()); works in this function.
Array.prototype.reverse = function() {
var arr = this.splice(0); //I understand first we remove all the items in the array starting from [0]
while(arr.length) { //then we run the arr.length of all the items?
this.push(arr.pop()); //then add the deleted last item? doesn't make sense...
}
return this;
};
test cases:
Test.assertSimilar([1, 2, 3, 4].reverse(), [4,3,2,1]);
Test.assertSimilar(["a", "b", "c"].reverse(), ["c", "b", "a"]);
Test.assertSimilar([].reverse(), []);
or please write a function you think is a better solution