Last active
August 22, 2020 14:25
-
-
Save vldvel/22d17d1ef1f2a0d0ace122ba5c3554bb to your computer and use it in GitHub Desktop.
JavaScript is Palindrome
This file contains 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
const string1 = 'level'; | |
const string2 = 'Le, vel.'; | |
const isPalindrome = (stringToTest1, stringToTest2) => { | |
const stringToTestTransformed1 = stringToTest1 | |
.replace(/[^\w]/gi, '') // replace all non-lettter characters | |
.toLowerCase(); // make all characters lower cased | |
const stringToTestTransformedAndReversed2 = stringToTest2 | |
.replace(/[^\w]/gi, '') | |
.toLowerCase() | |
.split('') // make an array | |
.reverse() // revers the array | |
.join(''); // make a string | |
return stringToTestTransformed1 === stringToTestTransformedAndReversed2; | |
} | |
isPalindrome(string1, string2); // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment