Skip to content

Docs test: add an average helper - #9

Closed
colinhacks wants to merge 6 commits into
mainfrom
docs-test-average
Closed

colinhacks wants to merge 6 commits into
mainfrom
docs-test-average

Conversation

@colinhacks

Copy link
Copy Markdown
Member

This PR is test activity for the Pullfrog documentation screenshots. It will be closed without merging once the screenshots are captured.

Adds average(numbers) to index.js and covers it in test.js. Refs #8.

Copilot AI balanced review requested due to automatic review settings October 5, 2026 22:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@pullfrog

pullfrog Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Fixed the failing test job. The PR's own assertion average([]) === 0 failed with NaN !== 0 because average divided 0 by an empty array's length. average now returns 0 early for an empty array. node test.js and npm run lint both pass locally. The fix is pushed as signed commit 641b27d on docs-test-average.

Pullfrog  | View workflow run | via Pullfrog | Using claude-opus-5-5 (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

average([]) returns NaN, not 0. That breaks the requirement in #8, and the new test on test.js:8 fails, so node test.js (and CI) exits non-zero.

Reviewed changes

This review covers the new average helper and its tests.

  • average(numbers) in index.js — sums the array with reduce, divides by length, and adds the function to module.exports.
  • Tests in test.js — check the mean of [1, 2, 3, 4] and the empty-array case.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using claude-opus-5-5 (free via Pullfrog for OSS) | 𝕏

Comment thread index.js
module.exports = { add, subtract };
function average(numbers) {
const total = numbers.reduce((sum, n) => sum + n, 0);
return total / numbers.length;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For an empty array this computes 0 / 0, which is NaN. #8 says an empty array should return 0. Running node test.js on this branch fails at test.js:8 with AssertionError: NaN !== 0.

Suggested change
return total / numbers.length;
if (numbers.length === 0) return 0;
return total / numbers.length;

@colinhacks

Copy link
Copy Markdown
Member Author

@pullfrog make average throw a TypeError when it gets something other than an array of numbers, and add tests for the new error cases.

@pullfrog

pullfrog Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

average now throws a TypeError when its input isn't an array of numbers. The change is pushed to this PR as a signed commit, 72176dd, and npm test passes.

  • index.js: average throws TypeError("average expects an array of numbers") in two cases: the input isn't an array, or any element's typeof isn't "number". Calling average([]) still returns 0.
  • test.js: new assert.throws(..., TypeError) checks cover no argument, null, a string, an array-like object ({ length: 2 }), and arrays containing a string or null.

Some inputs still get through, because typeof NaN === "number":

  • NaN and Infinity: these count as numbers, so average([1, NaN]) returns NaN instead of throwing. Using Number.isFinite for the element check would reject both.
  • Sparse arrays: average([1, , 3]) passes the check because every skips empty slots. It then returns 4/3, since it divides by the array length (3) rather than by the 2 actual numbers.

Pullfrog  | View workflow run | via Pullfrog | Using claude-opus-5-5 (free via Pullfrog for OSS) | 𝕏

@colinhacks

Copy link
Copy Markdown
Member Author

@pullfrog three more changes, please. Track them with your todo list as you go:

  1. Add a median(numbers) helper with the same input validation as average.
  2. Add tests for median, including even and odd lengths and an empty array.
  3. Document add, subtract, average and median in README.md with one example each.

@pullfrog

pullfrog Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

I added median, its tests and README docs for all four helpers to this PR in signed commit 67ef506. npm test and npm run lint pass.

  • median(numbers) in index.js: it uses the same TypeError check as average. It sorts a copy of the input, so the caller's array isn't changed. For an even length it returns the mean of the two middle values.
  • Tests in test.js: they cover an odd length, an even length and an empty array. They also check that the input isn't changed and repeat each bad-input case that average is tested against.
  • README.md: a new API section documents add, subtract, average and median, with one example each.

Decision for you: median([]) returns 0, the same as average([]). The request didn't specify empty arrays beyond testing them, so I copied average. If you'd rather it throw or return NaN, the change touches one line in index.js plus one test and one README sentence.

No todo-list tool was available in this run, so I tracked the three items by hand in the checklist above.

Pullfrog  | View workflow run | via Pullfrog | Using claude-opus-5-5 (free via Pullfrog for OSS) | 𝕏

@colinhacks

Copy link
Copy Markdown
Member Author

@pullfrog review

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ No critical issues — one validation gap and a nitpick worth a look.

Reviewed changes

Reviewed the full PR: two new array helpers, their tests, and new README API docs. node test.js passes locally.

  • average(numbers) — returns the arithmetic mean, 0 for an empty array, and throws TypeError for non-number-array input.
  • median(numbers) — returns the middle value (or mean of the two middle values) from a sorted copy without mutating the input, with the same empty/validation behavior as average.
  • Tests — cover happy paths, empty arrays, single-element input, non-mutation, and a range of invalid inputs for both helpers.
  • README — adds an ## API section that documents add, subtract, average, and median.

ℹ️ Nitpicks

  • The PR title and description only mention average, but the PR also adds median and documents all four helpers in README.md. Consider updating the description so it covers the full scope.
  • test.js:29 — the average([5]) assertion sits apart from the other average assertions (lines 7–14), after the median block and a blank line. Moving it up with the rest would keep each helper's tests together.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using claude-opus-5-5 (free via Pullfrog for OSS) | 𝕏

Comment thread index.js

module.exports = { add, subtract };
function average(numbers) {
if (!Array.isArray(numbers) || !numbers.every((n) => typeof n === "number")) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sparse arrays get past this check, because Array.prototype.every skips holes. average([1, , 3]) returns 1.333…: reduce also skips the hole, but the code still divides by length 3. median([1, , 3]) returns 3, because spreading turns the hole into undefined and sort moves it to the end. NaN also passes typeof n === "number", and then both helpers return NaN. The same check is copied at line 19, so whatever fix you choose belongs in both places, ideally in one shared validator.

Technical details
# Sparse arrays and NaN bypass input validation

## Affected sites
- index.js:10 — `average` validation uses `numbers.every(...)`, which skips holes; `typeof` accepts `NaN`
- index.js:19 — identical check in `median`

## Required outcome
- `average([1, , 3])` and `median([1, , 3])` throw `TypeError` (or otherwise don't silently return a wrong value)
- Decide whether `NaN` should be rejected; if so, both helpers throw `TypeError` for it
- Add matching `assert.throws` cases in test.js

## Suggested approach
- Extract one shared `assertNumberArray(numbers, name)` helper. Check every index with a plain `for` loop (or `Array.from(numbers).every(...)`) so holes come through as `undefined`, and use `Number.isFinite` / `!Number.isNaN` if `NaN` should be rejected.

@colinhacks colinhacks closed this Oct 5, 2026
@colinhacks
colinhacks deleted the docs-test-average branch October 5, 2026 22:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants