Confirm the Ending with Regexes

I have thought of a solution for this challenge using regexes and it seems to return only false.
I would like to better understand why this is happening. What am i missing ?

Your code so far

function confirmEnding(str, target) {
  // "Never give up and good luck will find you."
  // -- Falcor

let strArr = str.split("");
let targetArr = target.split("");

return /targetArr$/.test(strArr);

confirmEnding("Bastian", "n");


Your browser information:

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

Link to the challenge:

First of all i would like to point out this indication:

for the purpose of this challenge, we would like you to use one of the JavaScript substring methods instead.

However, if you want to dynamically set up a regex you cannot put the variable directly inside the regex because it will be interpreted literally; you are testing each string checking if it ends with the word targetArr.
To use a variable you can use the regex constructor, something like:

let target = 'name';
let dynamicRegex = new RegExp(target+'$')

You can dig deeper here: MDN - RegExp

1 Like

Thank you very much for this answer. I will read more into it.

1 Like