Skip to content

Commit e774c7c

Browse files
authored
Merge pull request #756 from maurodibert/patch-22
solution out of the tutorial
2 parents 58b7d72 + 9d4f6ed commit e774c7c

File tree

1 file changed

+22
-0
lines changed
  • 1-js/05-data-types/05-array-methods/3-filter-range-in-place

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
``` js run
2+
function filterRangeInPlace(arr, a, b) {
3+
4+
for (let i = 0; i < arr.length; i++) {
5+
let val = arr[i];
6+
7+
// remove if outside of the interval
8+
if (val < a || val > b) {
9+
arr.splice(i, 1);
10+
i--;
11+
}
12+
}
13+
14+
}
15+
16+
let arr = [5, 3, 8, 1];
17+
18+
filterRangeInPlace(arr, 1, 4); // removed the numbers except from 1 to 4
19+
20+
alert( arr ); // [3, 1]
21+
22+
```

0 commit comments

Comments
 (0)