Code Lab

Mode entretien. Minuteur de 5 min, sans indices ni autocomplétion

Truncate a string

FacileStrings5 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.

Exemples

truncate('Automation testing', 10)

Renvoie 'Automatio…'

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

Renvoie 'Hello'

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

Renvoie 'Hell…'

Source

Chargement de l'éditeur...
  1. Entréetruncate('Automation testing', 10)
    Attendu'Automatio…'
  2. Entréetruncate('Hello', 5)
    Attendu'Hello'
  3. Entréetruncate('Hello!', 5)
    Attendu'Hell…'

Les tests cachés se lancent à la soumission