728x90
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Filter Range "In place" | |
// Write a function filterRangeInPlace(arr, a, b) that gets an array arr and | |
// removes from it all values except those that are between a and b. The test is: a ≤ arr[i] ≤ b. | |
// !! The function should only modify the array. It should not return anything. | |
function filterRangeInPlace(arr, a, b) { | |
for(let i = 0; i < arr.length; i++) { | |
if ((arr[i] < a || arr[i] > b)) { | |
arr.splice(i, 1); | |
i--; | |
} | |
} | |
} | |
let arr = [5, 3, 8, 1]; | |
filterRangeInPlace(arr, 1, 4); // removed the numbers except from 1 to 4 | |
console.log( arr ); // [3, 1] |
'Algorithm > JavaScript(Node.js)' 카테고리의 다른 글
The Modern JS Tutorial: array-methods: map to names (0) | 2021.09.20 |
---|---|
The Modern JS Tutorial: array-methods: calculator (0) | 2021.09.20 |
The Modern JS Tutorial: array-methods: filterRange (0) | 2021.09.13 |
The Modern JS Tutorial: array-methods: camelize (0) | 2021.09.13 |
백준 15652번: N과 M (4) Node.js(JavaScript) (0) | 2020.02.27 |