Basic Algorithm Scripting: Mutations using regular expressions

Tell us what’s happening:
I’m trying to use a regular expression to check if each letter from the second string exists in the first string but the test continues to return false even though “h” and “e” should return true.

I’m not very good at regular expressions.
Can someone tell me if I’m writing it wrong?

I’m not finished with the assignment yet but I pseudo wrote my intentions for completion.

Your code so far


function mutation(arr) {
//split second string into an array. one letter per array item.
var splitArr = arr[1].split("");
//iterate through array to look through the first string for each letter
let i = 0;
for (i in splitArr) {
  let testStr = arr[0];
  //console.log(arr[0]);
  console.log(splitArr[i]);
  let testRegex = /splitArr[i]+/i; //ignore case
  console.log(testRegex.test(testStr)); // should show true or false
  //create a if statement to return true only if all iterations were true
}
}

mutation(["hello", "hey"]);

Your browser information:

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

Challenge: Mutations

Link to the challenge:

this is not how you make a regular expression with a variable, you need to use new RegExp

I’m having trouble finding information on “new”. It looks like it creates an empty object.

Are you saying I need to assign splitArr[i] to this "new "object?
Ex:
let testRegex = new RegExp ( /splitArr[i]+/i);

something like

let target = "word";
let pattern = target + "$"; // (I create the string with the various symbols, other characters, etc)
let flags = "i" 
let re = new RegExp(pattern, flags)
console.log(re); // /word$/i

the only thing you do not need to worry about is the slashes around tbe regex, those are added by it