最近开始在freeCodeCamp上学习,对JavaScript的算法部分的题目进行记录,强化一下自己的知识点,也方便以后复习。
题目描述: 检查一个字符串(str)是否以指定的字符串(target)结尾。 如果是,返回true, 如果不是,返回false。
思路: 该方法的实现主要是用指定的字符串和字符串最后一部分进行比较,用到String对象中的substr()方法或者substring()方法。两个方法都是从字符串中抽取字符。
function confirmEnding(str, target) {
if(target === str.substr(-target.length)){
return true;
}
else{
return false;
}
}
confirmEnding(
"Bastian",
"n");
转载请注明原文地址: https://ju.6miu.com/read-23874.html