freecodecamp.cn icon indicating copy to clipboard operation
freecodecamp.cn copied to clipboard

Confirm the Ending bug

Open cuixiaorui opened this issue 9 years ago • 3 comments

浏览器信息

  • chrome 59.0.3071.115
  • win10

问题描述

  • Confirm the Ending
  • confirmEnding("He has to give me a new name", "me") 应该返回 true. 这个条件是错误的 应该返回false

你的代码

function confirmEnding(str, target) { // 请把你的代码写在这里 var arr = str.split(" "); if (arr.length == 1) { if (str[str.length - 1] === target) { return true; } else { return false; }

        } else {
            if (arr[arr.length - 1] === target) {
                return true;
            } else {
                return false;
            }
        }

    }

    confirmEnding("He has to give me a new name", "me");

cuixiaorui avatar Jul 10 '17 09:07 cuixiaorui

@cuixiaorui 这个显然是正确的。 "He has to give me a new name" 可以是以 "e" 结尾,可以是 "me" 结尾,也可以是 "name" 结尾,还可以是 "new name" 结尾... 这道题不是只让你检查最后一个字符。。而是最后是否包含 target

S1ngS1ng avatar Jul 10 '17 13:07 S1ngS1ng

function confirmEnding(str, target) {
  // 请把你的代码写在这里
  if(target.length==1){
    if(str[str.length-1]==target[target.length-1]){
      return true;
    }else{
      return false;
    }
  }
  if(target.length!=1){
    if(str[str.length-1]==target[target.length-1]){
      if(str[str.length-2]==target[target.length-2]){
        return true;
      }else{
        return false;
      }    
    }else{
      return false;
    }
  }
}

~~confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification"); 虽然麻烦,可是通俗易懂~~

leifangqing avatar Oct 10 '17 05:10 leifangqing

@leifangqing 很显然你的代码思路不太对,因为你只检测了最后两个字符。 比如,confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "Aen") 结果应该是 false,你的代码会返回 true

S1ngS1ng avatar Oct 10 '17 17:10 S1ngS1ng