Checks str
whether 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");