diff --git a/snippets/Check_for_palindrome.md b/snippets/Check_for_palindrome.md new file mode 100644 index 000000000..4abd6ad1d --- /dev/null +++ b/snippets/Check_for_palindrome.md @@ -0,0 +1,8 @@ +### Check for palindrome + +Convert string `toLowerCase()` and use `replace()` to remove non-alphanumeric characters from it. +Then, `split('')` into individual characters, `reverse()`, `join('')` and compare to the original, unreversed string, after converting it `tolowerCase()`. + +``` +palindrome = str => (str.toLowerCase().replace(/[\W_]/g,'').split('').reverse().join('')==str.toLowerCase().replace(/[\W_]/g,'')); + ```