如何创建回文检查器?

发布于 2025-02-13 04:14:26 字数 1399 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水水月牙 2025-02-20 04:14:26

这是您的代码

  1. 函数palindrome(str){{删除第二个{
  2. 您声明REGEX,但切勿使用它
  3. /\ w/g.remove; ...这有什么作用? REGEX没有删除属性
  4. tolowercase是一个函数,它返回一个新值 - 您所做的不称呼该函数,也不会存储调用该函数的结果
  5. 删除此行
  6. 好的,
  7. 不要使其成为const,因为您不能更改const的const - 另外,命名变量与所使用的函数相同可能会引起混乱 - 因为您会看到
  8. 这将导致无限循环,您需要i - 循环
  9. 降低
  10. i
  11. 每个

​是:

function palindrome(str) {
    let regex = /\W/g;
    str = str.toLowerCase().replace(regex, '');

    function reverseString(str) {
        let reverseString = "";
        for (let i = str.length - 1; i >= 0; i--) {
            reverseString += str[i];
        }
        return reverseString;
    }
    if (str === reverseString(str)) {
        return true;
    }
    return false;
}

const result = palindrome("eye");
console.log(result)

但是,不需要该内部功能

function palindrome(str) {
    const regex = /\W/g;
    let reverseString = "";

    str = str.toLowerCase().replace(regex, '');

    for (let i = str.length - 1; i >= 0; i--) {
        reverseString += str[i];
    }

    if (str === reverseString) {
        return true;
    }
    return false;
}

const result = palindrome("eye");
console.log(result)

虽然我可能会像

function palindrome(str) {
    str = str.toLowerCase().replace(/\W/g, '');

    return str === str.split('').reverse().join('');
}

const result = palindrome("eye");
console.log(result);

here's the problems with your code

  1. function palindrome(str) {{ remove the second {
  2. you declare regex but never use it
  3. /\W/g.remove; ... what does this do? a regex doesn't have a remove property
  4. toLowerCase is a function, that returns a new value - what you do doesn't call the function, nor does it store the result of calling the function
  5. remove this line
  6. OK
  7. don't make this a const because you can't change const's - also, naming a variable the same as the function it is in can cause confusion - as you'll see
  8. this will cause an infinite loop, you need i-- to decrement i on each loop
  9. OK
  10. OK
  11. OK
  12. OK
  13. You need to call the function - it's confusing because your variable name
  14. OK
  15. OK
  16. OK
  17. OK
  18. This calls the function, but does nothing with the result

So, the resulting fixed code is:

function palindrome(str) {
    let regex = /\W/g;
    str = str.toLowerCase().replace(regex, '');

    function reverseString(str) {
        let reverseString = "";
        for (let i = str.length - 1; i >= 0; i--) {
            reverseString += str[i];
        }
        return reverseString;
    }
    if (str === reverseString(str)) {
        return true;
    }
    return false;
}

const result = palindrome("eye");
console.log(result)

But, no need for that inner function

function palindrome(str) {
    const regex = /\W/g;
    let reverseString = "";

    str = str.toLowerCase().replace(regex, '');

    for (let i = str.length - 1; i >= 0; i--) {
        reverseString += str[i];
    }

    if (str === reverseString) {
        return true;
    }
    return false;
}

const result = palindrome("eye");
console.log(result)

Though I'd probably write it like

function palindrome(str) {
    str = str.toLowerCase().replace(/\W/g, '');

    return str === str.split('').reverse().join('');
}

const result = palindrome("eye");
console.log(result);

抹茶夏天i‖ 2025-02-20 04:14:26

您可以执行以下操作:

function palindrome(str) {
    str = str.toLowerCase();  //Turn everything to lower case
    str = str.replace(/[^a-z0-9]/g, ''); // Remove all non alphanumeric characters.

    var revStr = str.split('');
    revStr = revStr.reverse().join('').toString();
  
    return str == revStr;

}

You can do the following:

function palindrome(str) {
    str = str.toLowerCase();  //Turn everything to lower case
    str = str.replace(/[^a-z0-9]/g, ''); // Remove all non alphanumeric characters.

    var revStr = str.split('');
    revStr = revStr.reverse().join('').toString();
  
    return str == revStr;

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文