Repository navigation
Docs test: add an average helper #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
14f5a0b
Add an average helper (index.js)
colinhacks 37b652d
Add an average helper (test.js)
colinhacks 641b27d
Return 0 from average for an empty array
pullfrog[bot] 72176dd
Throw a TypeError from average for non-number-array input
pullfrog[bot] 67ef506
Add a median helper, its tests, and README docs for all helpers
pullfrog[bot] c7b91de
Test average with a single element
colinhacks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,41 @@ | ||
| # test-scratch-repo | ||
|
|
||
| Scratch repo for testing Pullfrog triggers. | ||
|
|
||
| ## API | ||
|
|
||
| ```js | ||
| const { add, subtract, average, median } = require("./index"); | ||
| ``` | ||
|
|
||
| ### `add(a, b)` | ||
|
|
||
| Returns the sum of `a` and `b`. | ||
|
|
||
| ```js | ||
| add(1, 2); // 3 | ||
| ``` | ||
|
|
||
| ### `subtract(a, b)` | ||
|
|
||
| Returns `a` minus `b`. | ||
|
|
||
| ```js | ||
| subtract(5, 3); // 2 | ||
| ``` | ||
|
|
||
| ### `average(numbers)` | ||
|
|
||
| Returns the arithmetic mean of an array of numbers. Returns `0` for an empty array and throws a `TypeError` if `numbers` is not an array of numbers. | ||
|
|
||
| ```js | ||
| average([1, 2, 3, 4]); // 2.5 | ||
| ``` | ||
|
|
||
| ### `median(numbers)` | ||
|
|
||
| Returns the middle value of an array of numbers, or the mean of the two middle values when the length is even. The input array is not mutated. Returns `0` for an empty array and throws a `TypeError` if `numbers` is not an array of numbers. | ||
|
|
||
| ```js | ||
| median([4, 1, 3, 2]); // 2.5 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,4 +6,23 @@ function subtract(a, b) { | |||||||
| return a - b; | ||||||||
| } | ||||||||
|
|
||||||||
| module.exports = { add, subtract }; | ||||||||
| function average(numbers) { | ||||||||
| if (!Array.isArray(numbers) || !numbers.every((n) => typeof n === "number")) { | ||||||||
| throw new TypeError("average expects an array of numbers"); | ||||||||
| } | ||||||||
| if (numbers.length === 0) return 0; | ||||||||
| const total = numbers.reduce((sum, n) => sum + n, 0); | ||||||||
| return total / numbers.length; | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For an empty array this computes
Suggested change
|
||||||||
| } | ||||||||
|
|
||||||||
| function median(numbers) { | ||||||||
| if (!Array.isArray(numbers) || !numbers.every((n) => typeof n === "number")) { | ||||||||
| throw new TypeError("median expects an array of numbers"); | ||||||||
| } | ||||||||
| if (numbers.length === 0) return 0; | ||||||||
| const sorted = [...numbers].sort((a, b) => a - b); | ||||||||
| const mid = Math.floor(sorted.length / 2); | ||||||||
| return sorted.length % 2 === 0 ? (sorted[mid - 1] + sorted[mid]) / 2 : sorted[mid]; | ||||||||
| } | ||||||||
|
|
||||||||
| module.exports = { add, subtract, average, median }; | ||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,31 @@ | ||
| const { add, subtract } = require("./index"); | ||
| const { add, subtract, average, median } = require("./index"); | ||
|
|
||
| const assert = require("assert"); | ||
|
|
||
| assert.strictEqual(add(1, 2), 3); | ||
| assert.strictEqual(subtract(5, 3), 2); | ||
| assert.strictEqual(average([1, 2, 3, 4]), 2.5); | ||
| assert.strictEqual(average([]), 0); | ||
| assert.throws(() => average(), TypeError); | ||
| assert.throws(() => average(null), TypeError); | ||
| assert.throws(() => average("123"), TypeError); | ||
| assert.throws(() => average({ length: 2 }), TypeError); | ||
| assert.throws(() => average([1, "2", 3]), TypeError); | ||
| assert.throws(() => average([1, null]), TypeError); | ||
| assert.strictEqual(median([3, 1, 2]), 2); | ||
| assert.strictEqual(median([4, 1, 3, 2]), 2.5); | ||
| assert.strictEqual(median([10, 9, 100]), 10); | ||
| assert.strictEqual(median([]), 0); | ||
| const unsorted = [3, 1, 2]; | ||
| median(unsorted); | ||
| assert.deepStrictEqual(unsorted, [3, 1, 2]); | ||
| assert.throws(() => median(), TypeError); | ||
| assert.throws(() => median(null), TypeError); | ||
| assert.throws(() => median("123"), TypeError); | ||
| assert.throws(() => median({ length: 2 }), TypeError); | ||
| assert.throws(() => median([1, "2", 3]), TypeError); | ||
| assert.throws(() => median([1, null]), TypeError); | ||
|
|
||
| assert.strictEqual(average([5]), 5); | ||
|
|
||
| console.log("All tests passed!"); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.everyskips holes.average([1, , 3])returns1.333…:reducealso skips the hole, but the code still divides bylength3.median([1, , 3])returns3, because spreading turns the hole intoundefinedandsortmoves it to the end.NaNalso passestypeof n === "number", and then both helpers returnNaN. The same check is copied at line 19, so whatever fix you choose belongs in both places, ideally in one shared validator.Technical details