Mode entretien. Minuteur de 5 min, sans indices ni autocomplétion
Print every word on its own line
FacileStrings5 min
Write a function printWords(sentence) that prints every word of a string on its own line with console.log.
- Words are separated by one or more spaces.
sentencecan also start and end with spaces. - Print the words in their order, one word per line, with no spaces around the word.
- A word is any group of characters between spaces, so punctuation stays with the word:
'Hi, Ann!'has the wordsHi,andAnn!. - Do not print empty lines. If there are no words (an empty string or only spaces), print nothing.
- The function does not need to return a value.
Exemples
printWords('My Name is Nikhil')Affiche My | Name | is | Nikhil
printWords(' Hi, Ann! ')Affiche Hi, | Ann!
Posé chez Ness Digital · Source
Chargement de l'éditeur...
- Entrée
printWords('My Name is Nikhil')SortieMy Name is Nikhil - Entrée
printWords('Hello')SortieHello - Entrée
printWords('I love JavaScript')SortieI love JavaScript - Empty string: nothing is printed, not even an empty line
Les tests cachés se lancent à la soumission