Learning guides / AI coding courses
Coding practice · about 40 minutes
Refactor code while preserving observable behavior
Replace duplicated price logic with one helper while preserving zero-price items, empty lists and rounding behavior.
Is this the next skill you need?
Choose this if you can fix a defect but are unsure whether a cleanup silently changes behavior. Look for refactoring practice backed by behavior checks, not just demonstrations of code generation.
Preparation: You can edit a JavaScript function and run Node.js tests. This exercise uses integer cents and needs no package installation.
The time is our planning estimate for this small exercise, not a measured course completion time. If setup takes your whole session, save the command and error, then return after practicing the basics.
Your task and starter files
A fictional shop has two copies of discounted-total logic. Both calculate item discounts by rounding each item in cents before summing. A suggested cleanup sums raw discounted values and rounds only once, which changes a two-item result.
Download the original exercise ZIP
1,663 bytes · README, source, checks and reference answer. Extract the ZIP into one folder. Run the commands from that folder. No npm install or paid tool is needed.
node --test task.test.mjsExpected initial result: All 4 code tests pass; preserve them while removing duplication.
The examples are invented for learning. They are independent of provider courses and can be attempted without an AI assistant. If you use one, keep the requirement and supplied checks visible, then review its actual changes.
Work through it
- Run the four tests against the correct but duplicated starter. All four should pass.
- Extract a private helper shared by basketTotal and savedTotal. Preserve per-item rounding and handle an empty array naturally.
- Keep both exported function names and their parameters. Rerun the unchanged tests after refactoring.
- In a scratch copy, try rounding once after summing. Confirm the two 5-cent items at 10% discount expose the difference: 10 cents versus 9.
Check your result
Open the worked answer after your attempt
A shared helper can return items.reduce((sum, cents) => sum + Math.round(cents * (1-rate)), 0). Both exports call it. With two 5-cent items and rate 0.1, each item rounds 4.5 to 5, so the total is 10.
The ZIP includes a reference implementation or reference JSON. Use it to compare your reasoning after an attempt; a copied answer does not show that you can perform the task independently.
Keep as evidence: the original observation, your changed code or completed worksheet, the exact command and output, and a short explanation of the decision. When tests begin with one failure, the same unchanged tests should all pass after the repair. For the refactor, checks should pass both before and after.
Scope: The contract accepts nonnegative integer cents and rates from 0 to 1. Currency conversions, taxes, invalid inputs and floating-point policies outside these examples are separate work. Tests check behavior; inspect the diff to confirm duplication was actually removed.
Choose a lesson from the gap you found
If the exercise was hard because you could not run the code, work on language and command-line basics first. If running it was easy but explaining the change was hard, focus on the matching workflow lesson and repeat with fresh inputs.
Cursor Learn has official material on understanding code, developing changes, debugging, and reviewing work. Claude Code 101 covers agent context and a structured coding workflow. Use those lessons for instruction and this independent fixture to check your own result.
Public lesson access and tool usage are separate. Check current pricing, account requirements and usage limits before using either tool. A paid course is most useful when its assignments address the specific step you could not complete.
Find courses for a coding-assistant workflow →
Check the starting skills · Check free and paid access · Choose Claude Code or Cursor first