Hello everyone! I’m Sergei. And I want to ask you about the method of applying " Regular Expressions: Match Ending String Patterns to solve the problem Basic Algorithm Scripting: Confirm the Ending.
The original task looks like this:
function confirmEnding(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
return str;
}
And there is solution from FreeCodeCamp:
function confirmEnding(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
return str.slice(str.length-target.length)===target;
}
confirmEnding("Bastian", "n");
It is good solution.
But I think it is possible to solve this problem with the help of the application “ Regular Expressions: Match Ending String Patterns , for example:
function confirmEnding(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
return /target$/.test(str);
}
confirmEnding("Bastian", "n");
I understand that the “target” argument in the form / target $ / is not correct. But there is probably a way to pass this argument to the .test () function(for example using brackets or quotes).
I think so shorter and clean.
Thank you for your reply.