Reverse The Prefix Of A Word

04/17/22

Intro

For this algorithm, we are given a string and a specific character. The goal is to find the first occurrence of that character in the string, and then reverse everything from the beginning of the string up to that character (inclusive). If the character does not exist in the string, just return the original string without performing any operations.

For example, if our string is abcdefd and our character is d, then we should reverse the segment that starts at index 0 of our string, and ends at index 3 inclusive. The resulting string will be dcbaefd.

Getting Started

First off, let's set up a basic function.


/**
 * @param {string} str
 * @param {string} char
 * @return {string}
 */
const reversePrefix = (str, char) => {
  // do stuff here...
}

Now that we have that setup, what's should we tackle next? The next logical step is to find where our given char is located inside of str. We can do this easily by using the String.indexOf method.


/**
 * @param {string} str
 * @param {string} char
 * @return {string}
 */
const reversePrefix = (str, char) => {
  const index = str.indexOf(char)

  if(index <= 0) {
    return str
  }
}

Notice how I immediately check to see if index is equal to -1. This is due to the fact that when using indexOf, it will return -1 if the character was not found. In that case, we don't have to do anything, and can simply return the original string.

Converting To Array & String Slicing

Now that we have our edge case setup, we can tackle the rest of our function's duties. The first thing we need to do is handle finding our "prefix", and reverse it. We could slice the string starting at index 0 and up to our index + 1 and reverse it. However, it's not that simple, seeing as strings in JavaScript are immutable, and do not allow us to perform operations like reversing, inserting, deletions etc. We can still slice the string, but after that we need to convert that substring into an array. From there, it's simple as using the Array.reverse method.


/**
 * @param {string} str
 * @param {string} char
 * @return {string}
 */
const reversePrefix = (str, char) => {
  const index = str.indexOf(char)

  if(index <= 0) {
    return str
  }

  const prefix = str.slice(0, index + 1).split('').reverse()
}

This should give us what we want. After that, we need to create a substring from the original str that contains everything after our index. We can then convert our prefix array back into a string, and concatenate it with this newly created substring.


/**
 * @param {string} str
 * @param {string} char
 * @return {string}
 */
const reversePrefix = (str, char) => {
  const index = str.indexOf(char)

  if(index <= 0) {
    return str
  }

  const prefix = str.slice(0, index + 1).split('').reverse()
  const rest = str.slice(index + 1)

  return prefix.join('') + rest
}

And that's it folks. If you have any problems understanding any of this, feel free to shoot me a message, and I'll make sure it's crystal clear.

Want To Level Up Your JavaScript Game?

Book a private session with me, and you will be writing slick JavaScript code in no time.