Repository navigation
London | 26-ITP-Sep | Bartosz Kawiak | Sprint 1 | Data Groups #1478
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
a5d9c95
5e63389
8e2d553
552c695
0cdf000
98d4d64
8562710
df1f5cb
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,14 @@ | ||
| function dedupe() {} | ||
| function dedupe(arr) { | ||
| if (arr.length === 0) { | ||
| return arr; | ||
| } | ||
| const newArr = []; | ||
| for (const item of arr) { | ||
| if (!newArr.includes(item)) { | ||
| newArr.push(item); | ||
| } | ||
| } | ||
| return newArr; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,19 @@ | ||
| function findMax(elements) { | ||
| if (!Array.isArray(elements)) { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
| if (elements.includes(NaN)) { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
| if (elements.length === 0) { | ||
| return -Infinity; | ||
| } | ||
|
|
||
| if (elements.length === 1) { | ||
| return elements[0]; | ||
| } | ||
| const sortedList = elements.toSorted((a, b) => a - b); | ||
| return sortedList.at(-1); | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,28 +16,58 @@ const findMax = require("./max.js"); | |
| // When passed to the max function | ||
| // Then it should return -Infinity | ||
| // Delete this test.todo and replace it with a test. | ||
| test.todo("given an empty array, returns -Infinity"); | ||
|
|
||
| test("given an empty array, returns -Infinity", () => { | ||
| expect(findMax([])).toEqual(-Infinity); | ||
| }); | ||
| // Given an array with one number | ||
| // When passed to the max function | ||
| // Then it should return that number | ||
| test("given an array with one number, returns that number", () => { | ||
| expect(findMax([4])).toEqual(4); | ||
| }); | ||
|
|
||
| // Given an array with both positive and negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest number overall | ||
| test("given an array with both positive and negative numbers, should returns the largest number overall", () => { | ||
| expect(findMax([4, -2])).toEqual(4); | ||
| }); | ||
|
|
||
| // Given an array with just negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the closest one to zero | ||
| test("given an array with just negative numbers, should return the closest one to zero", () => { | ||
| expect(findMax([-2, -54])).toEqual(-2); | ||
| }); | ||
|
|
||
| // Given an array with decimal numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest decimal number | ||
| test("given an array with decimal numbers, should return the largest decimal number", () => { | ||
| expect(findMax([3.5, 10.8])).toEqual(10.8); | ||
| }); | ||
|
|
||
| // Given an array containing a value that isn't a number | ||
| // When passed to the max function | ||
| // Then it should throw Error("findMax requires an array of numbers") | ||
|
|
||
| test("throws an error when an array contains a non-number", () => { | ||
| expect(() => findMax([10, NaN, 22])).toThrow( | ||
| "findMax requires an array of numbers" | ||
| ); | ||
| }); | ||
|
Comment on lines
+53
to
+57
Contributor
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. Could your function throw an error with this function call?
Note:
|
||
| // Given something that isn't an array at all, such as "hey", 42 or no argument | ||
| // When passed to the max function | ||
| // Then it should throw Error("findMax requires an array of numbers") | ||
| test("throws an error when input is a string", () => { | ||
| expect(() => findMax("hello")).toThrow( | ||
| "findMax requires an array of numbers" | ||
| ); | ||
| }); | ||
|
|
||
| test("throws an error when input is a number", () => { | ||
| expect(() => findMax(42)).toThrow("findMax requires an array of numbers"); | ||
| }); | ||
|
|
||
| test("throws an error when no argument is provided", () => { | ||
| expect(() => findMax()).toThrow("findMax requires an array of numbers"); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,26 @@ | ||
| function calculateMean(list) {} | ||
| function calculateMean(list) { | ||
| if (!Array.isArray(list)) { | ||
| throw new Error("calculateMean requires an array of numbers"); | ||
| } | ||
| for (const item of list) { | ||
| if (typeof item !== "number") { | ||
| throw new Error("calculateMean requires an array of numbers"); | ||
| } | ||
| } | ||
| if (list.length === 0) { | ||
| throw new Error("calculateMean requires a non-empty array"); | ||
| } | ||
| if (list.length === 1) { | ||
| return list[0]; | ||
| } | ||
|
|
||
| let result = 0; | ||
| let totalItem = 0; | ||
| for (const item of list) { | ||
| totalItem += item; | ||
| result = totalItem / list.length; | ||
|
Contributor
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. Is it necessary to compute the average multiple times? |
||
| } | ||
| return result; | ||
| } | ||
|
|
||
| module.exports = calculateMean; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,28 +23,65 @@ const calculateMean = require("./mean.js"); | |
| // 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"); | ||
| test("given [1, 2, 6], returns 3", () => { | ||
| expect(calculateMean([1, 2, 6])).toEqual(3); | ||
| }); | ||
|
|
||
| // Given an array with a single number | ||
| // When passed to calculateMean | ||
| // Then it should return that number | ||
| test("given an array with a single number, should return that number", () => { | ||
| expect(calculateMean([54])).toEqual(54); | ||
| }); | ||
|
|
||
| // Given an array containing negative or decimal numbers | ||
| // When passed to calculateMean | ||
| // Then it should return the correct mean | ||
| test("given an array containing negative or decimal numbers, should return the correct mean", () => { | ||
| expect(calculateMean([-3, 3.6, -10, 2.2])).toEqual(-1.8); | ||
| }); | ||
|
Comment on lines
+40
to
+42
Contributor
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. Decimal numbers in most programming languages (including JS) are internally represented in "floating point number" format. Floating point arithmetic is not exact. For example, the result of So the following could happen expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.805 ); // This fail
expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.8049999999999997 ); // This pass
expect( 0.005 + 0.6 + 1.2 ).toEqual( 1.8049999999999997 ); // This fail
console.log(1.2 + 0.6 + 0.005 == 1.805); // false
console.log(1.2 + 0.6 + 0.005 == 0.005 + 0.6 + 1.2); // falseCan you find a more appropriate way to test a value (that involves decimal number calculations) for equality? Suggestion: Look up
|
||
|
|
||
| // Given an empty array | ||
| // When passed to calculateMean | ||
| // Then it should throw Error("calculateMean requires a non-empty array") | ||
| test("given an empty array,should throw Error(calculateMean requires a non-empty array)", () => { | ||
| expect(() => calculateMean([])).toThrow( | ||
| "calculateMean requires a non-empty array" | ||
| ); | ||
|
Contributor
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.
With a string parameter, Jest checks whether the thrown error's message contains the specified string (instead of checking whether the thrown error's message match the specified string exactly). Suggestion: Look up how to use Jest's |
||
| }); | ||
|
|
||
| // 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") | ||
| test("given a value that is not an array,should throw new Error(calculateMean requires an array of numbers)", () => { | ||
| expect(() => calculateMean("banana")).toThrow( | ||
| "calculateMean requires an array of numbers" | ||
| ); | ||
| expect(() => calculateMean(42)).toThrow( | ||
| "calculateMean requires an array of numbers" | ||
| ); | ||
| expect(() => calculateMean(null)).toThrow( | ||
| "calculateMean requires an array of numbers" | ||
| ); | ||
| expect(() => calculateMean({})).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") | ||
| test("given no argument at all, should throw Error(calculateMean requires an array of numbers)", () => { | ||
| expect(() => calculateMean()).toThrow( | ||
| "calculateMean requires an array of numbers" | ||
| ); | ||
| }); | ||
|
|
||
| // 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") | ||
| test("given an array containing a non-number value, should throw Error(calculateMean requires an array of numbers)", () => { | ||
| expect(() => calculateMean([1, 5, "13"])).toThrow( | ||
| "calculateMean requires an array of numbers" | ||
| ); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,21 @@ | ||
| function sum(elements) { | ||
| if (!Array.isArray(elements)) { | ||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
| if (elements.includes(NaN)) { | ||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
| if (elements.length === 0) { | ||
| return 0; | ||
| } | ||
| if (elements.length === 1) { | ||
| return elements[0]; | ||
| } | ||
| let total = 0; | ||
| for (const item of elements) { | ||
| total += item; | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| 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.
Since the error message is the same, could you find a way to combine the if-statements?