Check if a given string is a palindrome. Case sensitivity should be taken into account.

Technology CommunityCategory: JavaScriptCheck if a given string is a palindrome. Case sensitivity should be taken into account.
VietMX Staff asked 3 years ago

palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward.

isPalindrome("racecar"); // true
isPalindrome("race Car"); // true

function isPalindrome(word) {
  // Replace all non-letter chars with "" and change to lowercase
  var lettersOnly = word.toLowerCase().replace(/\s/g, "");

  // Compare the string with the reversed version of the string
  return lettersOnly === lettersOnly.split("").reverse().join("");
}

Or 25x faster than the standard answer

function isPalindrome(s,i) {
   return (i=i||0)<0||i>=s.length>>1||s[i]==s[s.length-1-i]&&isPalindrome(s,++i);
}