Member-only story

Algorithms: String Reversal

Jay Wen
1 min readJul 30, 2020

--

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”

--

--

Jay Wen
Jay Wen

Written by Jay Wen

Hey! I’m a full stack software engineer. Here’s where I document my technical learning and any tips/skills I can share as I continue to learn. :)

No responses yet