diff --git a/README.md b/README.md index 1e8ec7625..c3bd1e001 100644 --- a/README.md +++ b/README.md @@ -7259,18 +7259,12 @@ pad('foobar', 3); // 'foobar' 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()`. +Then, use the spread operator (`...`) to split string 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('') - ); + return s === [...s].reverse().join(''); }; ``` diff --git a/docs/string.html b/docs/string.html index 5978a0e16..6d7eaed53 100644 --- a/docs/string.html +++ b/docs/string.html @@ -148,15 +148,9 @@
pad('cat', 8); // ' cat ' pad(String(42), 6, '0'); // '004200' pad('foobar', 3); // 'foobar' -
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().
const palindrome = str => { +
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, use the spread operator (...) to split string into individual characters, Array.reverse(), String.join('') and compare to the original, unreversed string, after converting it String.tolowerCase().
const palindrome = str => { const s = str.toLowerCase().replace(/[\W_]/g, ''); - return ( - s === - s - .split('') - .reverse() - .join('') - ); + return s === [...s].reverse().join(''); };
palindrome('taco cat'); // true
Returns the singular or plural form of the word based on the input number. If the first argument is an object, it will use a closure by returning a function that can auto-pluralize words that don't simply end in s if the supplied dictionary contains the word.
If num is either -1 or 1, return the singular form of the word. If num is any other number, return the plural form. Omit the third argument to use the default of the singular word + s, or supply a custom pluralized word when necessary. If the first argument is an object, utilize a closure by returning a function which can use the supplied dictionary to resolve the correct plural form of the word.
const pluralize = (val, word, plural = word + 's') => {