Repository navigation
London | 26-ITP-Sep | Sakiya Mayow | Sprint 1| coursework/data groups #1504
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
d3de7c2
15d231b
11887fc
c107da3
eae219d
0f5f682
92438ea
613fa40
0ad4f5e
1fb0ce9
8191a04
7a5ad81
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,7 @@ | ||
| function dedupe() {} | ||
| function dedupe(array) { | ||
| return array.filter((item, index) => { | ||
| return array.indexOf(item) === index; | ||
| }); | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,14 @@ | ||
| 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"); | ||
| } | ||
| } | ||
|
|
||
| return Math.max(...elements); | ||
| } | ||
|
|
||
| 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"); | ||
| } | ||
| for (const number of list) { | ||
| if (typeof number !== "number") { | ||
| throw new Error("calculateMean requires an array of numbers"); | ||
| } | ||
| } | ||
| let total = 0; | ||
| for (const number of list) { | ||
| total += number; | ||
| } | ||
|
|
||
| return total / 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,72 @@ 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", () => { | ||
| 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 an array with one number, returns that number", () => { | ||
| expect(calculateMean([6])).toBe(6); | ||
| }); | ||
|
|
||
| // Given an array containing negative or decimal numbers | ||
| // When passed to calculateMean | ||
| // Then it should return the correct mean | ||
|
|
||
| test("given negative and decimal numbers, returns the correct mean", () => { | ||
| expect(calculateMean([-2, 3.5, 4.5])).toBe(2); | ||
| }); | ||
|
|
||
|
Comment on lines
+43
to
+46
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); // falseSuggestion: 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, 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 something that isn't an array, throws an error", () => { | ||
| expect(() => calculateMean("banana")).toThrow( | ||
| "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" | ||
| ); | ||
| }); | ||
|
|
||
| // Given no argument at all | ||
| // When passed to calculateMean | ||
| // Then it should throw Error("calculateMean requires an array of numbers") | ||
|
|
||
| test("given no argument, 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 containing a non-number, throws 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,21 @@ | ||
| function sum(elements) { | ||
| if (!Array.isArray(elements)) { | ||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
|
|
||
| for (const element of elements) { | ||
| if (typeof element !== "number") { | ||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
| } | ||
|
|
||
| let total = 0; | ||
|
|
||
| for (const element of elements) { | ||
| total += element; | ||
| } | ||
|
|
||
| return total; | ||
| } | ||
|
|
||
| module.exports = sum; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,7 @@ | ||
| // Refactor the implementation of includes to use a for...of loop | ||
|
|
||
| function includes(list, target) { | ||
| for (let index = 0; index < list.length; index++) { | ||
| const element = list[index]; | ||
| if (element === target) { | ||
| return true; | ||
| } | ||
| } | ||
|
Comment on lines
1
to
-9
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: The objective of this exercise is to change the normal for-loop to for-of-loop (per the Instruction on line 1), as oppose to totally rewrite the whole function. |
||
| return false; | ||
| return list.includes(target); | ||
| } | ||
|
|
||
| module.exports = includes; | ||
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.
How should the function handle an array containing
NaN?