How to write palindrome in JavaScript

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

    I am not sure how this JSPerf check the code performance. I just tried to reverse the string & check the values. Please comment about the Pros & Cons of this method.

    function palindrome(str) {
        var re = str.split(''),
            reArr = re.slice(0).reverse();
    
        for (a = 0; a < re.length; a++) {
            if (re[a] == reArr[a]) {
                return false;
            } else {
                return true;
            }
        }
    }
    

    JS Perf test

提交回复
热议问题