How to write palindrome in JavaScript

前端 未结 30 1670
情书的邮戳
情书的邮戳 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:36

    Or you could do it like this.

    var palindrome = word => word == word.split('').reverse().join('')
    
    0 讨论(0)
  • 2020-11-29 03:37
    function palindrome(str) {
    
        var len = str.length;
        var mid = Math.floor(len/2);
    
        for ( var i = 0; i < mid; i++ ) {
            if (str[i] !== str[len - 1 - i]) {
                return false;
            }
        }
    
        return true;
    }
    

    palindrome will return if specified word is palindrome, based on boolean value (true/false)

    UPDATE:

    I opened bounty on this question due to performance and I've done research and here are the results:

    If we are dealing with very large amount of data like

    var abc = "asdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfdasdhfkahkjdfhkaksjdfhaksdfjhakjddfhkjahksdhfaiuyqewiuryiquweyriyqiuweyriuqiuweryiquweyriuqyweirukajsdhfkahdfjkhakjsdhfkahksdhfakhdjkfqwiueryqiueyriuasdkfjhakjhdfkjashfkajhsdfkjahsdkalsdjflkasjdfljqoiweurasldjflasfd";
    
    for ( var i = 0; i < 10; i++ ) {
        abc += abc;  // making string even more larger
    }
    
    function reverse(s) { // using this method for second half of string to be embedded
        return s.split("").reverse().join("");
    }
    
    abc += reverse(abc); // adding second half string to make string true palindrome
    

    In this example palindrome is True, just to note

    Posted palindrome function gives us time from 180 to 210 Milliseconds (in current example), and the function posted below with string == string.split('').reverse().join('') method gives us 980 to 1010 Milliseconds.

    Machine Details:

    System: Ubuntu 13.10 OS Type: 32 Bit RAM: 2 Gb CPU: 3.4 Ghz*2 Browser: Firefox 27.0.1

    0 讨论(0)
  • 2020-11-29 03:37

    Look at this:

    function isPalindrome(word){
        if(word==null || word.length==0){
            // up to you if you want true or false here, don't comment saying you 
            // would put true, I put this check here because of 
            // the following i < Math.ceil(word.length/2) && i< word.length
            return false;
        }
        var lastIndex=Math.ceil(word.length/2);
        for (var i = 0; i < lastIndex  && i< word.length; i++) {
            if (word[i] != word[word.length-1-i]) {
                return false;
            }
         }
         return true;
    } 
    

    Edit: now half operation of comparison are performed since I iterate only up to half word to compare it with the last part of the word. Faster for large data!!!

    Since the string is an array of char no need to use charAt functions!!!

    Reference: http://wiki.answers.com/Q/Javascript_code_for_palindrome

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-29 03:38

    Below code tells how to get a string from textBox and tell you whether it is a palindrome are not & displays your answer in another textbox

    <html>
    <head>
    <meta charset="UTF-8"/>
    <link rel="stylesheet" href=""/>
    
    </head>  
    <body>   
    <h1>1234</h1>
    <div id="demo">Example</div>
    <a  accessKey="x" href="http://www.google.com" id="com" >GooGle</a>
    <h1 id="tar">"This is a Example Text..."</h1>
    Number1 : <input type="text" name="txtname" id="numb"/>
    Number2 : <input type="text" name="txtname2" id="numb2"/>
    Number2 : <input type="text" name="txtname3" id="numb3" />
    <button type="submit"  id="sum" onclick="myfun()" >count</button>
    <button type="button"  id="so2" onclick="div()" >counnt</button><br/><br/>
    <ol>
    <li>water</li>
    <li>Mazaa</li>
    </ol><br/><br/>
    <button onclick="myfun()">TryMe</button>
        <script>
        function myfun(){
        var pass = document.getElementById("numb").value;
        var rev = pass.split("").reverse().join("");
        var text = document.getElementById("numb3");
        text.value = rev;
        if(pass === rev){
        alert(pass + " is a Palindrome");
        }else{
        alert(pass + " is Not a Palindrome")
        }
        }
        </script>
    </body>  
    </html>  
    
    0 讨论(0)
  • 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;
      }
    }
    
    0 讨论(0)
提交回复
热议问题