Skip to content

Instantly share code, notes, and snippets.

@johnnybenson
Forked from edoubart/isPalindrome.js
Last active March 27, 2019 22:30
Show Gist options
  • Save johnnybenson/836416442ab067fe72cad894abd212f0 to your computer and use it in GitHub Desktop.
Save johnnybenson/836416442ab067fe72cad894abd212f0 to your computer and use it in GitHub Desktop.
// Create a function that checks if a given string is a palindrome.
// A palindrome is text that reads the same forwards and backwards, disregarding puncation, spaces, etc.
const testStrings = [
"b",
"aba",
"abc", // FALSE
"abba",
"Race Car",
"Mr. Owl ate my metal worm",
"This is not a palindrome", // FALSE
"Ya! Pizza zip pizazz! I pay.",
"Doc, Note: I Dissent. A Fast Never Prevents A Fatness. I Diet On Cod.",
];
function isPalindrome(str) {
// Clean Input
const cleanStr = str
.toLowerCase()
.replace(/[^a-z]/g, '');
// Clean Input Reversed
const cleanStrReversed = cleanStr.split('').reverse().join('')
return cleanStr === cleanStrReversed;
}
// Test Strings
testStrings.forEach(function(str) {
console.log(`isPalindrome:${isPalindrome(str)}`, str);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment