DEV Community

Cover image for #7DaysJS: Reverse a String
Lautaro Lobo
Lautaro Lobo

Posted on • Originally published at lautarolobo.xyz on

1 1

#7DaysJS: Reverse a String

Welcome to day 4 of 7 Days of JavaScript!

Today we will see how to reverse a string. It’s probably the most asked question ever on technical interviews.

So the statement is quite simple, although the problem has an unexpected complexity when you face it for the first time: write a function that takes a string and returns the string reversed.

Here’s an example:

reverseString('Japan')
...
'napaJ'

Enter fullscreen mode Exit fullscreen mode

Give it a try.

You can see the solutions I made here.

Top comments (2)

Collapse
 
bharath_bheemireddy profile image
Bharath_Bheemireddy

const reverseString = str => str.split('').reverse().join('');
Collapse
 
lautarolobo profile image
Lautaro Lobo

That's I think the best way to do it!

11 Tips That Make You a Better Typescript Programmer

typescript

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay