Code Lab

Reverse the words and capitalize them

MediumStrings10 min

Write a function reverseAndCapitalize(text) that returns the words of the string text in reverse order, and every word starts with a capital letter: 'India is great' becomes 'Great Is India'.

  • Words in text are separated by one or more spaces. There can be spaces at the start and at the end. A word is any group of characters without spaces, so it can contain ' or -.
  • In every word the first character becomes upper case and all other characters become lower case: 'wORLD' becomes 'World'. Change the letter case with toUpperCase() and toLowerCase(); a digit stays as it is.
  • The letters inside a word keep their order. Only the order of the words changes.
  • In the result, words are separated by exactly one space, with no spaces at the start or the end.
  • For an empty string or a string of spaces return ''.

You can split the string into words, reorder them and join them back.

Examples

reverseAndCapitalize('India is great')

Returns 'Great Is India'

reverseAndCapitalize('hello WORLD')

Returns 'World Hello'

reverseAndCapitalize('  one   two ')

Returns 'Two One'

Asked at EPAM · Source

Loading the editor...
  1. InputreverseAndCapitalize('India is great')
    Expected'Great Is India'
  2. InputreverseAndCapitalize('hello WORLD')
    Expected'World Hello'
  3. InputreverseAndCapitalize('')
    Expected''

Hidden tests run on Submit.