# Reverse Words in String

![](https://3743232000-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHW2IQuh2PFpWJDvBz2FF%2Fuploads%2Fgit-blob-d5e5a02651c337f9b7e53471e2e44e2b99186d8a%2FScreenshot%202023-02-05%20at%2014.27.30.png?alt=media)

* n, n

```tsx
function reverseWordsInString(str) {

  const arr = [];
  let word = [];
  for (let i = 0; i < str.length; i++) {
    const curr = str[i];
    if (curr !== ' ') {
      word.push(curr);
      continue;
    }
    
    arr.unshift(...word);
    arr.unshift(curr)
    word = [];
  }

  if (word.length > 0) 
    arr.unshift(...word);

  return arr.join('');
}

// Do not edit the line below.
exports.reverseWordsInString = reverseWordsInString;
```
