-
-
Notifications
You must be signed in to change notification settings - Fork 271
Manchester | 26-ITP-Jan | Khalidbih| Sprint 1| Data-Groups #1068
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
ef89cd9
de5a96d
41dc44b
8a8b878
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(arr) { | ||
| if (!Array.isArray(arr)) return []; | ||
| const result = []; | ||
| for (let i = 0; i < arr.length; i++) { | ||
| if (!result.includes(arr[i])) { | ||
| result.push(arr[i]); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,17 @@ | ||
| function findMax(elements) { | ||
| if (!Array.isArray(elements)) return null; | ||
|
|
||
| let maxValue = -Infinity; | ||
|
|
||
| for (let i = 0; i < elements.length; i++) { | ||
| if (typeof elements[i] === "number" && isFinite(elements[i])) { | ||
| if (elements[i] > maxValue) { | ||
| maxValue = elements[i]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return maxValue; | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,33 +11,52 @@ We have set things up already so that this file can see your function from the o | |
| */ | ||
|
|
||
| const findMax = require("./max.js"); | ||
|
|
||
| // Given an empty array | ||
| // When passed to the max function | ||
| // Then it should return -Infinity | ||
| // Delete this test.todo and replace it with a test. | ||
| test.todo("given an empty array, returns -Infinity"); | ||
|
|
||
| // Given an array with one number | ||
| // When passed to the max function | ||
| // Then it should return that number | ||
|
|
||
| // Given an array with both positive and negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest number overall | ||
|
|
||
| // Given an array with just negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the closest one to zero | ||
|
|
||
| // Given an array with decimal numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest decimal number | ||
|
|
||
| // Given an array with non-number values | ||
| // When passed to the max function | ||
| // Then it should return the max and ignore non-numeric values | ||
|
|
||
| // Given an array with only non-number values | ||
| // When passed to the max function | ||
| // Then it should return the least surprising value given how it behaves for all other inputs | ||
| describe("findMax", () => { | ||
| // Given an empty array | ||
| // When passed to the max function | ||
| // Then it should return -Infinity | ||
| // Delete this test.todo and replace it with a test. | ||
| test("given an empty array, returns -Infinity", () => { | ||
| expect(findMax([])).toBe(-Infinity); | ||
| }); | ||
|
|
||
| // Given an array with one number | ||
| // When passed to the max function | ||
| // Then it should return that number | ||
| test("array with one number returns that number", () => { | ||
| expect(findMax([42])).toBe(42); | ||
| }); | ||
|
|
||
| // Given an array with both positive and negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest number overall | ||
| test("array with positive and negative numbers returns the largest number", () => { | ||
| expect(findMax([-10, 5, 3, -2])).toBe(5); | ||
| }); | ||
|
|
||
| // Given an array with just negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the closest one to zero | ||
| test("array with only negative numbers returns the closest to zero", () => { | ||
| expect(findMax([-10, -3, -7])).toBe(-3); | ||
| }); | ||
| // Given an array with decimal numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest decimal number | ||
| test("array with decimal numbers returns the largest decimal", () => { | ||
| expect(findMax([1.5, 2.7, 2.6, 0.9])).toBe(2.7); | ||
| }); | ||
| // Given an array with non-number values | ||
| // When passed to the max function | ||
| // Then it should return the max and ignore non-numeric values | ||
| test("array with non-number values ignores them and returns max of numbers", () => { | ||
| expect(findMax([10, "a", 5, null, 7])).toBe(10); | ||
| }); | ||
|
|
||
| // Given an array with only non-number values | ||
| // When passed to the max function | ||
| // Then it should return the least surprising value given how it behaves for all other inputs | ||
| test("array with only non-number values returns -Infinity", () => { | ||
| expect(findMax(["a", null, "b"])).toBe(-Infinity); | ||
| }); | ||
|
Comment on lines
+52
to
+61
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 a string representing a valid numeric literal (for example, To test if the function can correctly ignore non-numeric values, |
||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,15 @@ | ||
| function sum(elements) { | ||
| if (!Array.isArray(elements)) return 0; | ||
|
|
||
| let total = 0; | ||
|
|
||
| for (let i = 0; i < elements.length; i++) { | ||
| if (typeof elements[i] === "number" && isFinite(elements[i])) { | ||
| total += elements[i]; | ||
| } | ||
| } | ||
|
|
||
| return total; | ||
| } | ||
|
|
||
| module.exports = sum; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,29 +8,47 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical | |
|
|
||
| const sum = require("./sum.js"); | ||
|
|
||
| // Acceptance Criteria: | ||
|
|
||
| // Given an empty array | ||
| // When passed to the sum function | ||
| // Then it should return 0 | ||
| test.todo("given an empty array, returns 0") | ||
|
|
||
| // Given an array with just one number | ||
| // When passed to the sum function | ||
| // Then it should return that number | ||
|
|
||
| // Given an array containing negative numbers | ||
| // When passed to the sum function | ||
| // Then it should still return the correct total sum | ||
|
|
||
| // Given an array with decimal/float numbers | ||
| // When passed to the sum function | ||
| // Then it should return the correct total sum | ||
|
|
||
| // Given an array containing non-number values | ||
| // When passed to the sum function | ||
| // Then it should ignore the non-numerical values and return the sum of the numerical elements | ||
|
|
||
| // Given an array with only non-number values | ||
| // When passed to the sum function | ||
| // Then it should return the least surprising value given how it behaves for all other inputs | ||
| describe("sum", () => { | ||
| // Acceptance Criteria: | ||
|
|
||
| // Given an empty array | ||
| // When passed to the sum function | ||
| // Then it should return 0 | ||
| test("given an empty array, returns 0", () => { | ||
| expect(sum([])).toBe(0); | ||
| }); | ||
|
|
||
| // Given an array with just one number | ||
| // When passed to the sum function | ||
| // Then it should return that number | ||
| test("array with one number returns that number", () => { | ||
| expect(sum([5])).toBe(5); | ||
| }); | ||
|
|
||
| // Given an array containing negative numbers | ||
| // When passed to the sum function | ||
| // Then it should still return the correct total sum | ||
| test("array with negative numbers returns correct sum", () => { | ||
| expect(sum([-2, -3, 5])).toBe(0); | ||
| }); | ||
|
|
||
| // Given an array with decimal/float numbers | ||
| // When passed to the sum function | ||
| // Then it should return the correct total sum | ||
| test("array with decimal numbers returns correct sum", () => { | ||
| expect(sum([1.5, 2.5, 1])).toBe(5); | ||
| }); | ||
|
Comment on lines
+38
to
+40
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 array containing non-number values | ||
| // When passed to the sum function | ||
| // Then it should ignore the non-numerical values and return the sum of the numerical elements | ||
| test("ignores non-number values and sums numbers only", () => { | ||
| expect(sum([10, "a", 5, null, 7])).toBe(22); | ||
| }); | ||
|
|
||
| // Given an array with only non-number values | ||
| // When passed to the sum function | ||
| // Then it should return the least surprising value given how it behaves for all other inputs | ||
| test("returns 0 when array has only non-number values", () => { | ||
| expect(sum(["a", null, "b"])).toBe(0); | ||
| }); | ||
| }); | ||
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.
Do you plan to consider also
-Infinity,Infinity, andNaNin the median calculation?Could explore using
Array.prototype.filter()to simplify the code on lines 11-16.