Files
30-seconds-of-code/snippets/palindrome.md
Angelos Chalaris 97c250bcfb Updated examples
2017-12-27 16:35:25 +02:00

18 lines
523 B
Markdown

### palindrome
Returns `true` if the given string is a palindrome, `false` otherwise.
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()`.
```js
const palindrome = str => {
const s = str.toLowerCase().replace(/[\W_]/g,'');
return s === s.split('').reverse().join('');
}
```
```js
palindrome('taco cat') // true
```