FreeCodeCampChina / freecodecamp.cn

FCC China open source codebase and curriculum. Learn to code and help nonprofits.

Home Page:https://fcc.asia/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

issue

YongweiWu opened this issue · comments

Challenge Check for Palindromes has an issue.
User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.
Please describe how to reproduce this issue, and include links to screenshots if possible.

My code:

function palindrome(str) {
  var z;
  str = str.toLowerCase();
  console.log(str);
  s = str.replace(/[\s|\,|\.]/g,"").test();
  var d = s.split("");
  var a = d.reverse();
  var b = a.join("");
  if(s != b){
    return false;
  }
  return true;
}
palindrome("0_0 (: /-\ :) 0-0");

@YongweiWu

  1. 你的正则写的不对。题目要求只保留字母和数字,可以考虑 /[^a-z0-9]/g。另外,[] 里面也不需要加 |。我的博客里有解释
  2. test() 方法也不是这么用的,这是正则的方法,字符串没有这个方法,看文档。所以你这么些会报错

以及,你也不需要这么多变量,那些方法可以连用的。比如

str.replace(...).split("").reverse().join("")

还有,最后那里,类似于:

if (xxx) {
  return true;
}
return false;

你直接写 return xxx 就好了