jokebird

导航

freeCodeCamp:Confirm the Ending

检查一个字符串(str)是否以指定的字符串(target)结尾。

如果是,返回true;如果不是,返回false。

 1 /*思路
 2   字符串长度str.length等于字符串位置str.indexOf() + 字符串长度target.length;
 3   为避免字符串中重复的target所以应从后往前搜索lastIndexOf() ;
 4 */
 5 
 6 function confirmEnding(str, target) {
 7   if(str.lastIndexOf(target)+target.length!=str.length){
 8     return false;
 9   }
10   return true;
11 }
12 
13 confirmEnding("Bastian", "n");

 

posted on 2016-09-26 15:01  jokebird  阅读(170)  评论(0编辑  收藏  举报