Repository navigation
London | 26-ITP-Sep | Mars Adesina | Sprint 1 | Data groups #1494
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
2071acd
19742a7
aca8e91
091e875
c4d3648
fc92cc2
a078653
e02094b
1875b30
f9a47c7
1516690
445b260
4337038
6fd0acf
8490f09
7b42dd0
9f17e6d
19d9c71
063182a
1b1468e
d90b2cf
7547c25
c87f412
f24628a
c574535
c427aa2
108c621
03ec939
3048952
e6bec3a
15f68c9
e4a79ce
51a5109
597c5f5
28a59d0
4e83c4c
c9f7754
75d26ab
9abe6cc
80d4434
9ea7f04
556c1a7
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 |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ node_modules | |
| **/.DS_Store | ||
| .idea | ||
| package-lock.json | ||
| /prep/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,18 @@ | ||
| function dedupe() {} | ||
| function dedupe(numbers) { | ||
| //function starts | ||
| const uniqueNumbers = []; | ||
|
|
||
| for (let i = 0; i < numbers.length; i++) { | ||
| // for loop starts | ||
| const currentValue = numbers[i]; | ||
|
|
||
| if (!uniqueNumbers.includes(currentValue)) { | ||
| // if statement starts | ||
| uniqueNumbers.push(currentValue); | ||
| } // if statement ends | ||
| } // for loop ends | ||
|
Comment on lines
+6
to
+13
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. When the code is properly indented, we could already easily match the paring |
||
|
|
||
| return uniqueNumbers; | ||
| } //function ends | ||
|
|
||
| module.exports = dedupe; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| // Don't change this function. | ||
| function calculateMedian(list) { | ||
|
|
||
|
|
||
| if (!Array.isArray(list)) { | ||
| throw new Error("calculateMedian requires an array of numbers"); | ||
| } | ||
|
|
@@ -20,6 +22,13 @@ function calculateMedian(list) { | |
| } | ||
|
|
||
| // Implement this function. See describe-median.test.js for the acceptance criteria. | ||
| function describeMedian(list) {} | ||
| function describeMedian(list) { // opens function | ||
| try { // opens try statement | ||
| const median = calculateMedian(list); | ||
| return `The median is ${median}`; | ||
| } catch (error) { // opens catch | ||
| return "Could not calculate a median: " + error.message; | ||
| } | ||
| } //closes function | ||
|
Comment on lines
+25
to
+32
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. These comments are also not needed when the code is properly indented. |
||
|
|
||
| module.exports = describeMedian; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,20 @@ | ||
| function findMax(elements) { | ||
| if ( | ||
| !Array.isArray(elements) || | ||
| !elements.every((element) => typeof element === "number") | ||
| ) { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
|
|
||
| let max = -Infinity; | ||
|
|
||
| for (let i = 0; i < elements.length; i++) { | ||
| if (elements[i] > max) { | ||
| max = elements[i]; | ||
| } | ||
| } | ||
|
|
||
| return max; | ||
| } | ||
|
|
||
| module.exports = findMax; |
|
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. How should the function handle an array containing |
| 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"); | ||
| } | ||
| if (!list.every((number) => typeof number === "number")) { | ||
| throw new Error("calculateMean requires an array of numbers"); | ||
| } | ||
| const sum = list.reduce((sum, number) => { | ||
| return sum + number; | ||
| }, 0); | ||
|
Comment on lines
+11
to
+13
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. There are two different |
||
|
|
||
| return sum / list.length; | ||
| } | ||
|
|
||
| module.exports = calculateMean; | ||
|
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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,28 +23,63 @@ 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 the array [1,2,6], should return the number 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 7", () => { | ||
| expect(calculateMean([7])).toEqual(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, should return -4", () => { | ||
| expect(calculateMean([-2, -4, -6])).toEqual(-4); | ||
| }); | ||
| test("given an array of decimal numbers, should return 2.5", () => { | ||
| expect(calculateMean([1.5, 2.5, 3.5])).toEqual(2.5); | ||
| }); | ||
|
Comment on lines
+43
to
+45
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 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 isn't an array, should throw an error", () => { | ||
| expect(() => calculateMean("banana")).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") | ||
| 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" | ||
| ); | ||
|
|
||
| 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 an error", () => { | ||
| expect(() => calculateMean([1, "2", 3])).toThrow( | ||
| "calculateMean requires an array of numbers" | ||
| ); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| function sum(elements) { | ||
| if (!Array.isArray(elements)) { | ||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
| if (!elements.every((element) => typeof element === "number")) { | ||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
| const sum = elements.reduce((sum, number) => { | ||
| return number + sum; | ||
| }, 0); | ||
|
Comment on lines
+7
to
+10
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. Indentation is off. |
||
| return sum; | ||
| } | ||
|
|
||
| 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.
Could also use
Array.prototype.toSorted().