Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

reverse a string while keeping spaces in javascript

const reverseStr = (input) => {
  const revArr = input.replaceAll(' ', '').split('').reverse();
  for (let i = 0; i < revArr.length; i++) {
    if (input[i] === ' ') revArr.splice(i, 0, ' ');
  }
  return revArr.join('');
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #reverse #string #keeping #spaces #javascript
ADD COMMENT
Topic
Name
6+9 =