Mode entretien. Minuteur de 10 min, sans indices ni autocomplétion
Reverse one word in a sentence
MoyenStrings10 min
Write a function reverseWord(sentence, word) that returns the string sentence where every word equal to word is written backwards. All other words stay as they are.
sentencehas words separated by single spaces, with no spaces at the start or the end and no punctuation. It can be empty.wordis one word: at least one letter, no spaces.- Only a whole word that is exactly equal to
wordchanges. Case matters:'amazon'is not'Amazon'. - A longer word that has
wordas a substring does not change:'Amazonian'stays'Amazonian'. - Every match is reversed, not only the first one.
- If there is no match, return
sentenceunchanged. An emptysentencegives''.
Tip: split the sentence into words, check each word, then join the words back.
Exemples
reverseWord('I am working for Amazon', 'Amazon')Renvoie 'I am working for nozamA'
reverseWord('Amazon sells Amazonian books', 'Amazon')Renvoie 'nozamA sells Amazonian books'
Posé chez Amazon · Source
Chargement de l'éditeur...
- Entrée
reverseWord('I am working for Amazon', 'Amazon')Attendu'I am working for nozamA' - Entrée
reverseWord('I am working for Google', 'Amazon')Attendu'I am working for Google'
Les tests cachés se lancent à la soumission