-
-
Notifications
You must be signed in to change notification settings - Fork 320
London | 26-ITP-May | Vito Moratti | Sprint 1 | Module data groups/sprint1 #1297
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
cae4a14
f9211a3
183be23
8e59e19
f9080a3
72267a0
4f2f012
f48b3b9
4fe95d9
120a279
a02f092
a22313c
dc82a68
4522a37
c22b7f9
8f921db
0bfabe2
23d816f
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) { | ||
| let newArr = []; | ||
| for (let i = 0; i < arr.length; i++) { | ||
| if (!newArr.includes(arr[i])) { | ||
| newArr.push(arr[i]); | ||
| } | ||
| } | ||
| return newArr; | ||
| } | ||
|
|
||
| console.log(dedupe([1,2,2,2,4])) | ||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,12 @@ | ||
| function findMax(elements) { | ||
| if (elements.length < 1) { | ||
| return -Infinity; | ||
| } else { | ||
| const filteredList = elements.filter((value) => typeof value === "number"); | ||
| return Math.max( ...filteredList)} | ||
|
Comment on lines
+5
to
+6
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.
|
||
|
|
||
| } | ||
|
|
||
| module.exports = findMax; | ||
|
|
||
| //console.log(findMax([30, 50, 10, 40])); // 50 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,28 +16,59 @@ 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"); | ||
| test("given an empty array, returns -Infinity", () => { | ||
| expect(findMax([])).toEqual(-Infinity); | ||
| }); | ||
|
|
||
| // Given an array with one number | ||
| // When passed to the max function | ||
| // Then it should return that number | ||
| describe("findMax", () => { | ||
| [{ input: [3], expected: 3 }].forEach(({ input, expected }) => | ||
| it(`returns the only number in the array for [${input}]`, () => | ||
| expect(findMax(input)).toEqual(expected)) | ||
| ); | ||
| }); | ||
|
|
||
| // 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", () => { | ||
| expect(findMax([1, -2, 3, -4, 5])).toEqual(5); | ||
| }); | ||
|
|
||
| // 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 closest to zero", () => { | ||
| expect(findMax([-1, -2, -3, -4])).toEqual(-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 largest decimal number", () => { | ||
| expect(findMax([1.5, 1.6, 1.7, 1.8])).toEqual(1.8); | ||
| expect(findMax([-1.5, -1.6, -1.7, -1.8])).toEqual(-1.5); | ||
| }); | ||
|
|
||
| // 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([1, 2, "3", null, undefined, 4])).toEqual(4); | ||
| expect(findMax(["apple", 1, 2, 3, "banana", 4])).toEqual(4); | ||
| expect(findMax([1, "2", 3, "4", 5])).toEqual(5); | ||
| expect(findMax([1, "apple", 2, null, 3, undefined, 4])).toEqual(4); | ||
| expect(findMax([3, "apple", 1, null, 2, undefined, 4])).toEqual(4); | ||
| expect(findMax(["banana", 5, 3, "apple", 1, 4, 2])).toEqual(5); | ||
| }); | ||
|
|
||
| // 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(["apple", null, undefined])).toEqual(-Infinity); | ||
| expect(findMax([null, undefined])).toEqual(-Infinity); | ||
| expect(findMax(["apple", "banana"])).toEqual(-Infinity); | ||
| }); | ||
|
Comment on lines
+70
to
+74
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,10 @@ | ||
| function sum(elements) { | ||
| let innit = 0; | ||
|
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 does the name |
||
| const filteredList = elements.filter((value) => typeof value === "number"); | ||
|
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)? |
||
| for (let i = 0; i < filteredList.length; i++) { | ||
| innit += filteredList[i]; | ||
| } | ||
| return innit; | ||
| } | ||
|
Comment on lines
+7
to
8
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. |
||
|
|
||
| module.exports = sum; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,24 +13,44 @@ const sum = require("./sum.js"); | |
| // 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([])).toEqual(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([2])).toEqual(2); | ||
| }); | ||
|
|
||
| // 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([-1, -2, -3])).toEqual(-6); | ||
| }); | ||
|
|
||
| // 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.5])).toEqual(7.5); | ||
| expect(sum([-1.5, -2.5, -3.5])).toEqual(-7.5); | ||
| }); | ||
|
Comment on lines
+37
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("given an array containing non-number values, ignores the non-numerical values and returns the sum of the numerical elements", () => { | ||
| expect(sum([1, 2, "3", null, undefined, 4])).toEqual(7); | ||
| expect(sum(["apple", 1, 2, 3, "banana", 4])).toEqual(10); | ||
| expect(sum([1, "2", 3, "4", 5])).toEqual(9); | ||
| }); | ||
|
|
||
| // 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(["apple", "banana", null, undefined])).toEqual(0); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,8 @@ | ||
| // 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) { | ||
| for (let i of list) { | ||
| if (i === target) { | ||
|
Comment on lines
+4
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. The identifier |
||
| return true; | ||
| } | ||
| } | ||
|
|
||
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.
Your function implementation is correct. However, this test could be improved to better ensure
that any future changes continue to align with the expected behavior described on line 25:
This test should fail if the function returns the original array (instead of a copy of the original array).
The current test checks only if both the original array and the returned array contain identical elements.
In order to validate the returned array is a different array, we need an additional check.
Can you find out what this additional check is?