
确认末尾字符算法挑战
方法一:
function confirmEnding(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
str = str.substr(str.length - target.length);
if(str == target){
return true;
}else{
return false;
}
}
confirmEnding("Bastian", "n");
方法二:
function confirmEnding(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
if(target.length > str.length){
return false;
} else{
if(str == target || str.substr(str.length- target.length) == target){
return true;
}else{
return false;
}
}
}
confirmEnding("Bastian", "n");