Modo entrevista. Temporizador de 10 min, sin pistas y sin autocompletado
Reverse each word and the whole string
MediaStrings10 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.
Ejemplos
reverseBoth('Hello World')Devuelve { eachWord: 'olleH dlroW', whole: 'dlroW olleH' }
reverseBoth('Hi Ann')Devuelve { eachWord: 'iH nnA', whole: 'nnA iH' }
Preguntada en JP Morgan · Fuente
Cargando el 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' }
Las pruebas ocultas se ejecutan al enviar