Repository navigation
Manchester |26-ITP-Sep | Precious Moses | Sprint 1 | Data Groups #1479
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 55 commits
09fe065
1fde0e0
ecfa4aa
fd8ec66
e3e6ea5
e95631c
bba68b6
66a3e6a
ed01080
b564d44
f104afb
b6f529d
c7b0e6e
872a29b
6db3742
82f5c3a
7aaff3c
02c2e87
44bf4cf
0a3d100
5506d62
1507d42
cc8664c
c229437
6b578bf
4b21f36
6fc8984
fb39a6b
4ee500d
6d78cdb
7130597
b3bd915
aafec32
70f312c
6f4c2d4
f96c928
9b1afc7
84122b3
7eeab41
bece9ad
c0a278a
0bb1418
55521a9
cd600e2
0ab70f6
3cd3611
783326c
b9f0e90
74b3e77
c08e109
435bc33
068ad9d
3f5b5d3
d1b8c93
de542e0
2061fba
0afbaed
ca719e8
5d3b691
9710b41
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(originalArray) { | ||
| const uniqueItems = []; | ||
|
|
||
| for (let i = 0; i < originalArray.length; i++) { | ||
| const currentItem = originalArray[i]; | ||
| const alreadyInArray = uniqueItems.includes(currentItem); | ||
|
|
||
| if (alreadyInArray === false) { | ||
| uniqueItems.push(currentItem); | ||
| } | ||
| } | ||
|
|
||
| return uniqueItems; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,20 @@ | ||
| function findMax(elements) { | ||
| if(!Array.isArray(elements)) { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
|
|
||
| let max = -Infinity; | ||
|
|
||
| for (const element of elements) { | ||
| if (typeof element !== "number" || Number.isNaN(element)) { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
|
|
||
| if (element > max) { | ||
| max = element; | ||
| } | ||
| } | ||
| return max; | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,22 @@ | ||
| 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 (let i = 0; i < list.length; i++) { | ||
| if (typeof list[i] !== "number") { | ||
| throw new Error("calculateMean requires an array of numbers"); | ||
| } | ||
| total += list[i]; | ||
| } | ||
|
|
||
| return total / list.length; | ||
| } | ||
|
|
||
| module.exports = calculateMean; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,29 +22,65 @@ 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"); | ||
| test("given [1, 2, 6], returns 3", () => { | ||
| expect(calculateMean([1, 2, 6])).toBe(3); | ||
| }); | ||
|
|
||
| // Given an array with a single number | ||
| // When passed to calculateMean | ||
| // Then it should return that number | ||
| test("given [7], return 7", () => { | ||
| expect(calculateMean([7])).toBe(7); | ||
| }); | ||
|
|
||
| // Given an array containing negative or decimal numbers | ||
| // When passed to calculateMean | ||
| // Then it should return the correct mean | ||
| test("given an array of negative numbers, return the correct mean", () => { | ||
| expect(calculateMean([-1, -3, -5])).toBe(-3); | ||
| }); | ||
|
|
||
| test("given an array of decimal numbers, return the correct mean", () => { | ||
| expect(calculateMean([1.5, 2.5])).toBe(2); | ||
| expect(calculateMean([0.1, 0.2])).toBeCloseTo(0.15); | ||
| }); | ||
|
|
||
| // Given an empty array | ||
| // When passed to calculateMean | ||
| // Then it should throw Error("calculateMean requires a non-empty array") | ||
| test("given an empty array, throws an error", () => { | ||
| 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") | ||
| test("given a value that is not an array, throws an error", () => { | ||
| expect(() => calculateMean("banana")).toThrow("calculateMean requires an array of numbers"); | ||
| }); | ||
|
|
||
| test("given a value that is not an array, throws an error", () => { | ||
| expect(() => calculateMean(42)).toThrow("calculateMean requires an array of numbers"); | ||
| }); | ||
|
|
||
| test("given a value that is not an array, throws an error", () => { | ||
| expect(() => calculateMean(null)).toThrow("calculateMean requires an array of numbers"); | ||
| }); | ||
|
|
||
| test("given a value that is not an array, throws an error", () => { | ||
| expect(() => calculateMean({})).toThrow("calculateMean requires an array of numbers"); | ||
| }); | ||
|
Comment on lines
+60
to
+73
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 consider grouping these test cases into one category; they have the same test description.
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. Code refactored. |
||
|
|
||
| // 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, throws an error", () => { | ||
| 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 with a non-number, throws an error", () => { | ||
| expect(() => calculateMean([7, "2", 5])).toThrow("calculateMean requires an array of numbers"); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,18 @@ | ||
| 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") { | ||
|
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. Note:
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. I have refactored the code. |
||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
| total += element; | ||
| } | ||
|
|
||
| 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.
Spacing a bit inconsistent after
if.