Prepare repository for merge
This commit is contained in:
24
javascript/snippets/palindrome.md
Normal file
24
javascript/snippets/palindrome.md
Normal file
@ -0,0 +1,24 @@
|
||||
---
|
||||
title: Palindrome
|
||||
type: snippet
|
||||
tags: [string]
|
||||
cover: bridge-drop
|
||||
dateModified: 2020-10-22T20:24:04+03:00
|
||||
---
|
||||
|
||||
Checks if the given string is a palindrome.
|
||||
|
||||
- Normalize the string to `String.prototype.toLowerCase()` and use `String.prototype.replace()` to remove non-alphanumeric characters from it.
|
||||
- Use the spread operator (`...`) to split the normalized string into individual characters.
|
||||
- Use `Array.prototype.reverse()`, `Array.prototype.join()` and compare the result to the normalized string.
|
||||
|
||||
```js
|
||||
const palindrome = str => {
|
||||
const s = str.toLowerCase().replace(/[\W_]/g, '');
|
||||
return s === [...s].reverse().join('');
|
||||
};
|
||||
```
|
||||
|
||||
```js
|
||||
palindrome('taco cat'); // true
|
||||
```
|
||||
Reference in New Issue
Block a user