From 9d4f6ed12fec564110998b907f710c1dd91ae762 Mon Sep 17 00:00:00 2001 From: Mau Di Bert Date: Thu, 24 Jan 2019 08:39:08 -0300 Subject: [PATCH] solution out of the tutorial --- .../3-filter-range-in-place/solution.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/1-js/05-data-types/05-array-methods/3-filter-range-in-place/solution.md b/1-js/05-data-types/05-array-methods/3-filter-range-in-place/solution.md index e69de29b..ced9a9ac 100644 --- a/1-js/05-data-types/05-array-methods/3-filter-range-in-place/solution.md +++ b/1-js/05-data-types/05-array-methods/3-filter-range-in-place/solution.md @@ -0,0 +1,22 @@ +``` js run +function filterRangeInPlace(arr, a, b) { + + for (let i = 0; i < arr.length; i++) { + let val = arr[i]; + + // remove if outside of the interval + if (val < a || val > b) { + arr.splice(i, 1); + i--; + } + } + +} + +let arr = [5, 3, 8, 1]; + +filterRangeInPlace(arr, 1, 4); // removed the numbers except from 1 to 4 + +alert( arr ); // [3, 1] + +```