Reversing a string while maintaining the position of spaces in JavaScript

JavascriptWeb DevelopmentFront End Technology

Problem

We are required to write a JavaScript function that takes in a string that might contain some spaces.

Our function should reverse the words present in the string internally without interchange the characters of two separate words or the spaces.

Example

Following is the code −

 Live Demo

const str = 'this is normal string';
const reverseWordsWithin = (str = '') => {
   let res = "";
   for (let i = str.length - 1; i >= 0; i--){
      if(str[i] != " "){
         res += str[i];
      };
      if(str[res.length] == " "){
         res += str[res.length];
      };
   };
   return res;
};
console.log(reverseWordsWithin(str));

Output

gnir ts lamron sisiht
raja
Published on 20-Apr-2021 09:24:46
Advertisements