Modo entrevista. Cronômetro de 10 min, sem dicas e sem autocompletar
Reverse each word and the whole string
MédiaStrings10 min
Write a function reverseBoth(sentence) that reverses a string in two ways and returns both results in an object { eachWord, whole }.
eachWord: the characters of every word are reversed, but the words stay in their order.'Hello World'gives'olleH dlroW'.whole: the whole string reversed character by character.'Hello World'gives'dlroW olleH'.- Words are separated by spaces. A word is any group of characters between spaces, so punctuation is part of the word:
'Hi!'gives'!iH'. - In
eachWordevery space stays where it is: double spaces, spaces at the start and spaces at the end too. The input has only spaces as whitespace, no tabs or line breaks. - For the empty string both fields are
''.
You can split the string into words and join them back, but keep every space.
Exemplos
reverseBoth('Hello World')Retorna { eachWord: 'olleH dlroW', whole: 'dlroW olleH' }
reverseBoth('Hi Ann')Retorna { eachWord: 'iH nnA', whole: 'nnA iH' }
Cobrada em JP Morgan · Fonte
Carregando o editor...
- Entrada
reverseBoth('Hello World')Esperado{ eachWord: 'olleH dlroW', whole: 'dlroW olleH' } - Entrada
reverseBoth('I Love Java')Esperado{ eachWord: 'I evoL avaJ', whole: 'avaJ evoL I' } - Entrada
reverseBoth('Selenium')Esperado{ eachWord: 'muineleS', whole: 'muineleS' }
Os testes ocultos rodam no envio