Skip to the content

The shortened text

Problem

Long texts don't fit on the card. Write a function that takes a text and a limit of letters: if the text is that long or shorter, you return it as it is; if it goes over, you return its first limit letters with "..." stuck at the end. With "good morning" and 4 you return "good...".

The three dots don't count toward the limit, and the text can be empty.

Examples

  • "good morning", 4 → "good..."

  • "hi", 10 → "hi"

  • "programming", 7 → "program..."

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def shorten(text, limit):
    pass

JavaScript

function shorten(text, limit) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. It is free and you do not need an account to start.

More O(1) challenges

See all challenges →