-
-
Notifications
You must be signed in to change notification settings - Fork 276
London | 26-ITP-Jan | Carlos Abreu |Sprint 1 | Data Module Groups #1105
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
f9ba770
039ab90
514123c
521f05a
caa05c0
43da880
e96c90e
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(elements) { | ||
| // Using a Set to remove duplicates while preserving insertion order | ||
| // Then spread the Set back into an array | ||
| return [...new Set(elements)]; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,15 @@ | ||
|
|
||
| function findMax(elements) { | ||
| // Filter out non-numeric values | ||
| const numbers = elements.filter(element => typeof element === 'number'); | ||
|
|
||
|
Comment on lines
+3
to
+5
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 expect from the following function calls (on extreme cases)? |
||
| // If no numbers in array, return -Infinity | ||
| if (numbers.length === 0) { | ||
| return -Infinity; | ||
| } | ||
|
|
||
| // Find and return the maximum value | ||
| return Math.max(...numbers); | ||
| } | ||
|
|
||
| module.exports = findMax; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,62 @@ | ||
| /* Find the maximum element of an array of numbers | ||
|
|
||
| In this kata, you will need to implement a function that find the largest numerical element of an array. | ||
|
|
||
| E.g. max([30, 50, 10, 40]), target output: 50 | ||
| E.g. max(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-numerical elements) | ||
|
|
||
| You should implement this function in max.js, and add tests for it in this file. | ||
|
|
||
| We have set things up already so that this file can see your function from the other file. | ||
| */ | ||
|
|
||
| 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"); | ||
| 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("given an array with one number, returns that number", () => { | ||
| expect(findMax([42])).toBe(42); | ||
| expect(findMax([-10])).toBe(-10); | ||
| expect(findMax([0])).toBe(0); | ||
| }); | ||
|
|
||
| // Given an array with both positive and negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest number overall | ||
| test("given an array with both positive and negative numbers, returns the largest number overall", () => { | ||
| expect(findMax([30, 50, 10, 40])).toBe(50); | ||
| expect(findMax([-5, 10, -20, 30])).toBe(30); | ||
| expect(findMax([-100, -200, 0, 150])).toBe(150); | ||
| }); | ||
|
|
||
| // Given an array with just negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the closest one to zero | ||
| test("given an array with just negative numbers, returns the closest one to zero", () => { | ||
| expect(findMax([-5, -10, -3, -8])).toBe(-3); | ||
| expect(findMax([-1, -100, -50])).toBe(-1); | ||
| expect(findMax([-0.5, -0.8, -0.1])).toBe(-0.1); | ||
| }); | ||
|
|
||
| // Given an array with decimal numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest decimal number | ||
| test("given an array with decimal numbers, returns the largest decimal number", () => { | ||
| expect(findMax([1.5, 2.7, 1.2, 3.8])).toBe(3.8); | ||
| expect(findMax([-1.5, -2.7, -1.2, -0.5])).toBe(-0.5); | ||
| expect(findMax([0.1, 0.01, 0.001])).toBe(0.1); | ||
| }); | ||
|
|
||
| // 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("given an array with non-number values, returns the max and ignores non-numeric values", () => { | ||
| expect(findMax(['hey', 10, 'hi', 60, 10])).toBe(60); | ||
| expect(findMax([20, 'abc', 30, 'def', 40])).toBe(40); | ||
| expect(findMax(['x', 'y', 'z', 100, 50])).toBe(100); | ||
| }); | ||
|
Comment on lines
+49
to
+53
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, |
||
|
|
||
| // 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("given an array with only non-number values, returns -Infinity", () => { | ||
| expect(findMax(['hey', 'hi', 'hello'])).toBe(-Infinity); | ||
| expect(findMax([true, false, null])).toBe(-Infinity); | ||
| expect(findMax([{}, [], 'string'])).toBe(-Infinity); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,9 @@ | ||
| function sum(elements) { | ||
| // Filter out non-numeric values | ||
| const numbers = elements.filter(element => typeof element === 'number'); | ||
|
|
||
| // Sum all the numbers using reduce | ||
| return numbers.reduce((total, current) => total + current, 0); | ||
| } | ||
|
|
||
| module.exports = sum; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,59 @@ | ||
| /* Sum the numbers in an array | ||
|
|
||
| In this kata, you will need to implement a function that sums the numerical elements of an array | ||
|
|
||
| E.g. sum([10, 20, 30]), target output: 60 | ||
| E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical elements) | ||
| */ | ||
|
|
||
| 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") | ||
| 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("given an array with just one number, returns that number", () => { | ||
| expect(sum([42])).toBe(42); | ||
| expect(sum([-10])).toBe(-10); | ||
| expect(sum([0])).toBe(0); | ||
| expect(sum([3.14])).toBe(3.14); | ||
| }); | ||
|
|
||
| // Given an array containing negative numbers | ||
| // When passed to the sum function | ||
| // Then it should still return the correct total sum | ||
| test("given an array containing negative numbers, returns the correct total sum", () => { | ||
| expect(sum([10, -5, 20, -3])).toBe(22); | ||
| expect(sum([-10, -20, -30])).toBe(-60); | ||
| expect(sum([5, -5, 10, -10])).toBe(0); | ||
| expect(sum([-1, -2, -3, -4])).toBe(-10); | ||
| }); | ||
|
|
||
| // Given an array with decimal/float numbers | ||
| // When passed to the sum function | ||
| // Then it should return the correct total sum | ||
| test("given an array with decimal/float numbers, returns the correct total sum", () => { | ||
| expect(sum([1.5, 2.5, 3.0])).toBe(7); | ||
| expect(sum([0.1, 0.2, 0.3])).toBeCloseTo(0.6); | ||
| expect(sum([1.99, 2.99, 3.99])).toBeCloseTo(8.97); | ||
| expect(sum([-1.5, 2.5, -1.0])).toBe(0); | ||
| }); | ||
|
|
||
| // 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("given an array containing non-number values, ignores non-numerical values and returns sum of numerical elements", () => { | ||
| expect(sum(['hey', 10, 'hi', 60, 10])).toBe(80); | ||
| expect(sum([20, 'abc', 30, 'def', 40])).toBe(90); | ||
| expect(sum(['x', 'y', 'z', 100, 50])).toBe(150); | ||
| expect(sum([true, 5, false, 10, null, 15])).toBe(30); | ||
| expect(sum([{}, [], 25, 'string', 35])).toBe(60); | ||
| }); | ||
|
|
||
| // 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("given an array with only non-number values, returns 0", () => { | ||
| expect(sum(['hey', 'hi', 'hello'])).toBe(0); | ||
| expect(sum([true, false, null])).toBe(0); | ||
| expect(sum([{}, [], 'string', undefined])).toBe(0); | ||
| expect(sum(['a', 'b', 'c'])).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.
Is it necessary to make a copy of
numbersbefore sorting?