Basic Algorithm Scripting-problem

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.

FreeCodeCamp is open source, you can add yourself the solution with the regular expression to the guide

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

2 Likes

I believe you’re looking for something like this?

function confirmEnding(str, target) {
    let re = new RegExp(target + '$');
    return re.test(str);
}
1 Like

Thanks for your reply

Thank. That’s what I need.

Can someone help me find where I can get lessons for basic algorithm scripting and beyond for Javascript on Youtube or somewhere.

Hi there…

Although I am studying …
How I do it

  1. Simply break the problem
  2. View MDN examples of divided issues
    And apply
  3. Attach now

Conclusion: I should try myself
Improved ability.

I study this way
Just for reference.