-
-
Notifications
You must be signed in to change notification settings - Fork 327
London | 26-ITP-May | Damilola Odumosu | Sprint 1 | Coursework #1296
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
3ac6be3
5fdea58
7bde1b5
64b9210
f1f59ac
b881892
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,9 @@ | ||
| function dedupe() {} | ||
| function dedupe(elements) { | ||
| if (elements.length === 0) { | ||
| return []; | ||
| } | ||
|
|
||
| return elements.filter((item, index) => elements.indexOf(item) === index); | ||
|
Comment on lines
+2
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. Could we expect line 6 to work also when array length is 0? |
||
| } | ||
|
|
||
| module.exports = dedupe; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,13 +16,46 @@ E.g. dedupe([1, 2, 1]) returns [1, 2] | |
| // Given an empty array | ||
| // When passed to the dedupe function | ||
| // Then it should return an empty array | ||
| test.todo("given an empty array, it returns an empty array"); | ||
| test("given an empty array, return an empty array", () => { | ||
| expect(dedupe([])).toEqual([]); | ||
| }); | ||
|
|
||
| // Given an array with no duplicates | ||
| // When passed to the dedupe function | ||
| // Then it should return a copy of the original array | ||
| const testCaseNoDuplicates = [ | ||
| { | ||
| input: ["hey", "buddy", "world", "10", "sister", 1], | ||
| expected: ["hey", "buddy", "world", "10", "sister", 1], | ||
| }, | ||
| { | ||
| input: [2, 3, 4, 5, 6, 7], | ||
| expected: [2, 3, 4, 5, 6, 7], | ||
| }, | ||
| ]; | ||
| testCaseNoDuplicates.forEach(({ input, expected }) => { | ||
| test("given an array with no duplicates, return a copy of the array", () => { | ||
| expect(dedupe(input)).toEqual(expected); | ||
| }); | ||
|
Comment on lines
+37
to
+39
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. Your function implementation is correct. However, this test could be improved to better ensure
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. Can you find out what this additional check is? |
||
| }); | ||
|
|
||
| // Given an array of strings or numbers | ||
| // When passed to the dedupe function | ||
| // Then it should return a new array with duplicates removed while preserving the | ||
| // Then it should return a new array with duplicates removed while preserving the | ||
| // first occurrence of each element from the original array. | ||
| const testCaseWithDuplicates = [ | ||
| { input: [1, 2, 2, 3, 1], expected: [1, 2, 3] }, | ||
| { input: ["a", "b", "a", "c", "b"], expected: ["a", "b", "c"] }, | ||
| { input: [5, 5, 1, 1, 2, 3, 2], expected: [5, 1, 2, 3] }, | ||
| { | ||
| input: ["apple", "banana", "apple", "orange"], | ||
| expected: ["apple", "banana", "orange"], | ||
| }, | ||
| { input: [1, "a", 1, "b", "a", 2], expected: [1, "a", "b", 2] }, | ||
| { input: ["hey", "hey", "hey", "hey", "hey", "hey"], expected: ["hey"] }, | ||
| ]; | ||
| testCaseWithDuplicates.forEach(({ input, expected }) => { | ||
| test("given an array with duplicates, return a new array with only the first occurrence", () => { | ||
| expect(dedupe(input)).toEqual(expected); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,23 @@ | ||
| function findMax(elements) { | ||
| if (elements.length === 0) { | ||
| return -Infinity; | ||
| } | ||
| const elementLists = elements.filter((number) => { | ||
| return typeof number === "number"; | ||
| }); | ||
| if (elementLists.length === 0) { | ||
| return undefined; | ||
| } | ||
|
Comment on lines
+8
to
+10
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 function is expected to return a number, it is better to design the function to always returned a value of type number for consistency. Technically, an empty array also an array containing no numbers. |
||
| if (elementLists.length === 1) { | ||
| return elementLists[0]; | ||
| } | ||
| let max = elementLists[0]; | ||
| for (let i = 1; i < elementLists.length; i++) { | ||
| if (elementLists[i] > max) { | ||
| max = elementLists[i]; | ||
| } | ||
| } | ||
| return max; | ||
| } | ||
|
|
||
| console.log(findMax([-1, 0, -2])); | ||
| module.exports = findMax; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,29 +15,93 @@ 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([])).toEqual(-Infinity); | ||
| }); | ||
|
|
||
| // Given an array with one number | ||
| // When passed to the max function | ||
| // Then it should return that number | ||
| const testCaseArrayWithOneNumber = [ | ||
| { input: [4], expected: 4 }, | ||
| { input: [5], expected: 5 }, | ||
| { input: [10], expected: 10 }, | ||
| ]; | ||
| testCaseArrayWithOneNumber.forEach(({ input, expected }) => { | ||
| test("given an array with one number, returns that number", () => { | ||
| 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 | ||
| const testCaseArrayWithPositiveAndNegativeNumbers = [ | ||
| { input: [-5, 3, -10, 8, 2, -1], expected: 8 }, | ||
| { input: [-20, 15, -3, 7, -100, 4], expected: 15 }, | ||
| { input: [-1, -50, 0.5, -2, -10], expected: 0.5 }, | ||
| ]; | ||
| testCaseArrayWithPositiveAndNegativeNumbers.forEach(({ input, expected }) => { | ||
| test("given a mix of positive and negative number, returns the largest number overall", () => { | ||
| expect(findMax(input)).toEqual(expected); | ||
| }); | ||
| }); | ||
|
|
||
| // Given an array with just negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the closest one to zero | ||
| const testCaseArrayWithOnlyNegativeNumbers = [ | ||
| { input: [-5, -3, -10, -8, -2, -1], expected: -1 }, | ||
| { input: [-20, -15, -3, -7, -100, -4], expected: -3 }, | ||
| { input: [-1, -50, -0.5, -2, -10], expected: -0.5 }, | ||
| ]; | ||
|
|
||
| testCaseArrayWithOnlyNegativeNumbers.forEach(({ input, expected }) => { | ||
| test("given negative numbers, returns the closest number to zero", () => { | ||
| expect(findMax(input)).toEqual(expected); | ||
| }); | ||
| }); | ||
|
|
||
| // Given an array with decimal numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest decimal number | ||
| const testCaseArrayWithOnlyDecimalNumbers = [ | ||
| { input: [1.5, 3.7, 2.4, 8.9, 4.2], expected: 8.9 }, | ||
| { input: [0.2, 0.8, 0.1, 0.6, 0.4], expected: 0.8 }, | ||
| { input: [-1.5, -3.2, -0.7, -5.6, -2.1], expected: -0.7 }, | ||
| ]; | ||
| testCaseArrayWithOnlyDecimalNumbers.forEach(({ input, expected }) => { | ||
| test("given decimal numbers, returns the largets decimal number", () => { | ||
| expect(findMax(input)).toEqual(expected); | ||
| }); | ||
| }); | ||
|
|
||
| // Given an array with non-number values | ||
| // When passed to the max function | ||
| // Then it should return the max and ignore non-numeric values | ||
| const testCaseArrayWithNonNumberValues = [ | ||
| { input: [5, "hello", 10, null, 3], expected: 10 }, | ||
| { input: ["world", -5, 8, undefined, 2], expected: 8 }, | ||
| { input: [true, 4, false, 12, "hey"], expected: 12 }, | ||
| { input: [-10, "hello", -2, null, -20], expected: -2 }, | ||
| { input: [2.5, {}, 7.8, [], "test"], expected: 7.8 }, | ||
| ]; | ||
| testCaseArrayWithNonNumberValues.forEach(({ input, expected }) => { | ||
| test("given an array including non-numerical values, returns the max and ignore non-numeric values", () => { | ||
| expect(findMax(input)).toEqual(expected); | ||
| }); | ||
| }); | ||
|
|
||
| // 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 | ||
| const testCaseArrayWithOnlyNonNumberValues = [ | ||
| { input: ["hello", "world"], expected: undefined }, | ||
| { input: [null, undefined, true, false], expected: undefined }, | ||
| { input: [{}, [], "test"], expected: undefined }, | ||
| ]; | ||
|
Comment on lines
+98
to
+102
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, |
||
| testCaseArrayWithOnlyNonNumberValues.forEach(({ input, expected }) => { | ||
| test("given an array with only non-number values, returns undefined", () => { | ||
| expect(findMax(input)).toEqual(expected); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,17 @@ | ||
| function sum(elements) { | ||
| if (elements.length === 0) { | ||
| return 0; | ||
| } | ||
| const elementsList = elements.filter((element) => { | ||
| return typeof element === "number"; | ||
| }); | ||
|
Comment on lines
+5
to
+7
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 (elementsList.length === 1) { | ||
| return elementsList[0]; | ||
| } | ||
| let sum = 0; | ||
| for (let i = 0; i < elementsList.length; i++) { | ||
| sum += elementsList[i]; | ||
| } | ||
| return sum; | ||
|
Comment on lines
+11
to
+15
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. Could this code work for an array of any length? |
||
| } | ||
|
|
||
| module.exports = sum; | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
.filter()returns a new array without mutating the original array, there is no need to clone another array.