Code Lab

Modo entrevista. Cronômetro de 5 min, sem dicas e sem autocompletar

Print every word on its own line

FácilStrings5 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.

Exemplos

printWords('My Name is Nikhil')

Imprime My | Name | is | Nikhil

printWords('  Hi,   Ann! ')

Imprime Hi, | Ann!

Extra spaces are skipped, punctuation stays with the word.

Cobrada em Ness Digital · Fonte

Carregando o editor...
  1. EntradaprintWords('My Name is Nikhil')
    SaídaMy Name is Nikhil
  2. EntradaprintWords('Hello')
    SaídaHello
  3. EntradaprintWords('I love JavaScript')
    SaídaI love JavaScript
  4. Empty string: nothing is printed, not even an empty line

Os testes ocultos rodam no envio