javascript - slice dosent work in function js -
simple thing doing eloquentjs exercise. slice doesn't return right output array in functions parameter
console.log(swaparray([1, 2, 3, 4, 5, 6, 7])); function swaparray(inserttab) { console.log([0, 1, 2, 3, 4].slice(2, 4)); // work fine inserttab.slice(2, 4); // nothing return inserttab; }
array.prototype.slice()
doesn't mutate array called on, returns new array. assign return value inserttab
, works fine:
console.log(swaparray([1, 2, 3, 4, 5, 6, 7])); function swaparray(inserttab) { console.log([0, 1, 2, 3, 4].slice(2, 4)); inserttab = inserttab.slice(2, 4); return inserttab; }
Comments
Post a Comment