JS Cara memfilter rentang di tempatnya

// @ts-check

let arr = [5, 3, 8, 1, 9, 2, 4, 15, 18, 42, 39];

function filterRangeInPlace(arr, a, b) {
  for (let i = 0; i < arr.length; ++i) {
    if (a > arr[i] || arr[i] > b) {
      arr.splice(i, 1);
      i--; // since the length of the array has changed
    }
  }
}
filterRangeInPlace(arr, 1, 4);

console.log(arr);
Condemned Corncrake