From a4c1f68717c74d63dca87561d0228fe8dd64a768 Mon Sep 17 00:00:00 2001 From: spassaro80 <43064539+spassaro80@users.noreply.github.com> Date: Thu, 22 Aug 2019 19:40:06 +0200 Subject: [PATCH] Solution using RegExp (#36665) * Update index.md I think it can be interesting to propose a solution using RegExp. * fix: wrapped code in code fences --- .../mutations/index.md | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations/index.md index ef89c65dec..f85d80ccd8 100644 --- a/guide/english/certifications/javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations/index.md +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations/index.md @@ -90,4 +90,33 @@ Grab the second string, lowercase and turn it into an array; then make sure _eve * Array.split() * Array.every() - \ No newline at end of file + + + +
Solution 3 (Click to Show/Hide) + +**Declarative** + +```js +function mutation(arr) { + for (var el of arr[1]){ + let re = new RegExp(el,"i"); + if (re.test(arr[0]) == false){ + return false; + } + } + return true; +} +``` + +#### Code Explanation + +* For every letter of the second Array we want to check if it's in the first array using the test() Method. +* The RegExp(el,"i") function will create a Regular Expression for every letter. For example RegExp("Hey",i) will create /H/i, then /e/i, then /y/i. +* If there is one letter that return false, than we return false. If arr[0] contains all letters than we return true. + +#### Relevant Links + +* RegExp.test() + +