Repository navigation
London | 26-ITP-Sep | Russom Gebremeskel | Sprint 1 | Coursework #1506
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
5ce7d15
8f51f4c
d68c02b
f65276c
6745e82
20d911a
6f7f6d4
a922943
2491759
697d23f
33ffd60
b471137
434ca4b
d997797
734381d
215a119
c12fb12
31ba3f1
eb978b1
9a8945a
68b854a
1f53630
d0b0c30
c0cc6b7
5da7996
b772767
1374324
2b9d397
1c95b32
2f02e30
1735942
351f97b
7bb3a8f
15e8f9f
6c1939d
db24d03
e9e6c74
ca8caef
2f663ed
a42743b
abd6b35
189c032
0160ee1
a273717
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,16 @@ | ||
| function dedupe() {} | ||
| function dedupe(list) { | ||
| const elements = []; | ||
|
|
||
| if (list.length === 0) { | ||
| return []; | ||
| } | ||
|
|
||
| for (const item of list) { | ||
| if (!elements.includes(item)) { | ||
| elements.push(item); | ||
| } | ||
| } | ||
| return elements; | ||
| } | ||
|
|
||
| module.exports = dedupe; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,13 +16,27 @@ E.g. dedupe([1, 2, 1]) returns [1, 2] | |
| // Given an empty array | ||
| // When passed to the dedupe function | ||
| // Then it should return an empty array | ||
| test.todo("given an empty array, it returns an empty array"); | ||
| test("given an empty array, it returns an empty array", () => { | ||
| const list = []; | ||
| const result = dedupe(list); | ||
| expect(dedupe(list)).toEqual(result); | ||
|
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. What output does this test require for an empty list? To "like" or "dislike" this comment, please follow this link |
||
| }); | ||
|
|
||
| // Given an array with no duplicates | ||
| // When passed to the dedupe function | ||
| // Then it should return a copy of the original array | ||
| test("given an array with no duplicates, it returns a copy of the original array", () => { | ||
| const list = ["a", "b", "c"]; | ||
| const result = dedupe(list); | ||
| expect(dedupe(list)).toEqual(result); | ||
| expect(result).not.toBe(list); | ||
| }); | ||
|
cjyuan marked this conversation as resolved.
Resolved
|
||
|
|
||
| // Given an array of strings or numbers | ||
| // When passed to the dedupe function | ||
| // Then it should return a new array with duplicates removed while preserving the | ||
| // Then it should return a new array with duplicates removed while preserving the | ||
| // first occurrence of each element from the original array. | ||
| test("given an array, it returns an array with duplicates removed and its original occurrence", () => { | ||
| expect(dedupe([1, 3, 1, 2, 3])).toEqual([1, 3, 2]); | ||
| expect(dedupe(["a", "b", "a", "c", "b"])).toEqual(["a", "b", "c"]); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,12 @@ function calculateMedian(list) { | |
| } | ||
|
|
||
| // Implement this function. See describe-median.test.js for the acceptance criteria. | ||
| function describeMedian(list) {} | ||
| function describeMedian(list) { | ||
| try { | ||
| return `The median is ${calculateMedian(list)}`; | ||
| } catch (error) { | ||
| return `Could not calculate a median: calculateMedian requires a non-empty array: ${error.message}`; | ||
|
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. What exact sentence does this return when To "like" or "dislike" this comment, please follow this link |
||
| } | ||
| } | ||
|
|
||
| module.exports = describeMedian; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,18 +15,33 @@ message, so the caller can see what went wrong. | |
|
|
||
| const describeMedian = require("./describe-median.js"); | ||
|
|
||
|
|
||
| // Acceptance criteria: | ||
|
|
||
| // 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"'); | ||
| test("given [1, 2, 3], returns The median is 2", () => { | ||
| const list = [1, 2, 3]; | ||
| const result = describeMedian(list); | ||
| expect(describeMedian(list)).toEqual(result); | ||
|
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. What incorrect sentence would either of these assertions catch if To "like" or "dislike" this comment, please follow this link |
||
| }); | ||
|
|
||
| // Given an empty array | ||
| // When passed to describeMedian | ||
| // Then it should return "Could not calculate a median: calculateMedian requires a non-empty array" | ||
| test("given an empty array [], returns Could not calculate a median", () => { | ||
| const list = []; | ||
| const result = describeMedian(list); | ||
| expect(describeMedian(list)).toEqual(result); | ||
| }); | ||
|
Comment on lines
+34
to
+38
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. The tests in this file could not yet correctly check whether the function could return the expected value.
Author
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. Updated the test with duplicated elements in the 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. Still have the same issue. AI also identified this bug. |
||
|
|
||
| // 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" | ||
| test("given a non-array , returns Could not calculate a median", () => { | ||
| const list = []; | ||
| const result = describeMedian(list); | ||
|
Comment on lines
+44
to
+45
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. Does this test pass a non-array value to To "like" or "dislike" this comment, please follow this link |
||
| expect(describeMedian(list)).toEqual(result); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,18 @@ | ||
| function findMax(elements) { | ||
| if (!Array.isArray(elements)) { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
|
|
||
| for (const element of elements) { | ||
| if (typeof element !== "number") { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
|
|
||
| if (!elements.length === 1) { | ||
|
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 a one-element array, what does To "like" or "dislike" this comment, please follow this link |
||
| return element; | ||
| } | ||
| } | ||
| return Math.max(...elements); | ||
| } | ||
|
|
||
| module.exports = findMax; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,18 @@ | ||
| 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"); | ||
| } | ||
|
|
||
| let total = 0; | ||
| for (const item of list) { | ||
| if (typeof item !== "number") { | ||
| throw new Error("calculateMean requires an array of numbers"); | ||
| } | ||
| total = total + item; | ||
| } | ||
| return Number(total.toFixed(2)) / 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. Why limit the mean to only two decimal places? 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. What happens to the mean of values with more than two decimal places when the total is rounded before dividing? To "like" or "dislike" this comment, please follow this link |
||
| } | ||
| module.exports = calculateMean; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,28 +23,75 @@ 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", () => { | ||
| const list = [1, 2, 6]; | ||
| const result = calculateMean(list); | ||
| expect(result).toEqual(3); | ||
| }); | ||
|
|
||
| // Given an array with a single number | ||
| // When passed to calculateMean | ||
| // Then it should return that number | ||
| test("given [6], returns 6", () => { | ||
| expect(6).toEqual(6); | ||
|
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. What function call is this assertion checking? To "like" or "dislike" this comment, please follow this link |
||
| }); | ||
|
|
||
| // Given an array containing negative or decimal numbers | ||
| // When passed to calculateMean | ||
| // Then it should return the correct mean | ||
| test("given negative numbers, return mean", () => { | ||
| const list = [-7, -3, -5]; | ||
| const result = calculateMean(list); | ||
| expect(result).toEqual(-5); | ||
| }); | ||
|
|
||
| test("given decimal numbers, return mean", () => { | ||
| const list = [7.2, 2.4, 6.8]; | ||
| const result = calculateMean(list); | ||
| expect(result).toBeCloseTo(16.4 / 3, 5); | ||
| }); | ||
|
|
||
| // Given an empty array | ||
| // When passed to calculateMean | ||
| // Then it should throw Error("calculateMean requires a non-empty array") | ||
| test("given an empty array, returns, calculateMean requires a non-empty array", () => { | ||
| expect(() => calculateMean([])).toThrow( | ||
| new Error("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") | ||
| test("given a non-array, returns, calculateMean requires an array of numbers", () => { | ||
| expect(() => calculateMean("banana")).toThrow( | ||
| new Error("calculateMean requires an array of numbers") | ||
| ); | ||
| expect(() => calculateMean(42)).toThrow( | ||
| new Error("calculateMean requires an array of numbers") | ||
| ); | ||
| expect(() => calculateMean(null)).toThrow( | ||
| new Error("calculateMean requires an array of numbers") | ||
| ); | ||
| expect(() => calculateMean({})).toThrow( | ||
| new Error("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, returns, calculateMean requires an array of numbers", () => { | ||
| expect(() => calculateMean()).toThrow( | ||
| new Error("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 non-number containing array, returns, calculateMean requires an array of numbers", () => { | ||
| expect(() => calculateMean([1, "2", 3])).toThrow( | ||
| new Error("calculateMean requires an array of numbers") | ||
| ); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,15 @@ | ||
| function sum(elements) { | ||
| if (!Array.isArray(elements)) { | ||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
| let total = 0; | ||
| for (const element of elements) { | ||
| if (typeof element !== "number") { | ||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
| total += element; | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| module.exports = sum; |
|
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. What should
Author
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. Test changed to separate expect() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,24 +13,53 @@ const sum = require("./sum.js"); | |
| // Given an empty array | ||
| // When passed to the sum function | ||
| // Then it should return 0 | ||
| test.todo("given an empty array, returns 0") | ||
| test("given an empty array, returns 0", () => { | ||
| expect(sum([])).toEqual(0); | ||
| }); | ||
|
|
||
| // Given an array with just one number | ||
| // When passed to the sum function | ||
| // Then it should return that number | ||
| test("given a one number array, returns the same number", () => { | ||
| expect(sum([9])).toEqual(9); | ||
| }); | ||
|
|
||
| // Given an array containing negative numbers | ||
| // When passed to the sum function | ||
| // Then it should still return the correct total sum | ||
| test("given a negative number array, returns the sum", () => { | ||
| expect(sum([-9, -3, -6])).toEqual(-18); | ||
| }); | ||
|
|
||
| // Given an array with decimal/float numbers | ||
| // When passed to the sum function | ||
| // Then it should return the correct total sum | ||
| test("given a decimal numbers array, returns the sum", () => { | ||
| expect(sum([1.9, 7.5, 4.91])).toBeCloseTo(1.9 + 7.5 + 4.91); | ||
| }); | ||
|
|
||
| // Given an array containing a value that isn't a number | ||
| // When passed to the sum function | ||
| // Then it should throw Error("sum requires an array of numbers") | ||
| test("given a non-numbers array, returns sum requires an array of numbers", () => { | ||
| expect(() => sum(["apple"])).toThrow( | ||
| new Error("sum requires an array of numbers") | ||
| ); | ||
| expect(() => sum([null])).toThrow( | ||
| new Error("sum requires an array of numbers") | ||
| ); | ||
| expect(() => sum([3])).toThrow(new Error("sum requires an array of numbers")); | ||
|
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. These assertions expect To "like" or "dislike" this comment, please follow this link |
||
| }); | ||
|
|
||
| // Given something that isn't an array at all, such as "hey", 42 or no argument | ||
| // When passed to the sum function | ||
| // Then it should throw Error("sum requires an array of numbers") | ||
| test("given a non-array, returns sum requires an array of numbers", () => { | ||
| expect(() => sum(["apple"])).toThrow( | ||
| new Error("sum requires an array of numbers") | ||
| ); | ||
| expect(() => sum([null])).toThrow( | ||
| new Error("sum requires an array of numbers") | ||
| ); | ||
| expect(() => sum([3])).toThrow(new Error("sum requires an array of numbers")); | ||
| }); | ||
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.
Note: The loop also works for empty array.