How to write palindrome in JavaScript

前端 未结 30 1776
情书的邮戳
情书的邮戳 2020-11-29 02:42

I wonder how to write palindrome in javascript, where I input different words and program shows if word is palindrome or not. For example word noon is palindrome, while bad

30条回答
  •  长情又很酷
    2020-11-29 03:37

    Best Way to check string is palindrome with more criteria like case and special characters...

    function checkPalindrom(str) {
        var str = str.replace(/[^a-zA-Z0-9]+/gi, '').toLowerCase();
        return str == str.split('').reverse().join('');
    }
    

    You can test it with following words and strings and gives you more specific result.
    1. bob
    2. Doc, note, I dissent. A fast never prevents a fatness. I diet on cod

    For strings it ignores special characters and convert string to lower case.

提交回复
热议问题