Today’s problem is how to reverse the order of the letters in a word, and of each word in a sentence. It’s a fairly simple problem but one that shouldn’t be underestimated. It allows you to test your knowledge of arrays and strings.

The problem

link to the Kata

Complete the function that accepts a string parameter, and reverses each word in the string. All spaces in the string should be retained.

Examples

"This is an example!" ==> "sihT si na !elpmaxe"
"double  spaces"      ==> "elbuod  secaps"

The solution

a cute little robot grabbing a book. beautiful light. soft colour scheme, 8 k render

The first thing to do is to read the problem well. It’s not just about reversing the order of the letters in a word or phrase. It’s not just about reversing the order of the letters in a word or phrase. But it may be useful to start from this part, to understand how to deal with the problem.

So, I need a function that converts a string to an array of letters. I can do this using the spread operator.

const reverseString = (str: string): string[] => [...str];

After getting an array I use the Array.reverse() method to …reverse the order of the elements of the array.

const reverseString = (str: string): string[] => [...str].reverse();

Finally I concatenate the various elements again to obtain a string (I use the Array.join() method)

const reverseString = (str: string): string => [...str].reverse().join("");

Now I have a function that I can reuse to reverse the order of the letters in a word. I just have to apply it to a sentence. But first I have to break each sentence into words. I can do this using the String.split() method.

const words: string[] = str.split(" ");

To change each word I use the Array.map() method combined with the reverseString() function. This way I get this function:

const reverseString = (str: string): string => [...str].reverse().join("");

export function reverseWords(str: string): string {
  const words: string[] = str.split(" ");
  const invertedWords: string[] = words.map((w) => reverseString(w));
  const result: string = invertedWords.join(" ");
  return result;
}

I can refractor the function like this:

const reverseString = (str: string): string => [...str].reverse().join("");

export function reverseWords(str: string): string {
  const invertedWords: string = str
    .split(" ")
    .map((w) => reverseString(w))
    .join(" ");
  return invertedWords;
}

And then like this:

const reverseString = (str: string): string => [...str].reverse().join("");

export const reverseWords = (s: string): string =>
  s
    .split(" ")
    .map((w) => reverseString(w))
    .join(" ");

What about the JavaScript version? Well, I remove the types and I get:

const reverseString = (str) => [...str].reverse().join("");

export const reverseWords = (s) =>
  s
    .split(" ")
    .map((w) => reverseString(w))
    .join(" ");