Member-only story
String reversals are one of the most common questions on tech interviews. There are many different ways to do this and one way I’d like to do this is by using the built in JavaScript methods.
function reverse(string) {return str.split('').reverse().join('')}reverse('hello there')
Breaking down each method:
We call .split('') on the the string which separates each character into its own element. It returns ["h", "e", "l", "l", "o", " ", "t", "h", "e", "r", "e"]
Then we chain .reverse() which reverses the elements order.
It returns [“e”, “r”, “e”, “h”, “t”, “ “, “o”, “l”, “l”, “e”, “h”]
Finally we call .join(‘’) to combine each character element together to create a string again.
It returns “ereht olleh”