Command Palette

Search for a command to run...

Hectal
Phase 4: Conversation State
AI Frontend · Exercise 3 of 5 PreviousNext
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

  1. Six small messages with a budget of 30: three are sent
  2. One huge message is still sent: #count shows 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.