Confirm the Ending. Need help understanding

Confirm the Ending. Need help understanding
0

#1

Hello, can comeone, please, explain, why my solution does not work? I tried different variations of syntax but the problem, I think, is with chosen method itself. This looks quite logical to me, but why doesn’t it work?

My code so far


function confirmEnding(str, target) {
var trg = /target$/i;

 if (trg.test(str) == true) {
   return true
 } else {
   return false
 }
 

}

confirmEnding("Bastian", "n");

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/confirm-the-ending


#2

You can’t use variables in regex with the // format. You must create a RegExp object, like this: var re = new RegExp(target,"g");

Anyway, the challenge does not ask for regex. It asks you to use the substring method, which you can read more about here https://www.w3schools.com/jsref/jsref_substring.asp


#3

Right! Thank you very much!