-
-
Notifications
You must be signed in to change notification settings - Fork 280
Sheffield | ITP-Jan-26 | Hayriye Saricicek | Sprint 1| Module-Data-Groups #1082
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
f0fccc0
03e23fd
7d641a3
2886968
8dc8e91
48b6e13
50c6698
585b0cb
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,6 @@ | ||
| function dedupe() {} | ||
| function dedupe(arr) { | ||
| if (!Array.isArray(arr)) return []; | ||
| return [...new Set(arr)]; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,26 @@ | ||
| function findMax(elements) { | ||
| //find largest number in an array | ||
| if (!Array.isArray(elements)) { | ||
| throw new Error("Input must be an array"); //checks input is an array if not throws error | ||
| } | ||
|
|
||
| let max = -Infinity; //starts at -Infinity so any number will be bigger | ||
| let foundValid = false; //starts at false and will become true if valid number found | ||
|
|
||
| for (const val of elements) { | ||
| if (!Number.isFinite(val)) continue; //skips non-numeric values | ||
|
|
||
| foundValid = true; //sets to true if a valid number is found | ||
| if (val > max) { | ||
| max = val; //updates max if current value is greater | ||
| } | ||
| } | ||
| if (!foundValid) { | ||
| //if no valid numbers found, returns"-Inifinity" this includes invalid data and empty arrays | ||
| return -Infinity; | ||
| } | ||
|
Comment on lines
+18
to
+21
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. If
Author
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. thank you for explaining |
||
|
|
||
| return max; //returns the largest number found | ||
| } | ||
|
|
||
| module.exports = findMax; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,28 +16,56 @@ const findMax = require("./max.js"); | |
| // 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"); | ||
| describe("findMax.js", () => { | ||
| test.each([ | ||
|
Comment on lines
+19
to
+20
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. Should you need to classify test data into different categories, you can write:
Author
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. thank you |
||
| { input: [], expected: -Infinity }, | ||
| // Given an array with one number | ||
| // When passed to the max function | ||
| // Then it should return that number | ||
|
|
||
| // Given an array with one number | ||
| // When passed to the max function | ||
| // Then it should return that number | ||
| { input: [1], expected: 1 }, | ||
|
|
||
| // 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 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 | ||
| { input: [1, 2, -3], expected: 2 }, | ||
|
|
||
| // Given an array with decimal numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest decimal number | ||
| // 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 non-number values | ||
| // When passed to the max function | ||
| // Then it should return the max and ignore non-numeric values | ||
| { input: [-6, -2, -3], expected: -2 }, | ||
|
|
||
| // 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 | ||
| // Given an array with decimal numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest decimal number | ||
|
|
||
| { input: [3.5, 2.9, 6.3], expected: 6.3 }, | ||
|
|
||
| // Given an array with non-number values | ||
| // When passed to the max function | ||
| // Then it should return the max and ignore non-numeric values | ||
|
|
||
| { input: ["a", 2, 3], expected: 3 }, | ||
|
|
||
| // 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 | ||
| // It will return -Infinity as it ignores all non-numeric values and returns -Infinity if no valid numbers found | ||
|
|
||
| { input: ["a", "!", "x"], expected: -Infinity }, | ||
|
|
||
| // Given an array contains a number string it will ignore the string and return the max number | ||
|
|
||
| { input: ["5", 2, 3], expected: 3 }, | ||
|
|
||
|
|
||
| // Given an array contains only number strings it will rerturn -Infinity | ||
|
|
||
| { input: ["5", "2", "3"], expected: -Infinity }, | ||
|
|
||
| ])("returns correct max for %o", ({ input, expected }) => { | ||
| expect(findMax(input)).toBe(expected); | ||
| }); | ||
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,21 @@ | ||
| function sum(elements) { | ||
| } | ||
| // adds numbers and ignores non-numbers unless there are no valid numbers then throws error | ||
|
|
||
| if (!Array.isArray(elements)) { | ||
| throw new Error("Input must be an array"); //checks input is an array if not throws error | ||
| } | ||
|
|
||
| if (elements.length === 0) return 0; // <-- empty array returns 0 | ||
|
|
||
| let total = 0; | ||
| let foundValid = false; //starts at false and will become true if valid number found | ||
|
|
||
| for (const val of elements) { | ||
| if (!Number.isFinite(val)) continue; | ||
| foundValid = true; // loops and checks if input valid and if valid changes foundValid to true | ||
| total += val; //adds number to sum | ||
| } | ||
|
|
||
| return foundValid ? total : "Invalid Input"; // if at least one valid value is found return sum if no valid number is found return Invalid Input | ||
|
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 do you think about designing the function to support this behavior?
Author
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. I don't understand what it is you want me to do. The idea of the function is to add numbers together and ignore everything else so I am not sure why you are asking me to do this.
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.
Both sides are two different ways to compute "sum of all the elements in However, if As a result, in my opinion, |
||
| } | ||
| module.exports = sum; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,30 +7,57 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical | |
| */ | ||
|
|
||
| const sum = require("./sum.js"); | ||
| describe("sum.js", () => { | ||
| test.each([ | ||
| // Acceptance Criteria: | ||
|
|
||
| // Acceptance Criteria: | ||
| // Given an empty array | ||
| // When passed to the sum function | ||
| // Then it should return 0 | ||
|
|
||
| // Given an empty array | ||
| // When passed to the sum function | ||
| // Then it should return 0 | ||
| test.todo("given an empty array, returns 0") | ||
| { input: [], expected: 0 }, | ||
|
|
||
| // Given an array with just one number | ||
| // When passed to the sum function | ||
| // Then it should return that number | ||
| // 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 | ||
| { input: [1], expected: 1 }, | ||
|
|
||
| // 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 negative numbers | ||
| // When passed to the sum function | ||
| // Then it should still 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 | ||
| { input: [-2, 3, 4, -6], expected: -1 }, | ||
| { input: [4, -2], expected: 2 }, | ||
|
|
||
| // 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 | ||
| // Given an array with decimal/float numbers | ||
| // When passed to the sum function | ||
| // Then it should return the correct total sum | ||
|
|
||
| { input: [2.3, 4.2, 3.5], expected: 10 }, | ||
| { input: [4, 1.5], expected: 5.5 }, | ||
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // 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 | ||
|
|
||
| { input: ["a", 2, 5, "b"], expected: 7 }, | ||
|
|
||
| // 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 | ||
|
|
||
| { input: ["a", "n", "p"], expected: "Invalid Input" }, | ||
| ])( | ||
| "sums all numbers from $input and expects $expected", | ||
| ({ input, expected }) => { | ||
| const result = sum(input); | ||
|
|
||
| if (typeof expected === "number") { | ||
| expect(Math.abs(result - expected)).toBeLessThan(1e-10); // for special cases of decimals to avoid precision issues | ||
|
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. You can also use
Author
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. thank you, that is useful to know
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: They are not entirely equivalent though.
The default value of the second parameter is 2 (surprisingly). So
|
||
| } else { | ||
| expect(result).toEqual(expected); // for non-numeric expected values, check for exact match | ||
| } | ||
| } | ||
| ); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.