From 62ba0777e01bf7110ad3d1ab94346073345328b9 Mon Sep 17 00:00:00 2001 From: VitBu <33810987+VitBu@users.noreply.github.com> Date: Fri, 19 Oct 2018 18:03:25 +0300 Subject: [PATCH] Adding short and clear solution --- .../steamroller/index.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/steamroller/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/steamroller/index.md index 305c90ce49..b105d08d2f 100644 --- a/guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/steamroller/index.md +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/steamroller/index.md @@ -100,6 +100,29 @@ You will definitely need recursion or another way to go beyond two level arrays * Spread operator * Ternary Operator (`condition ? a : b`) +## ![:sunflower:](https://forum.freecodecamp.com/images/emoji/emoji_one/sunflower.png?v=3 ":sunflower:") Intermediate Code Solution 2: + + function steamrollArray(arr) { + while(arr.some(element => Array.isArray(element))) { + arr = arr.flat(); + } + return arr; + } + + steamrollArray([1, [2], [3, [[4]]]]); + +![:rocket:](https://forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=3 ":rocket:") Run Code + +### Code Explanation: + +* Use `Array.some()` method to find out if the new array contains an array still, if it does flatten the array +* Repeats, until there is no more arrays inside arr. + +#### Relevant Links + +* Array.some +* Array.flat + ## ![:rotating_light:](https://forum.freecodecamp.com/images/emoji/emoji_one/rotating_light.png?v=3 ":rotating_light:") Advanced Code Solution: function steamrollArray(arr) {