Skip to content

Instantly share code, notes, and snippets.

@vldvel
Last active August 22, 2020 14:25
Show Gist options
  • Save vldvel/22d17d1ef1f2a0d0ace122ba5c3554bb to your computer and use it in GitHub Desktop.
Save vldvel/22d17d1ef1f2a0d0ace122ba5c3554bb to your computer and use it in GitHub Desktop.
JavaScript is Palindrome
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