function reverseWords(s) {
return s.split(' ').reverse().join(' ');
}
reverseWords('Hello World'); // 'World Hello'Reverse word order by splitting on spaces, reversing the array, and joining back.
Reverse the order of words in a string while keeping each word intact.
"hello world" β "world hello"
"JavaScript is fun" β "fun is JavaScript"
Algorithm:
function reverseWords(s: string): string {
// Reverse the order of words in s
}
reverseWords('hello world') // "world hello"