Mutations : can I use regular expression in this challange?

Mutations : can I use regular expression in this challange?
0

#1

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


#2

Of course you can. Go head.


#3

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


#4

What have you done? Can you share your code?


#5

I messaged you… did not you get?


#6

Can you share the link on the forum here, so anyone can help you?


#7

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;
}

@randelldawson


#8

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


#9

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.