Today, we’ll write a function that inverts a string. It will look like this:

"abcdefg" -> "gfedcba"
Copy the code

Array.prototype.reverse()

The easiest operation is to use the array.prototype.reverse () method, which reverses the position of the elements in the Array and returns the Array. The first element of the array becomes the last, and the last element of the array becomes the first. This method changes the original array.

  • useString.prototype.split()Method to convert a string to an array.
  • useArray.prototype.reverse()Method reverses it and then usesArray.prototype.join()Method converts it back to a string
const reverseString = str= > str.split(' ').reverse().join(' ')

console.log(reverseString('abcdefg')) // "gfedcba"
Copy the code

The For loop

Creates an empty string that will hold the reverse string, loop over each character in the string, and append it to the beginning of the new string.

const reverseString = str= > {
  let result = ' '

  for (let i = str.length - 1; i >= 0; i--) {
    result += str[i]
  }

  return result
}

console.log(reverseString('abcdefg')) // "gfedcba"
Copy the code

Use for… of

const reverseString = str= > {
  let result = ' '
  
  for (let character of str) {
    result += character
  }
  
  return reverseString
}

console.log(reverseString('abcdefg')) // "gfedcba"
Copy the code

Array.prototype.reduce()

The array.prototype.reduce () method performs a reducer function provided by you (in ascending order) on each element in the Array, summarizing its results into a single return value.

  • Convert a String to an array using the string.prototype.split () method. You can also use the extension operator, depending on you.

  • Convert this to a string using the array.prototype.reduce () method.

const reverseString = str= > str.split(' ').reduce((rev, char) = > char + rev, ' ')

console.log(reverseString('abcdefg')) // "gfedcba"
Copy the code

You can also use the Array. The prototype. ReduceRight ()

const reverseString = (str) = > [...str].reduceRight((acc, cur) = > acc + cur)

console.log(reverseString('abcdefg')) // "gfedcba"
Copy the code

Array.prototype.sort

Using array.prototype. sort to reverse strings is not covered here, as with the other methods above. How else can you sort an Array of objects in JavaScript? See how sort is used.

function reverseString(str) {
  return str
    .split(' ')
    .sort(() = > -1)
    .join(' ');
}

console.log(reverseString('abcdefg')) // "gfedcba"
Copy the code

recursive

Recursion is a way to solve a problem by using a function that calls itself. Each time a function calls itself, it reduces the problem to a subproblem. This recursive call continues until the point at which the subproblem can be invoked without further recursion is reached.

Note: Give the recursion the correct termination condition to ensure it does not lead to an infinite loop.

  • Recursion terminates if the string is empty. Recursion stops.

  • Using String. The prototype. The substring () method to capture and remove the first character in the String, and then passed to the function of other characters. Append the first character to the return statement.

const reverseString = str= > str ? reverse(str.substring(1)) + str[0] : str

console.log(reverseString('abcdefg')) // "gfedcba"
Copy the code