Trying to make a function that removes every number from an array (in place) except those between the last two parameters in the function. Those should be left. I got this exercise from:
https://javascript.info/array-methods
So why does this not work?
/*
* Array excercize Filter range in place.
* remove all except between a and b
*/
"strict"
var arr = [5, 3, 8, 1, 0, 11, 13, 100, 72, 80, 30, 22];
function filterRangeInPlace(arr, a, b) {
arr.forEach(function(item, index, array) {
if ((item < a) || (item > b)) {
array.splice(index, 1);
}
});
}
filterRangeInPlace(arr, 11, 30);
console.log(arr);