Code Lab

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. sentence can 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 words Hi, and Ann!.
  • 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!

Extra spaces are skipped, punctuation stays with the word.

Posé chez Ness Digital · Source

Chargement de l'éditeur...
  1. EntréeprintWords('My Name is Nikhil')
    SortieMy Name is Nikhil
  2. EntréeprintWords('Hello')
    SortieHello
  3. EntréeprintWords('I love JavaScript')
    SortieI love JavaScript
  4. Empty string: nothing is printed, not even an empty line

Les tests cachés se lancent à la soumission