Build an expense splitter! Two number inputs hold a bill total and a head count, and the component works out what each person owes.

The component must render an <input class="total" type="number"> for the bill total and an <input class="people" type="number"> for the number of people. Both must update the component as the user types, so wire them with oninput.

When there is at least one person, render a <p class="share"> whose text is exactly Each pays: $ followed by the amount owed rounded to two decimal places — for example Each pays: $25.00. Use toFixed(2) so the cents are always shown.

When the head count drops below 1, render a <p class="error"> reading exactly Enter at least one person instead of the share paragraph. Swap between the two with lwc:if and lwc:else.

The total starts at 100 and the head count starts at 4.

Examples:

Initial render: <input class="total" type="number" value="100" /> <input class="people" type="number" value="4" /> <p class="share">Each pays: $25.00</p> Type 90 into the total input -> <p class="share">Each pays: $22.50</p> Type 3 into the people input -> <p class="share">Each pays: $33.33</p> Type 0 into the people input -> <p class="error">Enter at least one person</p>
LWC

Preview

Press Run & Test to compile and preview your component.

Test results

Tests run automatically after each compile.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In