GithubHelp home page GithubHelp logo

Comments (9)

zeallean avatar zeallean commented on May 1, 2024 2

@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.

discountry avatar discountry commented on May 1, 2024

请详细说明,并附上代码
Please add your code and more details

from freecodecamp.cn.

LvShCh avatar LvShCh commented on May 1, 2024

在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.

TheFlashinchina avatar TheFlashinchina commented on May 1, 2024

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.

S1ngS1ng avatar S1ngS1ng commented on May 1, 2024

@TheFlashinchina 为啥不直接 return str.substr(str.length - target.length) === target。。。
另外,也可以 return str.slice(-target.length) === targetslice 支持负向索引。从结尾开始找

from freecodecamp.cn.

slowlearnerLspeedup avatar slowlearnerLspeedup commented on May 1, 2024

我的,刚开始的方案虽然不简洁,但更容易被初学者理解:
方案一:
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.

S1ngS1ng avatar S1ngS1ng commented on May 1, 2024

@Rodney2012 个人觉得,如果决定遍历,就不需要再用字符串方法了,用 index 直接获取可能会更容易理解。因为讲道理,如果用了字符串方法,那就没必要遍历了。

这道题用 substr 虽然可以实现,但会比较麻烦。建议去了解下 slice 方法。return str.slice(-target.length) === target

from freecodecamp.cn.

asqhay avatar asqhay commented on May 1, 2024

用ES6就一句话,,

from freecodecamp.cn.

asqhay avatar asqhay commented on May 1, 2024

return str.endsWith(target);

from freecodecamp.cn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.