String Reverser

Your string reversed is:

App by David Orson

The code used for the functionality of this challenge is:

const reverser = (string) => {
setReversedString(string.split('').reverse().join('')}
}

)

This is a simple coding challenge for reversing a string.

There is quite an easy way to execute this using helper methods built into JS.

The first helper method 'split('')' separates each character into a string of an array, the second '.reverse()' reverses the order of the strings in the array and the ''.join('')' method takes all of the strings in the array and concatenates them into a single string.

We then would usually return this new string, but when using React, we just put this new string in state by calling 'setReversedString()' so that it can be used in a different component.

Another solution would be to initialize a variable 'reversed' and iterate over it using a for loop. For each iteration you concatenate the newly modified reversed string to the character which is being addressed in the current iteration.

The code for this could be as follows:

const reverser = (string) => {
let reversed = '';

for (let character of string) {
reversed = character + reversed;
}

setReversedString(reversed)
}

Another Solution that could be used is to use the reduce function on the array with much the same logic as the previous solution.

We first split the string into an array of the characters using the '.split()' method, then we use the reduce function to iterate over the array and reduce it into a single value, being a string. The reducer function takes two arguments as a higher order function, the first being a function with the arguments 'rev' and 'char', the second argument is the initialization value which we set as an empty string.

We contain all of this logic inside the 'setReversedString()' React function for use inside other components of the app.

const reverser = (string) => {
setReversedString(string.split('').reduce((rev,char) => char + rev,''))
}