Code Lab

Modo entrevista. Cronômetro de 10 min, sem dicas e sem autocompletar

Words in reverse order

MédiaStrings10 min

Write a function reverseWords(sentence) that returns the words of the string in reverse order. The words themselves are not reversed: 'I Love Java' becomes 'Java Love I'.

  • Words are separated by one or more spaces.
  • In the result words are separated by exactly one space, with no spaces at the start or the end.
  • Punctuation stays part of the word.
  • For an empty string or a string of spaces return ''.

Exemplos

reverseWords('I Love Java')

Retorna 'Java Love I'

reverseWords('  one   two ')

Retorna 'two one'

Cobrada em Mphasis · Fonte

Carregando o editor...
  1. EntradareverseWords('I Love Java')
    Esperado'Java Love I'
  2. EntradareverseWords('Hello')
    Esperado'Hello'
  3. EntradareverseWords('')
    Esperado''

Os testes ocultos rodam no envio