How to write palindrome in JavaScript

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

    I think following function with time complexity of o(log n) will be better.

        function palindrom(s){
        s = s.toString();
        var f = true; l = s.length/2, len = s.length -1;
        for(var i=0; i < l; i++){
                if(s[i] != s[len - i]){
                        f = false; 
                        break;
                }
        }
        return f;
    
        }
    

    console.log(palindrom(12321));

提交回复
热议问题