Reverse Words in a String
- javascript
- typescript
- string
Problem URL:Reverse Words in a String
My Solution
JavaScript / TypeScript
const reverseWords = (s: string): string => {
let sCopy = s
.trim()
.split(" ")
.filter((word) => word.length > 0),
left = 0,
right = sCopy.length - 1;
while (left < right) {
[sCopy[left], sCopy[right]] = [sCopy[right], sCopy[left]];
left += 1;
right -= 1;
}
return sCopy.join(" ");
};
Let's Connect
Twitter •GitHub •LinkedIn