DEV Community

Discussion on: From "hello world" to "world hello": Reversing the Words in a String

Collapse
 
mohammedmahers profile image
Muhammed Maher

I feel like the code is too verbose for this problem, can't we just do

function reverseWords(s) {
const arr = s.split(" ")
let res = []
for (let i = 0; i < arr.length; i++) {
res.push(arr[(arr.length-1)-i])
}
return res.join(" ")
}