Basic Algorithm Scripting: MutationsPassed- unusual solution using regex

Tell us what’s happening:
While working through this module, I created an unusual solution using regular expressions. Though this solution passed (yay!) it is very different from the provided solutions. Please note, I didn’t crosscheck hints or the solution until after my solution had passed so I’m curious how my solution is vastly different.

I have two questions:
I know people don’t like regex because its difficult to read, but is there any other reason I shouldn’t use it in this module.

What is the argument to use another method for this solution (as opposed to mine)

Your code so far

//Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array. Think scrabble
function mutation(arr) {
let base = arr[0];
let regex = RegExp('^[' + base + ']+$', 'i');
return regex.test(arr[1]);
console.log(mutation(["Mary", "Army"])); //true

RegExp constructor creates a regular expression object for matching text with a pattern
Constructor parameters are enclosed between quote marks '' or "", instead of literal notation that is typically seen with regex /regex/
Constructor is used because the pattern is from the user input, arr[1] aka base variable
+base+ concatenated base variable with constructor ''

Reading the regex: 
^ start of string
$ end of string 
^string$ means no other characters other than the pattern 
base is the variable container value "Army"
[...] One of the characters in the brackets for that specific character space
+ one or more of the previous.
flag i: upper or lower case

RegExp('^[' + base + ']+$', 'i')
Equivalent to: /^[Army]+$/i

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36.

Challenge: Mutations

Link to the challenge:

@anon10857827 Hi Rielka, my solution works (and passes the tests) without any changes needed.

My questions were centered around why use regex or not

Your solution is clever and clean, I would say it’s better than any solution provided in the hints. Good job!

(Disclaimer: I might be biased as I prefer this solution)

1 Like

@snigo wow that’s a really clean solution!