Algorithm Exercise 7: Confirm the End Character Algorithm

Checks strwhether a string ( ) ends with the specified string ( target).

If it is, return true; if not, return false.

This algorithm can be solved by the method introduced in ES2015 .endsWith().

One of the JavaScript substring methods can also be used.

method one:

function confirmEnding(str, target) {
return str.endsWith(target);
}

confirmEnding("Bastian", "n");

Method Two:

Concatenate the parameters into a regular string, and then convert the string into a regular object. Call the test() method to verify.

function confirmEnding(str, target) {

s=eval("/.*"+target+"$/");
return s.test(str)

//return str.endsWith(target);
}

confirmEnding("Bastian", "n");

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325260546&siteId=291194637