Confirm the Ending - Regex

Tell us what’s happening:
The solution used .slice() to solve this. Can someone explain how that worked? And what are the pro or cons of using regexes instead of .slice() in this situation?

Your code so far


function confirmEnding(str, target) {
  // "Never give up and good luck will find you."
  // -- Falcor
  const regex = RegExp(target + "\$");
  return regex.test(str);
}

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/77.0.3865.90 Safari/537.36.

Link to the challenge:

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

could you link to the solution you are talking about? it is difficult to answer you without seeing the solution you are referring to

Sorry for the late response. . . and not blurring my code out :sweat_smile: Will fix that next time.

Anyways, here is the link to the given solution
I didn’t quite understand the solution explanation

when you use a single argument for the slice method, it copies from that index to the end of the string, so it is checking the last characters of the string against the target

I don’t know of advantages or disadvantages

The biggest pro of regex here, that you can add i option and compare case-insensitive and fairly easy exclude possible spaces or punctuation marks, so confirmEnding('freeCodeCamp; ', 'camp') would work