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.
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.
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
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