How to write palindrome in JavaScript

前端 未结 30 1766
情书的邮戳
情书的邮戳 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条回答
  •  萌比男神i
    2020-11-29 03:39

    To avoid errors with special characters use this function below

    function palindrome(str){
      var removeChar = str.replace(/[^A-Z0-9]/ig, "").toLowerCase();
      var checkPalindrome = removeChar.split('').reverse().join('');
      if(removeChar === checkPalindrome){
         return true;
      }else{
        return false;
      }
    }
    

提交回复
热议问题