Percentage #5

Open
opened 2026-11-21 02:25:12 +00:00 by lab-admin · 9 comments
Owner

Add a percentage operation.

Requirements

  • API: op=percent returns a * b / 100 ("a percent of b").
  • UI: a % button that works like the other binary operators. Pressing 5, 0, %, 8, = shows 4.
  • Existing operations and error handling keep working.
  • Tests in the module, API and page.

Acceptance criteria

  1. /api/calculate?a=50&b=8&op=percent returns {"result":4}.
  2. /api/calculate?a=12.5&b=200&op=percent returns {"result":25}.
  3. Bad input still returns 400 with an error.
  4. The page has a working % button.
Add a percentage operation. ## Requirements - API: `op=percent` returns `a * b / 100` ("a percent of b"). - UI: a `%` button that works like the other binary operators. Pressing `5`, `0`, `%`, `8`, `=` shows `4`. - Existing operations and error handling keep working. - Tests in the module, API and page. ## Acceptance criteria 1. `/api/calculate?a=50&b=8&op=percent` returns `{"result":4}`. 2. `/api/calculate?a=12.5&b=200&op=percent` returns `{"result":25}`. 3. Bad input still returns 400 with an `error`. 4. The page has a working `%` button.
Collaborator

Aira · queued

Aira picked up this task. Branch aira/5-percentage.

**Aira · queued** Aira picked up this task. Branch `aira/5-percentage`.
Collaborator

Aira · planning

Orchestrator thread started (composer-2.5). Planning on aira/5-percentage.

T3 thread: mcp:launch:03cc6f60b08fbef33dbf826fb7bbff1a071a850f17f77048bf44e0b8b4c9d4c4

**Aira · planning** Orchestrator thread started (composer-2.5). Planning on `aira/5-percentage`. T3 thread: `mcp:launch:03cc6f60b08fbef33dbf826fb7bbff1a071a850f17f77048bf44e0b8b4c9d4c4`
Collaborator

Aira · Plan (round 1) at c1b6f3c7

Wrote docs/plans/issue-5.md for op=percent (a*b/100), UI % button, and module/API/UI tests; committed and pushed aira/5-percentage.

docs/plans/issue-5.md

Issue #5: Percentage

Goal

Add a percentage operation end to end: a pure percent function in lib/calculator.js implementing a * b / 100 (“a percent of b”), GET /api/calculate support for op=percent, and a % button in the UI wired like the other binary operators. The flow 5, 0, %, 8, = must show 4 (50% of 8). Addition, subtraction, multiplication, division, and existing error handling remain unchanged.

Acceptance criteria

  1. GET /api/calculate?a=50&b=8&op=percent returns HTTP 200 with body {"result":4}.
  2. GET /api/calculate?a=12.5&b=200&op=percent returns HTTP 200 with body {"result":25}.
  3. Bad input (missing, empty, or non-numeric a/b, unknown op) still returns HTTP 400 with an error field in the body.
  4. The page at / has a working % button that selects op=percent via the same operator flow as +, −, ×, and ÷.

Design

Calculator module (lib/calculator.js)

  • Add percent(a, b) — returns a * b / 100 for finite numeric operands (caller supplies parsed numbers).
  • Extend calculate(op, a, b):
    • Include op === 'percent' in the allowed-operation check (alongside add, subtract, multiply, and divide).
    • Dispatch percent when op === 'percent'; keep existing branches for other operations.
    • Reuse existing parseNumber, operand validation, and the non-finite result guard after computation.
    • Any other op still returns { error: 'unknown operation' }.
  • Export percent from module.exports alongside existing exports.

HTTP layer (server.js)

  • No routing changes expected: /api/calculate already delegates to calculate and maps out.error to HTTP 400 with { error: out.error }.

UI (public/index.html)

  • Add a % key to the button grid in labels, consistent with other operators.
  • Extend onKey so % calls pressOperator('percent') (same waitingForSecond flow as other operators).
  • Reorder or extend labels so the 4-column grid still includes all digits, operators (+, −, ×, ÷, %), C, =, and . without removing existing keys.

Operand mapping for the UI flow (unchanged pattern): first number entered becomes a (the percentage value), second becomes b (the value to take that percent of). Example: 50, %, 8, = → API a=50&b=8&op=percent → 4.

Files to change

File Change
lib/calculator.js Add percent; extend calculate for op=percent; export percent.
server.js None (logic stays in module).
public/index.html % button; wire pressOperator('percent'); adjust labels layout.
test/calculator.test.js Unit tests for percent, calculate('percent', ...), and regression for other ops.
test/server.test.js HTTP tests for acceptance criteria 1–2 and bad-input regression (criterion 3).
test/ui.test.js % button present; script references percent; regression for existing buttons.

Test plan

Unit tests (test/calculator.test.js)

Test Proves
percent(50, 8) === 4 Criterion 1 (module level).
percent(12.5, 200) === 25 Criterion 2 (module level).
calculate('percent', '50', '8') → { result: 4 } Dispatch and parsing.
calculate('percent', '12.5', '200') → { result: 25 } Decimal percentage.
`calculate('add' 'subtract'
calculate('pow', ...) still unknown error Regression.
calculate('percent', 'x', '8') → { error: 'invalid a' } Bad input (module).
calculate('percent', '50', 'bad') → { error: 'invalid b' } Bad input (module).

HTTP tests (test/server.test.js)

Use existing withServer + fetch pattern:

Request Expect
?a=50&b=8&op=percent 200, { result: 4 }
?a=12.5&b=200&op=percent 200, { result: 25 }
?a=x&b=8&op=percent 400, { error: ... }
?a=50&b=bad&op=percent 400, { error: ... }
?a=2&b=3&op=add 200 regression
?a=2&b=5&op=subtract 200 regression
?a=6&b=7&op=multiply 200 regression
?a=9&b=3&op=divide 200 regression
?a=1&b=2&op=pow 400, error string

UI / page tests (test/ui.test.js)

Test Proves
HTML labels includes '%' Criterion 4 (button present).
Script uses pressOperator('percent') Criterion 4 (wiring).
HTML still includes required digits, +, −, ×, ÷, =, C, and fetch to /api/calculate Regression.

Run npm test locally before every push; all tests must pass.

Risks

Risk Mitigation
Floating-point rounding (50 * 8 / 100) Use acceptance values in tests; JSON serializes finite doubles correctly.
UI grid overflow with another operator Reorder labels to keep a sensible 4-column grid; UI test checks all prior buttons remain.
% label vs API op=percent API uses op=percent; UI label is display-only; tests assert HTML contains % and pressOperator('percent').
Operand order confusion (percent of vs value) Document in plan: a is percentage value, b is base; acceptance example 50 % of 8 → 4.
Breaking add/subtract/multiply/divide or validation Extend calculate only; run full existing test suite.

Implementation order (for later steps)

  1. lib/calculator.js + test/calculator.test.js
  2. HTTP tests in test/server.test.js (server unchanged)
  3. public/index.html UI + test/ui.test.js
  4. Full npm test, then push branch aira/5-percentage

Attachment: plan-5-r1.md

**Aira · Plan (round 1) at `c1b6f3c7`** Wrote docs/plans/issue-5.md for op=percent (a*b/100), UI % button, and module/API/UI tests; committed and pushed aira/5-percentage. <details><summary>docs/plans/issue-5.md</summary> # Issue #5: Percentage ## Goal Add a percentage operation end to end: a pure `percent` function in `lib/calculator.js` implementing `a * b / 100` (“a percent of b”), `GET /api/calculate` support for `op=percent`, and a `%` button in the UI wired like the other binary operators. The flow `5`, `0`, `%`, `8`, `=` must show `4` (50% of 8). Addition, subtraction, multiplication, division, and existing error handling remain unchanged. ## Acceptance criteria 1. `GET /api/calculate?a=50&b=8&op=percent` returns HTTP 200 with body `{"result":4}`. 2. `GET /api/calculate?a=12.5&b=200&op=percent` returns HTTP 200 with body `{"result":25}`. 3. Bad input (missing, empty, or non-numeric `a`/`b`, unknown `op`) still returns HTTP 400 with an `error` field in the body. 4. The page at `/` has a working `%` button that selects `op=percent` via the same operator flow as `+`, `−`, `×`, and `÷`. ## Design ### Calculator module (`lib/calculator.js`) - Add `percent(a, b)` — returns `a * b / 100` for finite numeric operands (caller supplies parsed numbers). - Extend `calculate(op, a, b)`: - Include `op === 'percent'` in the allowed-operation check (alongside `add`, `subtract`, `multiply`, and `divide`). - Dispatch `percent` when `op === 'percent'`; keep existing branches for other operations. - Reuse existing `parseNumber`, operand validation, and the non-finite result guard after computation. - Any other `op` still returns `{ error: 'unknown operation' }`. - Export `percent` from `module.exports` alongside existing exports. ### HTTP layer (`server.js`) - No routing changes expected: `/api/calculate` already delegates to `calculate` and maps `out.error` to HTTP 400 with `{ error: out.error }`. ### UI (`public/index.html`) - Add a `%` key to the button grid in `labels`, consistent with other operators. - Extend `onKey` so `%` calls `pressOperator('percent')` (same `waitingForSecond` flow as other operators). - Reorder or extend `labels` so the 4-column grid still includes all digits, operators (`+`, `−`, `×`, `÷`, `%`), `C`, `=`, and `.` without removing existing keys. Operand mapping for the UI flow (unchanged pattern): first number entered becomes `a` (the percentage value), second becomes `b` (the value to take that percent of). Example: `50`, `%`, `8`, `=` → API `a=50&b=8&op=percent` → `4`. ## Files to change | File | Change | |------|--------| | `lib/calculator.js` | Add `percent`; extend `calculate` for `op=percent`; export `percent`. | | `server.js` | None (logic stays in module). | | `public/index.html` | `%` button; wire `pressOperator('percent')`; adjust `labels` layout. | | `test/calculator.test.js` | Unit tests for `percent`, `calculate('percent', ...)`, and regression for other ops. | | `test/server.test.js` | HTTP tests for acceptance criteria 1–2 and bad-input regression (criterion 3). | | `test/ui.test.js` | `%` button present; script references `percent`; regression for existing buttons. | ## Test plan ### Unit tests (`test/calculator.test.js`) | Test | Proves | |------|--------| | `percent(50, 8) === 4` | Criterion 1 (module level). | | `percent(12.5, 200) === 25` | Criterion 2 (module level). | | `calculate('percent', '50', '8')` → `{ result: 4 }` | Dispatch and parsing. | | `calculate('percent', '12.5', '200')` → `{ result: 25 }` | Decimal percentage. | | `calculate('add'|'subtract'|'multiply'|'divide', ...)` still succeed | Regression. | | `calculate('pow', ...)` still unknown error | Regression. | | `calculate('percent', 'x', '8')` → `{ error: 'invalid a' }` | Bad input (module). | | `calculate('percent', '50', 'bad')` → `{ error: 'invalid b' }` | Bad input (module). | ### HTTP tests (`test/server.test.js`) Use existing `withServer` + `fetch` pattern: | Request | Expect | |---------|--------| | `?a=50&b=8&op=percent` | 200, `{ result: 4 }` | | `?a=12.5&b=200&op=percent` | 200, `{ result: 25 }` | | `?a=x&b=8&op=percent` | 400, `{ error: ... }` | | `?a=50&b=bad&op=percent` | 400, `{ error: ... }` | | `?a=2&b=3&op=add` | 200 regression | | `?a=2&b=5&op=subtract` | 200 regression | | `?a=6&b=7&op=multiply` | 200 regression | | `?a=9&b=3&op=divide` | 200 regression | | `?a=1&b=2&op=pow` | 400, `error` string | ### UI / page tests (`test/ui.test.js`) | Test | Proves | |------|--------| | HTML `labels` includes `'%'` | Criterion 4 (button present). | | Script uses `pressOperator('percent')` | Criterion 4 (wiring). | | HTML still includes required digits, `+`, `−`, `×`, `÷`, `=`, `C`, and `fetch` to `/api/calculate` | Regression. | Run `npm test` locally before every push; all tests must pass. ## Risks | Risk | Mitigation | |------|------------| | Floating-point rounding (`50 * 8 / 100`) | Use acceptance values in tests; JSON serializes finite doubles correctly. | | UI grid overflow with another operator | Reorder `labels` to keep a sensible 4-column grid; UI test checks all prior buttons remain. | | `%` label vs API `op=percent` | API uses `op=percent`; UI label is display-only; tests assert HTML contains `%` and `pressOperator('percent')`. | | Operand order confusion (percent of vs value) | Document in plan: `a` is percentage value, `b` is base; acceptance example `50 % of 8` → 4. | | Breaking add/subtract/multiply/divide or validation | Extend `calculate` only; run full existing test suite. | ## Implementation order (for later steps) 1. `lib/calculator.js` + `test/calculator.test.js` 2. HTTP tests in `test/server.test.js` (server unchanged) 3. `public/index.html` UI + `test/ui.test.js` 4. Full `npm test`, then push branch `aira/5-percentage` </details> Attachment: [plan-5-r1.md](https://aira-lab.cluster-dev.spkey.co/attachments/40df448b-a98d-46c6-8e15-39f1b5c16e71)
Collaborator

Aira · plan review

Plan pushed at c1b6f3c7; independent Dodik plan review started.

**Aira · plan review** Plan pushed at `c1b6f3c7`; independent Dodik plan review started.
Collaborator

Aira · dodik-plan PASS on c1b6f3c7

The issue #5 plan maps the percentage API, the % button flow (50% of 8 shows 4), and unchanged error handling onto four testable acceptance criteria. The design matches lib/calculator.js, server.js, and public/index.html, and the unit, HTTP, and page tests cover the success cases, bad input, and existing operations without extra scope.

Checks performed
  • Requirements map to acceptance criteria 1–4, including op=percent as a*b/100 and the 5,0,%,8,= flow showing 4
  • Design matches the calculate allow-list, parseNumber validation, non-finite guard, generic /api/calculate handler, and pressOperator/pressEquals operand order
  • Test plan proves criteria 1–2 at module and HTTP level, criterion 3 with invalid operands and unknown op plus the existing bad-input suite, and criterion 4 with a % label and pressOperator('percent')
  • Scope is limited to percent in the calculator, the page button, and tests in test/calculator.test.js, test/server.test.js, and test/ui.test.js
  • Acceptance values serialize as {"result":4} and {"result":25}; npm test is 48 passing on this commit

Attachment: dodik-plan-c1b6f3c7.json

**Aira · dodik-plan PASS on `c1b6f3c7`** The issue #5 plan maps the percentage API, the % button flow (50% of 8 shows 4), and unchanged error handling onto four testable acceptance criteria. The design matches lib/calculator.js, server.js, and public/index.html, and the unit, HTTP, and page tests cover the success cases, bad input, and existing operations without extra scope. <details><summary>Checks performed</summary> - Requirements map to acceptance criteria 1–4, including op=percent as a*b/100 and the 5,0,%,8,= flow showing 4 - Design matches the calculate allow-list, parseNumber validation, non-finite guard, generic /api/calculate handler, and pressOperator/pressEquals operand order - Test plan proves criteria 1–2 at module and HTTP level, criterion 3 with invalid operands and unknown op plus the existing bad-input suite, and criterion 4 with a % label and pressOperator('percent') - Scope is limited to percent in the calculator, the page button, and tests in test/calculator.test.js, test/server.test.js, and test/ui.test.js - Acceptance values serialize as {"result":4} and {"result":25}; npm test is 48 passing on this commit </details> Attachment: [dodik-plan-c1b6f3c7.json](https://aira-lab.cluster-dev.spkey.co/attachments/5b11959b-12ee-40f0-a894-2fd9d6dbc6e3)
Collaborator

Aira · implementing

Plan approved by Dodik at c1b6f3c7. Implementation started.

**Aira · implementing** Plan approved by Dodik at `c1b6f3c7`. Implementation started.
Collaborator

Aira · ci

Ready for review at 6939abf5 (PR #13). Waiting for CI.

Implemented percent in calculator.js (a*b/100), % button with pressOperator('percent'), and unit/server/UI tests; npm test 56/56 pass; pushed to aira/5-percentage.

**Aira · ci** Ready for review at `6939abf5` (PR #13). Waiting for CI. Implemented percent in calculator.js (a*b/100), % button with pressOperator('percent'), and unit/server/UI tests; npm test 56/56 pass; pushed to aira/5-percentage.
Collaborator

Aira · CI passed on 6939abf5

  • ci / test (pull_request): success
**Aira · CI passed on `6939abf5`** - ci / test (pull_request): success
Collaborator

Aira · review

CI is green on 6939abf5. Independent Dodik and code review started.

**Aira · review** CI is green on `6939abf5`. Independent Dodik and code review started.
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
lab/web-calculator-2#5
No description provided.