Files
30-seconds-of-code/snippets/palindrome.md
Angelos Chalaris 0425ebefe7 Archive, multitagging, cleanup
Cleaned up the current snippets for consistency and minor problems, added multiple tags to most of them, archived a few.
2018-01-05 16:33:54 +02:00

24 lines
602 B
Markdown

### palindrome
Returns `true` if the given string is a palindrome, `false` otherwise.
Convert string `String.toLowerCase()` and use `String.replace()` to remove non-alphanumeric characters from it.
Then, `String.split('')` into individual characters, `Array.reverse()`, `String.join('')` and compare to the original, unreversed string, after converting it `String.tolowerCase()`.
```js
const palindrome = str => {
const s = str.toLowerCase().replace(/[\W_]/g, '');
return (
s ===
s
.split('')
.reverse()
.join('')
);
};
```
```js
palindrome('taco cat'); // true
```