Day 3 Coding/Blog Challenge

·

4 min read

Today's challenge... is very interesting, somehow I keep getting string mutation challenges, but it's sharpening my skills with strings and arrays. I think I came up with an interesting solution to the problem. See what you think.

Die Herausforderung(The Challenge)

I got this one from CodeWars

Write a function that takes in a string of one or more words, and returns the same string, but with all five or more character words reversed.

  • Strings passed in will consist of only letters and spaces.
  • Spaces will be included only when more than one word is present.

Examples:

spinWords("Hey fellow warriors") => "Hey wollef sroirraw" 
spinWords("This is a test") => "This is a test" 
spinWords("This is another test") => "This is rehtona test"

Trust the process

Naturally, we want to first create our function outline. I went the route of splitting all words into an array. So, immediately, I know that I will need to initialize a variable to split the words the string contains. Of course, I also know that I want to split words at the space because that's how we know where one word ends and the next begins.

function spinWords(string) {
    //split string into array of words
    let strContainer = string.split(' ')

    return strContainer;
}

After this we want to loop over the new array of words so that we can check each word to see if it has 5 or more characters. So, we initialize the loop and set a condition check for the number of characters.

function spinWords(string) {
    //split string into array of words
    let strContainer = string.split(' ')
    //loop over the string
    for (let i = 0; i < strContainer.length; i++) {
        //if current word 5 char or more, do something
        if (strContainer[i].length >= 5) {

        }
    }
    return strContainer;
}

If the current word has 5 or more characters, we want to reverse it. For the 3rd day in a row, we get hammered with JavaScript string immutability. Cool, by now we should have the hang of this. We want to create a new word that is the original word but reversed. That means let's create a variable to hold our new word.

This was the fun part.

Next, we want to write the code that will reverse this word. My idea was to loop backwards over the current word and build our new reversed word character by character. Finally, we reassign the value of the original word to be equal to our reversed word.

function spinWords(string) {
    let strContainer = string.split(' ')
    for (let i = 0; i < strContainer.length; i++) {
        if (strContainer[i].length >= 5) {

            //variable to hold out reversed word
            let reverseStr = ""

            //loop over current word in reverse & build reverse word char by char
            for (let j = strContainer[i].length - 1; j >= 0; j--) {
                reverseStr += strContainer[i][j]
            }

            //reassign old word to reversed word
            strContainer[i] = reverseStr
        }
    }
    return strContainer;
}

After this, we need to convert our array back into a string. I do this with the String method toString(), but initially also thought of the String function, either would work.

Cool, now we have this as a string, except there is one problem now we are returning all the strings separated by commas just how the values were separated in the array. I fix this problem by using the String replace method. We want to globally replace all commas in this new string with a single white space.

function spinWords(string) {
    let strContainer = string.split(' ')
    for (let i = 0; i < strContainer.length; i++) {
        if (strContainer[i].length >= 5) {
            let reverseStr = ""
            for (let j = strContainer[i].length - 1; j >= 0; j--) {
                reverseStr += strContainer[i][j]
            }
            strContainer[i] = reverseStr
        }
    }
    //convert array to string
    strContainer = strContainer.toString();
    //replace ALL commas with a single white space
    strContainer = strContainer.replace(/,/g, ' ')

    return strContainer;
}

Viola!

This was fun to me, especially reverse looping and then also learning about globally replacing characters in a string. This is nice because you don't have to run another loop and conditional to check if each character at the current index is a comma.