Write a function reverseWordOrder(sentence) that returns a new string with the words in reverse order. The spaces between the words stay where they are: 'This is sample text' becomes 'text sample is This'.
' Hello world ' becomes ' world Hello '.If you split on one space and join back with one space, the gaps move together with the words. A loop or a regular expression that finds the words and the gaps works better.
reverseWordOrder('This is sample text')Returns 'text sample is This'
reverseWordOrder(' Hello world ')Returns ' world Hello '
reverseWordOrder('a b c')Returns 'c b a'
Asked at Amazon · Source
reverseWordOrder('This is sample text')'text sample is This'reverseWordOrder(' Hello world ')' world Hello 'reverseWordOrder('Hello')'Hello'Hidden tests run on Submit.