Drill

ProblemsJavaScript › warmup

Reverse the order of the words

easywarmupJavaScript

A text filter turns a sentence back to front, word by word, without disturbing the words themselves.

reverseWords(text: string) → string

Solve it in the editor →

Where you start

function reverseWords(text) {
  
}

Worked examples

CallResult
reverseWords("the quick brown fox")"fox brown quick the"
reverseWords(" a b ")"b a"
reverseWords("one")"one"
reverseWords("")""

Hint

Split on whitespace, reverse the list, join with a single space.

Reference solution in JavaScript
function reverseWords(text) {
  return text.split(/\s+/).filter((w) => w.length > 0).reverse().join(' ');
}

The same problem in another language

More warmup problems in JavaScript