Training Grounds
First Apply Always Shows Zero DiscountBlue Belt
+25 Ki
Description

A checkout panel has three preset discount buttons. Clicking a button should apply that discount immediately and update the total

Anomaly

Click any discount button - the Discount line stays at 0% and Total stays at $100. The code label updates to show the button you clicked but the numbers never change. Click the same button a second time and the discount from the first click finally appears.

Constraint
Do not replace useReducer with useState
Do not change the dispatch calls or the button layout
Hint

Inside the reducer, the new value arrives in one place and the current value lives in another - which one are you reading?

Consult the SenseiOnly for those truly stuck · Flip to reveal

Inside the reducer, the new value arrives in one place and the current value lives in another - which one are you reading?

Loading editor…
Correct Solution
Loading...

Why this fixes it

The reducer read CODES[state.code] to compute the discount — but state is the state from before this action ran, so it always looked up the previously applied code, not the one dispatched. On the first click from a clean state, state.code was an empty string so the lookup returned undefined and discount stayed 0%. The fix changes state.code to action.code, which is the code that was just dispatched. The reducer now computes the correct discount immediately from the current action.

Expected OutputGoal State
Your OutputLive