-
-
Save johnnybenson/836416442ab067fe72cad894abd212f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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