From 76a75658a7c8c62e8d2f7d7deeb683ce6c8ddc0f Mon Sep 17 00:00:00 2001 From: Marco Rampin <71671995+marcorampin@users.noreply.github.com> Date: Sun, 10 Nov 2024 17:30:47 +0100 Subject: [PATCH 1/4] Updated _js.view/solution.js with alternative solution --- .../3-filter-range-in-place/_js.view/solution.js | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/1-js/05-data-types/05-array-methods/3-filter-range-in-place/_js.view/solution.js b/1-js/05-data-types/05-array-methods/3-filter-range-in-place/_js.view/solution.js index 488db3755b..c6ec0e5567 100644 --- a/1-js/05-data-types/05-array-methods/3-filter-range-in-place/_js.view/solution.js +++ b/1-js/05-data-types/05-array-methods/3-filter-range-in-place/_js.view/solution.js @@ -1,14 +1,8 @@ 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--; - } - } + arr + .filter(value => value < a || value > b) + .forEach(value => arr.splice(arr.indexOf(value), 1)); } From 8c85d513611dd9f33b8e4c1e49b542131102f783 Mon Sep 17 00:00:00 2001 From: Marco Rampin <71671995+marcorampin@users.noreply.github.com> Date: Sun, 10 Nov 2024 17:35:01 +0100 Subject: [PATCH 2/4] Changed 11-array-unique solution Alternative solution uses array methods to solve it --- .../05-array-methods/11-array-unique/solution.md | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/1-js/05-data-types/05-array-methods/11-array-unique/solution.md b/1-js/05-data-types/05-array-methods/11-array-unique/solution.md index b9d627a0a3..6298e9bdea 100644 --- a/1-js/05-data-types/05-array-methods/11-array-unique/solution.md +++ b/1-js/05-data-types/05-array-methods/11-array-unique/solution.md @@ -4,15 +4,11 @@ Let's walk the array items: ```js run demo function unique(arr) { - let result = []; + const result = []; - for (let str of arr) { - if (!result.includes(str)) { - result.push(str); - } - } + arr.forEach(item => result.includes(item) ? null : result.push(item)); + return result; - return result; } let strings = ["Hare", "Krishna", "Hare", "Krishna", @@ -30,7 +26,7 @@ So if there are `100` elements in `result` and no one matches `str`, then it wil That's not a problem by itself, because JavaScript engines are very fast, so walk `10000` array is a matter of microseconds. -But we do such test for each element of `arr`, in the `for` loop. +But we do such test for each element of `arr`, in the `forEach` method. So if `arr.length` is `10000` we'll have something like `10000*10000` = 100 millions of comparisons. That's a lot. From 4ce13f90f404fa1e4a5e966c0addc072957cae6c Mon Sep 17 00:00:00 2001 From: Marco Rampin <71671995+marcorampin@users.noreply.github.com> Date: Sun, 10 Nov 2024 17:36:24 +0100 Subject: [PATCH 3/4] Updated _js.view/solution.js for 11-array-unique problem with alternative solution --- .../11-array-unique/_js.view/solution.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/1-js/05-data-types/05-array-methods/11-array-unique/_js.view/solution.js b/1-js/05-data-types/05-array-methods/11-array-unique/_js.view/solution.js index d15cea2c7e..8201328901 100644 --- a/1-js/05-data-types/05-array-methods/11-array-unique/_js.view/solution.js +++ b/1-js/05-data-types/05-array-methods/11-array-unique/_js.view/solution.js @@ -1,11 +1,7 @@ function unique(arr) { - let result = []; + const uniqueArr = []; + + arr.forEach(item => uniqueArr.includes(item) ? null : uniqueArr.push(item)); + return uniqueArr; - for (let str of arr) { - if (!result.includes(str)) { - result.push(str); - } - } - - return result; } From c82d32a654c9fb6f746bdd1c4911b38c1f595e8d Mon Sep 17 00:00:00 2001 From: Marco Rampin <71671995+marcorampin@users.noreply.github.com> Date: Sun, 10 Nov 2024 17:47:14 +0100 Subject: [PATCH 4/4] Changed 3-filter-range-in-place solution Alternative solution uses array methods to solve it --- .../3-filter-range-in-place/solution.md | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) 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 36e3130ff0..41003bb30a 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 @@ -1,15 +1,9 @@ ```js run demo 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--; - } - } + arr + .filter(value => value < a || value > b) + .forEach(value => arr.splice(arr.indexOf(value), 1)); }