Repository navigation
London |26-ITP-Sep| Maharit Belai| Sprint 1 | coursework sprint-1 #1483
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 47 commits
26b44e9
671b6c4
0eedf37
df36aae
b9818eb
bd50d80
0a0c4d5
3fdbba5
1ac1a8c
a8c8e5b
8b2277a
7a8e98b
20f698c
720fdaf
30cf7cd
f8736d9
ab03db5
3d22657
fd5644f
f15e553
f5e48dc
cd439b7
f2e4871
ea1f770
2d230bf
1f598f1
73325f6
8ef5244
aae9679
90f3ec4
7edbeee
3576733
e460491
251329c
8101cc2
3207a97
1da066e
be6ec8d
9ba29de
1cac38e
43647bb
49baa11
dab9188
12e8cb0
c4a0488
7165d34
31a874c
57a5cc3
ee47d72
b560e2c
58d46b5
02f2d48
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,12 @@ | ||
| function dedupe() {} | ||
| function dedupe(array) { | ||
| const withOutDup = []; | ||
| for (let i = 0; i < array.length; i++) { | ||
| const itemArray = array[i]; | ||
| if (!withOutDup.includes(itemArray)) { | ||
| withOutDup.push(itemArray); | ||
| } | ||
| } | ||
|
|
||
| return withOutDup; | ||
| } | ||
| module.exports = dedupe; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,13 +16,33 @@ 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.todo("given an empty array, it returns an empty array"); | ||
| test("when passed an emptyarray", () => { | ||
| const array = []; | ||
| dedupe(array); | ||
| expect(dedupe(array)).toEqual([]); | ||
| }); | ||
|
|
||
| // Given an array with no duplicates | ||
| // When passed to the dedupe function | ||
| // Then it should return a copy of the original array | ||
| test("when passed an array with no duplicate", () => { | ||
| const array = [1, 2, 3, 4]; | ||
| dedupe(array); | ||
| expect(dedupe(array)).toEqual([1, 2, 3, 4]); | ||
| }); | ||
|
|
||
| // 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("when passed an array of duplicate numbers", () => { | ||
| expect(dedupe([1, 1, 2])).toEqual([1, 2]); | ||
| expect(dedupe([1, 1, 2, 2, 2, 3])).toEqual([1, 2, 3]); | ||
| expect(dedupe([1, 1, 1, 3, 3, 4, 4])).toEqual([1, 3, 4]); | ||
| }); | ||
|
|
||
| test("when passed an array of duplicate characters", () => { | ||
| expect(dedupe(["a", "a", "b", "b", "b", "c"])).toEqual(["a", "b", "c"]); | ||
| expect(dedupe(["c", "c", "e", "e", "z", "z"])).toEqual(["c", "e", "z"]); | ||
| }); | ||
|
Comment on lines
+39
to
+48
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. All these test cases are testing consecutive duplications. How about |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,12 +21,33 @@ const describeMedian = require("./describe-median.js"); | |
| // 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("returns the correct median when given [1,2,3]", () => { | ||
| expect(describeMedian([1, 2, 3])).toEqual(2); | ||
| }); | ||
|
|
||
| // Given an empty array | ||
| // When passed to describeMedian | ||
| // Then it should return "Could not calculate a median: calculateMedian requires a non-empty array" | ||
|
|
||
| test("returns an error message when given an empty array", () => { | ||
| expect(describeMedian([])).toEqual( | ||
| "Could not calculate a median:calculateMedian requires a non-empty array" | ||
| ); | ||
| }); | ||
| // 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("returns an error message when given a string of array", () => { | ||
| expect(describeMedian(["banana"])).toEqual( | ||
| "Could not calculate a median:calculateMedian requires an array of numbers" | ||
| ); | ||
| }); | ||
| test("return an error message when given a number", () => { | ||
| expect(describeMedian(123)).toEqual( | ||
| "Could not calculate a median:calculateMedian requires an array of numbers" | ||
| ); | ||
| }); | ||
| test("return an error message when given list of strings of array", () => { | ||
| expect(describeMedian(["one", "two", "three"])).toEqual( | ||
| "Could not calculate a median:calculateMedian requires an array of numbers" | ||
| ); | ||
| }); | ||
|
Comment on lines
+39
to
+53
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. Well done. Good test cases coverage. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| function findMax(elements) { | ||
| if (!Array.isArray(elements)) { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
| for (let num of elements) { | ||
| if (typeof num !== "number") { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
| } | ||
| return Math.max(...elements); | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,20 @@ | ||
| 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"); | ||
| } | ||
| } | ||
| let total = 0; | ||
| for (const num of list) { | ||
| total += num; | ||
| } | ||
| return (mean = total / list.length); | ||
| } | ||
|
|
||
| module.exports = calculateMean; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,17 @@ | ||
| function sum(elements) { | ||
| if (!Array.isArray(elements)) { | ||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
| for (const num of elements) { | ||
| if (typeof num !== "number") { | ||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
| } | ||
| let total = 0; | ||
| for (const num of elements) { | ||
| total += num; | ||
| } | ||
| 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.
The variable name
itemArraymay be a bit confusing. Maybe considerarrayItemor even better justitemin the future.