Code Lab

Mode entretien. Minuteur de 10 min, sans indices ni autocomplétion

Words in reverse order

MoyenStrings10 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 ''.

Exemples

reverseWords('I Love Java')

Renvoie 'Java Love I'

reverseWords('  one   two ')

Renvoie 'two one'

Posé chez Mphasis · Source

Chargement de l'éditeur...
  1. EntréereverseWords('I Love Java')
    Attendu'Java Love I'
  2. EntréereverseWords('Hello')
    Attendu'Hello'
  3. EntréereverseWords('')
    Attendu''

Les tests cachés se lancent à la soumission