Comments (9)
@LvShCh 应该是你会错了题目意思。confirmEnding("He has to give me a new name", "me")
这个返回值应该就是true。测试用例没有错。
下面是参考代码?可以通过全部的测试用例。
function confirmEnding(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
if (target.length > str.length) return false;
return str.substr(0-target.length) == target;
}
confirmEnding("Bastian", "tan");
from freecodecamp.cn.
请详细说明,并附上代码
Please add your code and more details
from freecodecamp.cn.
在confirm the ending 这个挑战中,有一个测试用例是confirmEnding("He has to give me a new name", "me") 应该返回 true. 这里应该是返回false,虽然在页面提交的时候验证为正确。
------------------ 原始邮件 ------------------
发件人: "Disney";[email protected];
发送时间: 2016年11月12日(星期六) 下午2:02
收件人: "FreeCodeCampChina/freecodecamp.cn"[email protected];
抄送: "Mr_根号3"[email protected]; "Author"[email protected];
主题: Re: [FreeCodeCampChina/freecodecamp.cn] 页面的测试中有一条应该显示为false的,虽然能正常通过挑战 (#50)
请详细说明,并附上代码
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
from freecodecamp.cn.
function confirmEnding(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
var contant = str.substr(str.length-target.length);
var result = contant == target;
return result;
}
confirmEnding("hu", "hu ming");
我觉得这样做是比较好理解的做法了
from freecodecamp.cn.
@TheFlashinchina 为啥不直接 return str.substr(str.length - target.length) === target
。。。
另外,也可以 return str.slice(-target.length) === target
。slice
支持负向索引。从结尾开始找
from freecodecamp.cn.
我的,刚开始的方案虽然不简洁,但更容易被初学者理解:
方案一:
function confirmEnding(str, target) {
for(var i=1;i<=str.length;i++){
if(str.substr(-i,i)===target){
return true;
}
}
return false;
}
confirmEnding("Bastian", "n");
然而,前面FCC的课程表明,其实比较运算符已经自带了true or false属性,所以为什么不利用好这点呢:
方案二:
function confirmEnding(str, target) {
return str.substr(-target.length,target.length)===target;
}
confirmEnding("Bastian", "n");
关键是只要确定好如何表述末尾的字符串字符就好。
from freecodecamp.cn.
@Rodney2012 个人觉得,如果决定遍历,就不需要再用字符串方法了,用 index 直接获取可能会更容易理解。因为讲道理,如果用了字符串方法,那就没必要遍历了。
这道题用 substr
虽然可以实现,但会比较麻烦。建议去了解下 slice
方法。return str.slice(-target.length) === target
from freecodecamp.cn.
用ES6就一句话,,
from freecodecamp.cn.
return str.endsWith(target);
from freecodecamp.cn.
Related Issues (20)
- Why I can't pass the test?
- no need to use break HOT 2
- 输入参数检验结果是正确的,但是就是通不过运行
- "25 or More" is wrong ,but I can't fix it. HOT 1
- "9 or Under" is wrong ,but I can't fix it. HOT 1
- why i can't pass Create a Bootstrap Row, pls help?
- Truncate a string 有点问题
- Add your JavaScript Slot Machine Slots 文字错误
- Build a Tribute Page |观看不了右边视频,是否有其他视频源 HOT 1
- 在style标签的最后两段代码,按照要求添加的Id选择器,代码无法通过,并无明显错误,不知道什么原因?
- The Code I write wrong deliberately can run and work as well. HOT 1
- 代码输出正确,却不能通过测试 I'm a little tea pot HOT 1
- 点击校验
- input中type属性checkbook无法识别
- 这个为什么不能过去呢
- Chinese interface:It allows data structures to be any combination of strings, numbers, Booleans, strings, and objects.Did you write "strings" again?
- 这个是BUG吗 HOT 1
- 这样为什么会显示错误
- 为什么过不去?例题和实际无联系。 HOT 1
- 哪里有“我已经完成了这个挑战按钮”啊
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from freecodecamp.cn.