Mode entretien. Minuteur de 10 min, sans indices ni autocomplétion
Capitalize every word
MoyenStrings10 min
Write a function capitalizeWords(text) that returns a copy of the string text in which every word starts with a capital letter: 'hello world' becomes 'Hello World'.
- A word is a run of characters without spaces. Words are separated by the space character
' 'only: a tab or a line break is part of a word, not a separator. - Make the first character of every word upper case with
toUpperCase(). If it is a digit or a sign, it stays the same:'3d model'becomes'3d Model'. - All other characters keep their letter case:
'hello wORLD'becomes'Hello WORLD'. An apostrophe does not start a new word:"don't"becomes"Don't". - Letters with accents count as letters:
'élan'becomes'Élan'. - Keep every space: several spaces in a row and spaces at the start or the end stay where they are. For an empty string return
''.
You can walk over the characters, or split the string on spaces and join the words back. Either way, two spaces in a row are still two spaces in the result (whitespace is not removed).
Exemples
capitalizeWords('hello world')Renvoie 'Hello World'
capitalizeWords('hello wORLD')Renvoie 'Hello WORLD'
capitalizeWords(' two spaces ')Renvoie ' Two Spaces '
Posé chez T-Bank · Source
Chargement de l'éditeur...
- Entrée
capitalizeWords('hello world')Attendu'Hello World' - Entrée
capitalizeWords('hello wORLD')Attendu'Hello WORLD' - Entrée
capitalizeWords('')Attendu''
Les tests cachés se lancent à la soumission