Description
- Complete the solution so that it reverses all of the words within the string passed in.
Solution 01
function reverseWords(str) {
const arr = str.split(' ');
let result = [];
for (let i = arr.length - 1; i >= 0; i--) {
result.push(arr[i]);
}
return result.join(' ');
}
reverseWords('Codewars'); // Codewars
reverseWords('Hello World'); // World Hello
reverseWords('How are you?'); // you? are How
Solution 02
function reverseWords(str) {
return str.split(' ').reverse().join(' ');
}
reverseWords('Codewars'); // Codewars
reverseWords('Hello World'); // World Hello
reverseWords('How are you?'); // you? are How