Code Lab

Modo entrevista. Temporizador de 5 min, sin pistas y sin autocompletado

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.

Ejemplos

truncate('Automation testing', 10)

Devuelve 'Automatio…'

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

Devuelve 'Hello'

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

Devuelve 'Hell…'

Fuente

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

Las pruebas ocultas se ejecutan al enviar