Code Lab

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

Truncate a string

FácilStrings5 min

Write a function truncate(text, maxLength). If the string is longer than maxLength, cut it and put one … character at the end, so the result is exactly maxLength long. A string that fits in maxLength comes back unchanged.

Exemplos

truncate('Automation testing', 10)

Retorna 'Automatio…'

Nine letters and the ellipsis, 10 characters in total.
truncate('Hello', 5)

Retorna 'Hello'

The length is exactly 5, nothing to cut.
truncate('Hello!', 5)

Retorna 'Hell…'

Fonte

Carregando o editor...
  1. Entradatruncate('Automation testing', 10)
    Esperado'Automatio…'
  2. Entradatruncate('Hello', 5)
    Esperado'Hello'
  3. Entradatruncate('Hello!', 5)
    Esperado'Hell…'

Os testes ocultos rodam no envio