Mutation algorithm doubt

Tell us what’s happening:
Describe your issue in detail here.

why does regex=/[arr[1]/ ig not work when /[Hel]/ig does
isn’t arr[1]=Hel

   **Your code so far**

function mutation(arr) {
 let regex=/[arr[1]]/ig
 return regex.test(arr[0])
//return [arr[1]]
//return arr[1]
 
}

console.log(mutation(["hello", "Hel"]));
   **Your browser information:**

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

Challenge: Mutations

Link to the challenge:

/[Hel]/ig

One character, either an “H”, an “e” or an “l”.

g is any instances of it in the string you’re checking, i is regardless of case, so will also match “h”, “E” and “L”

/[arr[1]/ig

One character, either an “a”, an “r”, a “[” or a “1”,

g is any instances of it in the string you’re checking, i is regardless of case, so will also match “A” and “R”


A regex matches patterns in strings. If you want to dynamically build one, you need to use new RegExp()

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.