-
-
Notifications
You must be signed in to change notification settings - Fork 270
Sheffield | 26-Jan-ITP | Martha Ogunbiyi| Sprint 1 | Coursework/sprint 1 #1054
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
53784da
605551b
ecc365d
6af6d59
4950064
70627a0
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,5 @@ | ||
| function dedupe() {} | ||
| function dedupe(elements) { | ||
| if (elements.length === 0) return []; | ||
| return elements.filter((item, index) => elements.indexOf(item) === index); | ||
| } | ||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,12 +16,31 @@ E.g. dedupe([1, 2, 1]) target output: [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, it returns an empty array", () => { | ||
| const input = []; | ||
| const result = dedupe(input); | ||
| expect(result).toEqual([]); | ||
| }); | ||
|
|
||
| // Given an array with no duplicates | ||
| // When passed to the dedupe function | ||
| // Then it should return a copy of the original array | ||
| test("given an array with no duplicates, it returns return a copy of the original array", () => { | ||
| const input = [1, 2, 3, 4, 5]; | ||
| const result = dedupe(input); | ||
| expect(result).toEqual([1, 2, 3, 4, 5]); | ||
| }); | ||
|
Comment on lines
25
to
+32
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. 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 with strings or numbers | ||
| // When passed to the dedupe function | ||
| // Then it should remove the duplicate values, preserving the first occurence of each element | ||
| // Then it should remove the duplicate values, preserving the first occurrence of each element | ||
| test("given an array with strings, it removes the duplicate, preserving first occurrence of each element", () => { | ||
| const input = ["a", "a", "a", "b", "b", "c"]; | ||
| const result = dedupe(input); | ||
| expect(result).toEqual(["a", "b", "c"]); | ||
| }); | ||
| test("given an array with numbers it removes the duplicate, preserving first occurrence of each element", () => { | ||
| const input = [5, 1, 1, 2, 3, 2, 5, 8]; | ||
| const result = dedupe(input); | ||
| expect(result).toEqual([5, 1, 2, 3, 8]); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,10 @@ | ||
| function findMax(elements) { | ||
| if (!Array.isArray(elements) || elements.length === 0) return -Infinity; | ||
| return Math.max( | ||
| ...elements.filter( | ||
| (item) => typeof item === "number" && !Number.isNaN(item) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,28 +16,62 @@ 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", () => { | ||
| const input = [] | ||
| const result = findMax(input) | ||
| expect(result).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, its should return that number', () => { | ||
| const input = [5] | ||
| const result = findMax(input) | ||
| expect(result).toBe(5); | ||
| }); | ||
|
|
||
| // 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, it should return largest overall', () => { | ||
| const input = [5, -5, -10, -1, 6, 8, 10] | ||
| const result = findMax(input) | ||
| expect(result).toBe(10); | ||
| }); | ||
|
|
||
| // 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, it should return the closest one to zero', () => { | ||
| const input = [-10, -5, -2, -8]; | ||
| const result = findMax(input); | ||
| expect(result).toBe(-2); | ||
| }) | ||
|
|
||
| // 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 just decimal numbers, it should return the largest decimal number", () => { | ||
| const input = [1.0, 2.5, 2.2, 5.8]; | ||
| const result = findMax(input); | ||
| expect(result).toBe(5.8); | ||
| }); | ||
|
|
||
| // 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 value, it should return the max and ignore non-numeric value", () => { | ||
| const input = [4, 5, 'arr', 2, []," ", 15, 8]; | ||
| const result = findMax(input); | ||
| expect(result).toBe(15); | ||
| }); | ||
|
|
||
| // 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 value, it should return the least surprising value given how it behaves for all other inputs", () => { | ||
| const input = [true, {}, "arr", null, [], " ", NaN]; | ||
| const result = findMax(input); | ||
| expect(result).toBe(-Infinity); | ||
| }); | ||
|
Comment on lines
61
to
+77
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,8 @@ | ||
| function sum(elements) { | ||
| if (!Array.isArray(elements) || elements.length === 0) return 0; | ||
| return elements | ||
| .filter((item) => typeof item === "number" && !Number.isNaN(item)) | ||
| .reduce((acc, n) => acc + n, 0); | ||
| } | ||
|
|
||
| module.exports = sum; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,24 +13,53 @@ 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", () => { | ||
| const input = []; | ||
| const result = sum(input); | ||
| expect(result).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, it should return that number", () => { | ||
| const input = [5]; | ||
| const result = sum(input); | ||
| expect(result).toBe(5); | ||
| }); | ||
|
|
||
| // 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, it should return the correct total sum", () => { | ||
| const input = [5, -6, 10, -12]; | ||
| const result = sum(input); | ||
| expect(result).toBe(-3); | ||
| }); | ||
|
|
||
| // 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, it should return the correct total sum", () => { | ||
| const input = [5, 6.6, 10, 5.12]; | ||
| const result = sum(input); | ||
| expect(result).toBe(26.720000000000002); | ||
| }); | ||
|
Comment on lines
40
to
+47
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, it should ignore the numerical values and return sum of numeric element", () => { | ||
| const input = [4, 5, "arr", 2, [], " ", 15, 8]; | ||
| const result = sum(input); | ||
| expect(result).toBe(34); | ||
| }); | ||
|
|
||
| // 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, it should return least surprising value given how it behaves for all other inputs", () => { | ||
| const input = [true, {}, "arr", null, [], " ", NaN]; | ||
| const result = sum(input); | ||
| expect(result).toBe(0); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,7 @@ | ||
| // 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]; | ||
| for (const element of list) { | ||
| if (element === target) { | ||
| return true; | ||
| } | ||
|
|
@@ -11,3 +10,10 @@ function includes(list, target) { | |
| } | ||
|
|
||
| module.exports = includes; | ||
|
|
||
| // const array = ["a", "b", "c"]; | ||
|
|
||
| // for (const element of array) { | ||
| // console.log(element); | ||
| // } for (variable of iterable) statement | ||
|
|
||
|
Comment on lines
+13
to
+19
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. It's best practice to remove unnecessary code to keep the code clean. |
||
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.
Why make a copy of
numericValuesbefore sorting?