Reversing the words within keeping their order the same JavaScript


In this problem statement, our aim is to reverse the words within keeping their order in the same order with the help of Javascript functionalities. So for solving this problem we can use traditional for loops and also built-in methods or Javascript.

Understanding the problem statement

The problem statement is aiming for a function which takes a string as input and will return a new string in which the order of the words should remain intact but in reversed direction. For example, we have given a string "Hello World" so the function should return "olleH dlroW", here "Hello" is reversed to "olleH'' and the "World" has been reversed with "dlroW". So here we can see that the order of the words within the original string has been maintained.

Logic for the given problem

For solving this problem we can use some in-built functions of Javascript and we can also solve it using the for loops which is a traditional way in the programming language.

So first we will split the input string into an array of words. And then reverse the order of the words within the array and then join the words back into a string. For every word we can reverse the order of its characters by splitting it into an array of characters. Then reversing the order of the characters and joining them back into the string.

Here we have to be careful that the order of the words should be intact. Means we must reverse the order of the words whilst preserving the order of the characters within every word.

Algorithm - Using for loops

Step 1 − Declare a function to reverse the words. And pass an argument in it.

Step 2 − Use two variables to keep track of words. If a space is found within the words then add them in an array called words. And word variables reset to an empty string to start creating the next word.

Step 3 − Loop through each character of words in the input string. And build each word by checking the space characters. So extract all words from the input string.

Step 4 − Use another loop to iterate over words array in reverse order and build a new array of reversed words. Inside the loop another loop will be used to iterate over every character within every word and build new strings in reverse order.

Step 5 − Every reversed word is then added into the reversed words array.

Step 6 − Concatenate the reversed words into a single string using space character as the separator.

Code for the algorithm - Using for loops

// function to reverse the words
function reverseWords(str) {
   let words = [];
   let word = '';
   for (let i = 0; i < str.length; i++) {
      if (str[i] === ' ') {
         words.push(word);
         word = '';
      } else {
         word += str[i];
      }
   }
   words.push(word);
   let reversedWords = [];
   for (let i = words.length - 1; i >= 0; i--) {
      let reversedWord = '';
      for (let j = words[i].length - 1; j >= 0; j--) {
         reversedWord += words[i][j];
      }
      reversedWords.push(reversedWord);
   }
   return reversedWords.join(' ');
}
console.log(reverseWords('hello tutorials point'));
console.log(reverseWords('this is a code'));
console.log(reverseWords('  leading  and  trailing  spaces  '));

Algorithm - Using Built-in method

Step 1 − Declare a function called reverseWords which is using an argument of string.

Step 2 − Split the input string into an array of words with the help of space characters.

Step 3 − Apply map method to the array to reverse the order of the character in each word.

Step 4 − And this is achieved by splitting every word into an array of characters.

Step 5 − Reverse the order of the characters using the reverse method.

Step 6 − Join them back into a string using the join method.

Code for the algorithm - Using the Built-in method

// function to reverse the words
function reverseWords(str) {
   return str.split(' ').map(word => word.split('').reverse().join('')).join(' ');
}
console.log(reverseWords('hello tutorials point'));
console.log(reverseWords('this is a code'));
console.log(reverseWords('  leading  and  trailing  spaces  '));

Complexity

The time complexity for both the code is O(n*m) in which n is the number of words in the input string and m is the average length of every word. So the traditional method may be slower than using built-in methods because it requires more explicit looping and character manipulations.

Conclusion

The usage of built in methods of Javascript provides a straightforward and efficient approach to the problem of reversing the order of words within the string. And traditional implementation provides an alternative solution for the same. While traditional methods can be a lengthy process because of the usage of multiple for loops.

Updated on: 18-May-2023

693 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements