码迷,mamicode.com
首页 > 其他好文 > 详细

FreeCodeCamp:Confirm the Ending

时间:2016-11-09 22:02:44      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:out   ever   rom   font   tar   als   ping   span   style   

要求:

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

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

结果:

  • confirmEnding("Bastian", "n") 应该返回 true.
  • confirmEnding("Connor", "n") 应该返回 false.
  • confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") 应该返回 false.
  • confirmEnding("He has to give me a new name", "name") 应该返回 true.
  • confirmEnding("He has to give me a new name", "me") 应该返回 true.
  • confirmEnding("He has to give me a new name", "na") 应该返回 false.
  • confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") 应该返回 false.

 

 1 function confirmEnding(str, target) {
 2   // "Never give up and good luck will find you."
 3   var length=target.length;
 4   var strlength=str.length;
 5   var newstr=str.substr(strlength-length,length);
 6   if(newstr==target){
 7     return true;
 8   }
 9   else{
10     return false;
11   }
12   // -- Falcor
13   //return str;
14 }
15 
16 confirmEnding("Bastian", "n");

 

 

 

FreeCodeCamp:Confirm the Ending

标签:out   ever   rom   font   tar   als   ping   span   style   

原文地址:http://www.cnblogs.com/ttmj865/p/6048852.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!