Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 25, 2021 10:02 pm GMT

Technical Interview 2: Reverse a string

Interview Question #1:

Write a function that reverses a string.

If you need practice, try to solve this on your own. I have included 3 potential solutions below.

Note: There are many other potential solutions to this problem.

Solution #1: Array methods

function reverseString(str) {    return str.split("").reverse().join("");}

Solution #2: array forEach

function reverseString(str) {    let reversedString = ''    str.split('').forEach(char => {        reversedString = char + reversedString    })    return reversedString}

Solution #3: array reduce

function reverseString(str) {    return str.split('')        .reduce((prev, curr) => curr + prev, '')}

In case you like a video instead of bunch of code

Happy coding and good luck if you are interviewing!


Original Link: https://dev.to/frontendengineer/technical-interview-1-reverse-a-string-33pb

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To