Intermediate
Trim to a Budget
The page holds six messages of 18 characters each. estimate(text) gives a rough token count: the length divided by 4 rounded up, plus 4 for the message wrapper.
Write sendable(messages, budget) so it keeps the newest messages that fit in the budget and drops the older ones. The newest message must always be kept, even if it is bigger than the whole budget.
With a budget of 30, #count shows how many messages would be sent. Clicking #huge replaces the history with one 400-character message: #count must show 1, not 0.
What Check tests
- Six small messages with a budget of 30: three are sent
- One huge message is still sent:
#countshows 1
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
Runs in your browser. Your code is saved here.
Your page
Press Run to see the page.
Console
Nothing logged yet.