Divide numbers #4
Labels
No labels
aira:blocked
aira:cancelled
aira:ci
aira:done
aira:implementing
aira:merging
aira:plan-review
aira:planning
aira:qa
aira:queued
aira:ready
aira:review
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
lab/web-calculator-3#4
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Add division with safe handling of division by zero.
Requirements
op=dividereturnsa / b.400 {"error": "division by zero"}; neverInfinityorNaN.÷button. Pressing9,÷,3,=shows3; dividing by zero showsErroron the display, andCclears it.Acceptance criteria
/api/calculate?a=9&b=3&op=dividereturns{"result":3}./api/calculate?a=1&b=4&op=dividereturns{"result":0.25}./api/calculate?a=1&b=0&op=dividereturns 400{"error":"division by zero"}.ErrorandCresets the display.Aira · queued
Aira picked up this task. Branch
aira/4-divide-numbers.Aira · planning
Orchestrator thread started (composer-2.5). Planning on
aira/4-divide-numbers.T3 thread:
mcp:launch:3efade16fa57248f0ed946be6eb81d8ffa305c8c0da887179c57696a45ca124cAira · Plan (round 1) at
b0807bd1Added docs/plans/issue-4.md (goal, AC1–4, divide-by-zero design, files, test plan, risks), committed, pushed aira/4-divide-numbers; npm test green.
docs/plans/issue-4.md
Issue #4: Divide numbers
Goal
Extend the calculator with division end-to-end: handle
op=divideinlib/calculator.jsasa / bwith explicit rejection of division by zero, expose it through the existingGET /api/calculateroute (unchanged contract), and add a÷(U+00F7 division sign) keypad button that behaves like+,−, and×. The flow9,÷,3,=must display3via the API. Dividing by zero must return HTTP 400 with{"error":"division by zero"}(neverInfinityorNaNin the JSON body); the UI must showErroron the display, andCmust clear it. Addition, subtraction, multiplication, validation, and other error responses remain unchanged.Acceptance criteria
GET /api/calculate?a=9&b=3&op=dividereturns HTTP 200 with body{"result":3}.GET /api/calculate?a=1&b=4&op=dividereturns HTTP 200 with body{"result":0.25}.GET /api/calculate?a=1&b=0&op=dividereturns HTTP 400 with body{"error":"division by zero"}(notInfinity/NaN)./includes a divide button;9÷3=shows3; division by zero showsErroron the display;Cresets the display (including after an error).Design
Calculator module (
lib/calculator.js)calculate(a, b, op)with a branch forop === 'divide':parseNumberyieldsxandy, ify === 0, thrownew Error('division by zero')before performingx / y(JavaScript division would yieldInfinity/NaN, which must not be returned as a successfulresult).x / y; if the result is not finite, throwresult out of range(parity with add / subtract / multiply).add,subtract,multiply, anddivideonly; all other ops throwunknown operation.parseNumberunchanged.HTTP layer (
server.js)handleCalculatealready passesopthrough tocalculate;division by zerois mapped to HTTP 400 via existingcatch (err) → sendJson(400, { error: err.message }).200 {"result": <number>}and AC3 returns exactlydivision by zeroas the error string.UI (
public/index.html)÷withdata-key="÷"consistent with other operators.÷topendingOp = 'divide'alongside+/−/×.=, continuefetch('/api/calculate?...')withopfrompendingOp.body.error === 'division by zero', setdisplay.textContent = 'Error'(not the raw API message). Other API errors may continue to showbody.erroras today.Error, ensureCcalls existingreset()so the display returns to0and operand state is cleared (no code path should block clear after an error).9→÷→3→=→GET /api/calculate?a=9&b=3&op=divide→ display3.9→÷→0→=→ 400 → displayError→C→ display0.Keypad layout
×,−,+on the right column / bottom row. Place÷on an open or operator-adjacent cell without removingC, digits,., or=— e.g. share the operator column with×/−by adding÷on the row with7–9or4–6, following the same button styling.data-key="÷"is present in served HTML.Test layout
test/calculator.test.jsdivide, division by zero throw, regressions on other opstest/server.test.jstest/ui.test.jsErrorhandling on division by zeroFiles to change
lib/calculator.jsdividebranch with zero checkpublic/index.html÷button, operator mapping,Errordisplay for division by zerotest/calculator.test.jstest/server.test.jstest/ui.test.js÷toREQUIRED_KEYS; assert UI maps division-by-zero toErrorTest plan
Unit (
test/calculator.test.js)calculate('9', '3', 'divide')→3calculate('1', '4', 'divide')→0.25calculate('1', '0', 'divide')throws/division by zero/calculate('2', '3', 'add')→5calculate('6', '7', 'multiply')→42calculate('1', '2', 'pow')throws unknown operationout of rangeHTTP (
test/server.test.js)?a=9&b=3&op=divide→ 200,result: 3?a=1&b=4&op=divide→ 200,result: 0.25?a=1&b=0&op=divide→ 400,error: 'division by zero'; body must not containInfinityorNaN?a=2&b=3&op=add→ 200,result: 5op, missing / non-numeric operands withop=divideAll existing server tests (health, index, 404, prior op errors) must still pass.
UI (
test/ui.test.js)data-key="÷"divideop and showsErrorwhen API error isdivision by zero(e.g. match on'division by zero'and display'Error')REQUIRED_KEYSand/api/calculatefetch remainFull suite:
npm testmust pass before push.Risks
Infinity/NaNby dividing without a zero checky === 0before/; assert AC3 at module and HTTP layersdivision by zeroinstead ofError=handler; UI test assertsErrorstringCdoes not clear after error statereset()onC; manually verify flow; ensure error path does not leave staleleft/pendingOpif follow-up ops are attempted÷(U+00F7) vs/in testsdata-key="÷"; API usesop=divide3,0.25); mirror existing float behaviour÷Implementation order
lib/calculator.jsdivide branch + unit tests intest/calculator.test.jstest/server.test.jsfor AC1–3 and regressionspublic/index.htmldivide button, operator mapping,Errordisplay +test/ui.test.jsnpm test, then pushaira/4-divide-numbersAttachment: plan-4-r1.md
Aira · plan review
Plan pushed at
b0807bd1; independent Dodik plan review started.Aira · dodik-plan PASS on
b0807bd1The plan at
b0807bdcovers division end to end. AC1–AC3 require the exact 200 bodies for 9/3 and 1/4 and a 400 {"error":"division by zero"} that is not Infinity or NaN, at both module and HTTP level. AC4 requires a ÷ (U+00F7) button, the 9 ÷ 3 = flow showing 3, Error on division by zero, and C clearing via the existing reset(). The design matches calculate, handleCalculate, and the keypad operator handler, and limits edits to lib/calculator.js, public/index.html, and the three test files. Regression cases plus the current 27 passing tests keep existing operations and error handling in scope.Checks performed
Attachment: dodik-plan-b0807bd1.json
Aira · implementing
Plan approved by Dodik at
b0807bd1. Implementation started.Aira · ci
Ready for review at
2233dc3d(PR #12). Waiting for CI.Implemented divide in calculator, server contract, and UI (÷ key, Error on divide-by-zero); added calculator/server/ui tests for all acceptance criteria; npm test 34/34 pass; pushed aira/4-divide-numbers.
Aira · CI passed on
2233dc3dAira · review
CI is green on
2233dc3d. Independent Dodik and code review started.Aira · dodik PASS on
2233dc3dCommit
2233dc3implements op=divide end to end as specified in docs/plans/issue-4.md. GET /api/calculate returns 200 {"result":3} for 9/3, 200 {"result":0.25} for 1/4, and 400 {"error":"division by zero"} for 1/0 with no Infinity or NaN. The page has a ÷ button; 9 ÷ 3 = shows 3, division by zero shows Error, and C resets the display to 0. npm test passed 34/34 with no skipped tests, and the new divide tests fail on origin/main code. The diff only touches the plan, calculator, page, and tests; README does not list operations, so it did not need a change.Checks performed
2233dc3with origin/main and docs/plans/issue-4.mdAttachment: dodik-2233dc3d.json
Aira · reviewer PASS on
2233dc3dThe division feature matches issue #4 acceptance criteria: calculator logic rejects zero divisors before dividing and guards non-finite quotients, the existing API surfaces errors as 400 JSON without Infinity/NaN on division by zero, and the UI adds ÷ with divide op mapping plus Error display for that case while keeping C on reset(). Full
npm test(34/34) passed, and spot checks covered negatives, -0, and overflow-style division.Checks performed
2233dc3df3Attachment: reviewer-2233dc3d.json
Aira · qa
Dodik and the reviewer passed on
2233dc3d. Preview deployed at http://127.0.0.1:37353; QA started.Aira · qa PASS on
2233dc3dThe preview at http://127.0.0.1:37353 reports sha
2233dc3df3on /health. GET /api/calculate?a=9&b=3&op=divide returns 200 {"result":3} and a=1&b=4 returns 200 {"result":0.25}. Division by zero, including 0/0, negative zero, 0.0, and an underflowing divisor, returns 400 {"error":"division by zero"} and never Infinity or NaN. The served page has a ÷ button mapped to op=divide; a division-by-zero response sets the display to Error, and C resets it to 0. Add, subtract, multiply, and invalid-input errors still return the expected results. npm test passed 34 tests, including divide, division by zero (module, API, and UI text), and the page.Checks performed
Attachment: qa-2233dc3d.json
Aira · merging
QA passed on
2233dc3d. Merging.Aira · Merged PR #12
Merge commit
0b14307a4ace152b07bdd755e6cf6fb105f1f349from reviewed head2233dc3df39aa8d422a58acd27e820265d724f30.2233dc3implements op=divide end to end as specified in docs/plans/issue-4.md. GET /api/calculate returns 200 {"result":3} for 9/3, 200 {"result":0.25} f2233dc3df3on /health. GET /api/calculate?a=9&b=3&op=divide returns 200 {"result