Skip to content

Instantly share code, notes, and snippets.

@petergi
Created December 28, 2023 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petergi/18d4789ecd7dcf83efb9a486d75e98d5 to your computer and use it in GitHub Desktop.
Save petergi/18d4789ecd7dcf83efb9a486d75e98d5 to your computer and use it in GitHub Desktop.
Checks if the given string is a palindrome.
/**
* Check if a given string is a palindrome.
*
* @param {string} str - The string to be checked.
* @return {boolean} Returns true if the string is a palindrome, false otherwise.
*/
function isPalindrome(str) {
const formattedStr = str.toLowerCase().replace(/[\W_]/g, "");
const reversedStr = formattedStr.split("").reverse().join("");
return formattedStr === reversedStr;
}
palindrome("taco cat") //= true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment