Repository navigation
London | 26-ITP-SEPT | Carol Nassuna | Sprint 1 | Module-Data-Groups #1489
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,5 @@ | ||
| function dedupe() {} | ||
| function dedupe(values) { | ||
| return [...new Set(values)]; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,13 +20,32 @@ const describeMedian = require("./describe-median.js"); | |
| // Given an array of numbers | ||
| // When passed to describeMedian | ||
| // Then it should return "The median is " followed by the median | ||
| // Delete this test.todo and replace it with a test. | ||
| test.todo('given [1, 2, 3], returns "The median is 2"'); | ||
| it("returns the median for [1, 2, 3]", () => { | ||
| expect(describeMedian([1, 2, 3])).toEqual("The median is 2"); | ||
| }); | ||
|
|
||
| // Given an empty array | ||
| // When passed to describeMedian | ||
| // Then it should return "Could not calculate a median: calculateMedian requires a non-empty array" | ||
| it("returns an error description when given an empty array", () => { | ||
| try { | ||
| expect(describeMedian([])).toBe( | ||
| "Could not calculate a median: calculateMedian requires a non-empty array" | ||
| ); | ||
| } catch (error) { | ||
| throw new Error(`Empty-array case failed: ${error.message}`); | ||
| } | ||
| }); | ||
|
|
||
| // Given something that isn't an array of numbers, e.g. "banana" | ||
| // When passed to describeMedian | ||
| // Then it should return "Could not calculate a median: calculateMedian requires an array of numbers" | ||
| it("returns an error description for an array of strings", () => { | ||
| try { | ||
| expect(describeMedian(["banana", "apple", "orange"])).toBe( | ||
| "Could not calculate a median: calculateMedian requires an array of numbers" | ||
| ); | ||
| } catch (error) { | ||
| throw new Error(`String-array case failed: ${error.message}`); | ||
| } | ||
|
Comment on lines
+44
to
+50
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. The try-catch block here is not needed as |
||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,14 @@ | ||
| function findMax(elements) { | ||
| if ( | ||
| !Array.isArray(elements) || | ||
| elements.some((element) => typeof element !== "number") | ||
| ) { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
| return elements.reduce( | ||
| (max, element) => (element > max ? element : max), | ||
| -Infinity | ||
| ); | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,21 @@ | ||
| function calculateMean(list) {} | ||
| function calculateMean(list) { | ||
| if (!Array.isArray(list)) { | ||
| throw new Error("calculateMean requires an array of numbers"); | ||
| } | ||
|
|
||
| if (list.length === 0) { | ||
| throw new Error("calculateMean requires a non-empty array"); | ||
| } | ||
|
|
||
| for (const item of list) { | ||
| if (typeof item !== "number") { | ||
| throw new Error("calculateMean requires an array of numbers"); | ||
| } | ||
| } | ||
|
|
||
| const sum = list.reduce((a, b) => a + b, 0); | ||
|
|
||
| return sum / list.length; | ||
| } | ||
|
|
||
| module.exports = calculateMean; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,29 +22,56 @@ const calculateMean = require("./mean.js"); | |
| // Given an array of numbers | ||
| // When passed to calculateMean | ||
| // Then it should return their mean | ||
| // Delete this test.todo and replace it with a test. | ||
| test.todo("given [1, 2, 6], returns 3"); | ||
| it("returns the mean in a positive-number array", () => { | ||
| expect(calculateMean([1, 2, 6])).toBe(3); | ||
| }); | ||
|
|
||
| // Given an array with a single number | ||
| // When passed to calculateMean | ||
| // Then it should return that number | ||
| it("returns 10 for a single positive number", () => { | ||
| expect(calculateMean([10])).toBe(10); | ||
| }); | ||
|
|
||
| // Given an array containing negative or decimal numbers | ||
| // When passed to calculateMean | ||
| // Then it should return the correct mean | ||
| it("returns -3 for an array of negative numbers", () => { | ||
| expect(calculateMean([-1, -2, -6])).toEqual(-3); | ||
| }); | ||
|
|
||
| // Given an empty array | ||
| // When passed to calculateMean | ||
| // Then it should throw Error("calculateMean requires a non-empty array") | ||
| it("throws for an empty array", () => { | ||
| expect(() => calculateMean([])).toThrow( | ||
| "calculateMean requires a non-empty array" | ||
| ); | ||
| }); | ||
|
|
||
| // Given a value that isn't an array, e.g. "banana", 42, null or {} | ||
| // When passed to calculateMean | ||
| // Then it should throw Error("calculateMean requires an array of numbers") | ||
| it("throws when the input is not an array", () => { | ||
| expect(() => calculateMean("Hello")).toThrow( | ||
| "calculateMean requires an array of numbers" | ||
| ); | ||
| }); | ||
|
|
||
| // Given no argument at all | ||
| // When passed to calculateMean | ||
| // Then it should throw Error("calculateMean requires an array of numbers") | ||
| it("throws when the input has no argument", () => { | ||
| expect(() => calculateMean("")).toThrow( | ||
| "calculateMean requires an array of numbers" | ||
| ); | ||
| }); | ||
|
Comment on lines
61
to
+68
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. Empty string is still an argument, but this test case should be not passing in any argument. Please fix it, thank you. |
||
|
|
||
| // Given an array containing a non-number value, e.g. [1, "2", 3] | ||
| // When passed to calculateMean | ||
| // Then it should throw Error("calculateMean requires an array of numbers") | ||
| it("throws for an array containing a non-number value", () => { | ||
| expect(() => calculateMean(["2", 10, 20])).toThrow( | ||
| "calculateMean requires an array of numbers" | ||
| ); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| function sum(elements) { | ||
| if (!Array.isArray(elements)) { | ||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
|
|
||
| if (elements.some((element) => typeof element !== "number")) { | ||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
|
|
||
| return elements.reduce((total, element) => total + element, 0); | ||
| } | ||
|
|
||
| module.exports = sum; |
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.
The try-catch block here is not needed as
describeMedian()has already catched the error and no longer throws more error. If you need further clarification, please discuss with a volunteer on Saturday workshop. Thank you.