diff --git a/README.md b/README.md index 9914e2d30..bbadea755 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ * [Random number in range](#random-number-in-range) * [Randomize order of array](#randomize-order-of-array) * [Redirect to url](#redirect-to-url) +* [Reverse a string](#reverse-a-string) * [RGB to hexadecimal](#rgb-to-hexadecimal) * [Scroll to top](#scroll-to-top) * [Similarity between arrays](#similarity-between-arrays) @@ -263,7 +264,7 @@ var objectFromPairs = arr => ### Powerset -Use `reduce()` combined with `map()` to iterate over elements and combine into an array containing all combinations. +Use `reduce()` combined with `map()` to iterate over elements and combine into an array containing all combinations. ```js var powerset = arr => @@ -298,10 +299,11 @@ var redirect = (url, asLink = true) => ### Reverse a string -Use `reverse()` to reverse order of elements in `destructed` array. Combine elements to get a string using `join('')`. +Use array destructuring and `Array.reverse()` to reverse the order of the characters in the string. +Combine characters to get a string using `join('')`. ```js -var reverseString = str => [...str].reverse().join(''); +var reverseString = str => [...str].reverse().join(''); ``` ### RGB to hexadecimal diff --git a/snippets/reverse-a-string.md b/snippets/reverse-a-string.md new file mode 100644 index 000000000..5e7c41e7d --- /dev/null +++ b/snippets/reverse-a-string.md @@ -0,0 +1,8 @@ +### Reverse a string + +Use array destructuring and `Array.reverse()` to reverse the order of the characters in the string. +Combine characters to get a string using `join('')`. + +```js +var reverseString = str => [...str].reverse().join(''); +```