How to write palindrome in JavaScript

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

    Here's a one-liner without using String.reverse,

    const isPal = str => Array
      .apply(null, new Array(strLen = str.length))
      .reduce((acc, s, i) => acc + str[strLen - (i + 1)], '') === str;
    

提交回复
热议问题