Repository navigation
London | 26-ITP-Sep | Hanna Bohlin | Sprint 1 | Sprint 1 Exercises #1486
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
d1362cd
ca7d11e
c5a3e4e
882c3a9
5fa46ec
88ea97f
474445f
73722de
5d20017
9843b6b
d2a28a9
07073aa
fe7960b
995d91e
74383ea
0a5abe5
9568c85
bae2a3b
1147d90
c090717
e7d3cbb
98b9810
76b199a
4408d15
f76781f
f1278f1
cce090b
0accc03
dec5bce
6caf465
aaf6237
4df5d9b
795191f
0b34659
1452e8f
2adeba3
a94cc17
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,13 @@ | ||
| function dedupe() {} | ||
| function dedupe(elements) { | ||
| const dedupedArr = []; | ||
|
|
||
| for (element of elements) { | ||
| if (!dedupedArr.includes(element)) { | ||
| dedupedArr.push(element); | ||
| } | ||
| } | ||
|
|
||
| return dedupedArr; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,13 +16,21 @@ 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"); | ||
| it("given an empty array, returns empty array", () => { | ||
| expect(dedupe([])).toEqual([]); | ||
| }); | ||
|
|
||
| // Given an array with no duplicates | ||
| // When passed to the dedupe function | ||
| // Then it should return a copy of the original array | ||
| it("given array without duplicates, returns that array", () => { | ||
| expect(dedupe([4, 6, 9, 23])).toEqual([4, 6, 9, 23]); | ||
| }); | ||
|
|
||
| // 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. | ||
| it("given array with duplicates, returns array without duplicates", () => { | ||
| expect(dedupe([4, 6, 9, 23, 9, 23, 23])).toEqual([4, 6, 9, 23]); | ||
| }); | ||
|
Comment on lines
+34
to
+36
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. Only numbers has been tested here. How about array of strings, or array of mixed numbers and strings? Please add those tests as well. Thank you. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,12 +21,30 @@ 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"'); | ||
| it("Given array of positive numbers, it returns the correct message", () => { | ||
| expect(describeMedian([1, 2, 6])).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("Given an empty array, it tells you it couldn't calculate the median and why", () => { | ||
| 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" | ||
| it("Given something that isn't array of numbers, it tells you it couldn't calculate the median and why", () => { | ||
| expect(describeMedian("banana")).toEqual( | ||
| "Could not calculate a median: calculateMedian requires an array of numbers" | ||
| ); | ||
| expect(describeMedian([3, "5", 8])).toEqual( | ||
| "Could not calculate a median: calculateMedian requires an array of numbers" | ||
| ); | ||
| expect(describeMedian(42)).toEqual( | ||
| "Could not calculate a median: calculateMedian requires an array of numbers" | ||
| ); | ||
|
Comment on lines
+40
to
+49
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,24 @@ | ||
| function findMax(elements) { | ||
| if (!Array.isArray(elements)) { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
|
|
||
| if (elements.length === 0) { | ||
| return -Infinity; | ||
| } | ||
|
|
||
| let max = elements[0]; | ||
|
|
||
| for (element of elements) { | ||
| if (typeof element != "number") { | ||
| 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,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"); | ||
| } | ||
|
|
||
| let sum = 0; | ||
|
|
||
| for (item of list) { | ||
| if (typeof item != "number") { | ||
| throw new Error("calculateMean requires an array of numbers"); | ||
| } | ||
| sum += item; | ||
| } | ||
|
|
||
| return sum / list.length; | ||
| } | ||
|
|
||
| module.exports = calculateMean; |
| 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 theSum = 0; | ||
|
|
||
| for (element of elements) { | ||
| if (typeof element != "number") { | ||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
| theSum += element; | ||
| } | ||
|
|
||
| return theSum; | ||
| } | ||
|
|
||
| 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.
In the future, you may consider combining these three conditions into one by checking
if (!Array.isArray(list)).