Mutations : can I use regular expression in this challange?

can I use regular expression for this https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations

Of course you can. Go head.

but how ?
I stack . If you would like see my code ? I shared the link .

What have you done? Can you share your code?

I messaged you… did not you get?

I was trying to do that . but I did not get any way to come out . Next I perform the general way … for this.
But I am still interested in regex for this problem.
here is my code-

type or paste code here
function mutation(arr) {
  let a;
  let str = arr[0];
  for(let i=0;i<arr[1].length;i++){
    if(arr[1][i].test(str)){
      a = true
    }
    else{
        a = false;
        break;
    }  
    }
  return a;
}

@camperextraordinaire

in the if condition … this is not right. But I wanted like that something.

So in your current code, you are getting the error message “arr[1][i].test is not a function”. Why? Because, in your if statement (below), the arr[1][i] would need to be a regular expression to be able to use the test method on it.

if(arr[1][i].test(str)){

So how can you create a regular expression which will use arr[1][i] as part of it. Personally, since you are only looking at a single character in each iteration of the for loop, testing a regular expression on str is kind of overdoing it. You have two other methods which would be perfect for this (indexOf method and includes method). Look those up and see how you could use those.

However, if you are insistent on using a regular expression, doing a Google search on “using a constructor for JavaScript RegEx” yields one possible approach. Basically, you will declare a new RegEx object and “build” the regular expression with arr[1][i] being a part of this expression. There is another example on Stack Overflow you can study.

1 Like