-
-
Notifications
You must be signed in to change notification settings - Fork 275
Manchester | 26-ITP-Jan | Ofonime Edak | Sprint 1 | Data group #1045
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
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,13 @@ | ||
| function dedupe() {} | ||
| function dedupe(arr) { | ||
| if (arr.length === 0) return []; | ||
| const dedupeArray = []; | ||
| for (let i = 0; i < arr.length; i++) { | ||
| if (!dedupeArray.includes(arr[i])) { | ||
| dedupeArray.push(arr[i]); | ||
| } | ||
| } | ||
|
|
||
| return dedupeArray; | ||
| } | ||
| console.log(dedupe([1, 1, 2])); | ||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| const dedupe = require("./dedupe.js"); | ||
| const dedupe = require("./dedupe"); | ||
| /* | ||
| Dedupe Array | ||
|
|
||
|
|
@@ -16,12 +16,47 @@ 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"); | ||
| describe("dedupe()", () => { | ||
| [{ input: [], expected: [] }].forEach(({ input, expected }) => | ||
| it(`given an empty array, it returns an empty array [${input}]`, () => { | ||
| expect(dedupe(input)).not.toBe(expected); | ||
| }) | ||
| ); | ||
| // Given an array with no duplicates | ||
| // Then it should return a copy of the original array | ||
| [ | ||
| { input: [1, 2, 3, 4], expected: [1, 2, 3, 4] }, | ||
| { | ||
| input: ["apples", "banana", "orange"], | ||
| expected: ["apples", "banana", "orange"], | ||
| }, | ||
| { input: [-1, 7, 1], expected: [-1, 7, 1] }, | ||
| ].forEach(({ input, expected }) => | ||
| it(`should return same input values [${input}] without duplicate`, () => { | ||
| expect(dedupe(input)).toStrictEqual(expected); | ||
| }) | ||
|
Comment on lines
+35
to
+37
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 check cannot check if the function is returning a different array. |
||
| ); | ||
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Given an array with no duplicates | ||
| // When passed to the dedupe function | ||
| // Then it should return a copy of the original array | ||
| // When passed to the dedupe function | ||
| // 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 | ||
|
|
||
| // 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 | ||
| [ | ||
| { input: [1, 2, 5, 5, "a", 5, 10, 10, "a"], expected: [1, 2, 5, "a", 10] }, | ||
| { | ||
| input: ["apple", "banana", "orange", "apple", "banana", 1, 3, 4, 1], | ||
| expected: ["apple", "banana", "orange", 1, 3, 4], | ||
| }, | ||
| ].forEach(({ input, expected }) => | ||
| it(`should return deduplicated array for [${input}]`, () => { | ||
| expect(dedupe(input)).toStrictEqual(expected); | ||
| }) | ||
| ); | ||
|
|
||
| [{ input: [1, 1, 2], expected: [1, 2] }].forEach(({ input, expected }) => | ||
| it("returns a copy not the original array", () => { | ||
| expect(dedupe(input)).toStrictEqual(expected); | ||
| }) | ||
| ); | ||
|
Comment on lines
+57
to
+61
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 check is not checking if the function is returning a copy of the original array or not. |
||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,20 @@ | ||
| function findMax(elements) { | ||
| if (!Array.isArray(elements)) return "invalid elements"; | ||
| const number = []; | ||
| for (let i = 0; i < elements.length; i++) { | ||
| if (typeof elements[i] === "number" && !Number.isNaN(elements[i])) { | ||
| number.push(elements[i]); | ||
| } | ||
| } | ||
| if (number.length === 0) return -Infinity; | ||
| let max = number[0]; | ||
|
|
||
| for (let i = 1; i < number.length; i++) { | ||
| if (max < number[i]) { | ||
| max = number[i]; | ||
| } | ||
| } | ||
| return max; | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,18 @@ | ||
| function sum(elements) { | ||
| if (!Array.isArray(elements)) return "invalid elements"; | ||
| if (elements.length === 0) return 0; | ||
| const number = []; | ||
| for (const x of elements) { | ||
| if (typeof x === "number" && !Number.isNaN(x)) { | ||
| number.push(x); | ||
| } | ||
| } | ||
| if (number.length === 0) return "invalid elements"; | ||
| let sumOfNum = 0; | ||
| for (let i = 0; i < number.length; i++) { | ||
| sumOfNum += number[i]; | ||
| } | ||
| return sumOfNum; | ||
| } | ||
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| module.exports = sum; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,27 +10,73 @@ 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") | ||
|
|
||
| // 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 | ||
|
|
||
| // 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 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 | ||
|
|
||
| // 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 | ||
| describe("sum()", () => { | ||
| // Given an empty array | ||
| // When passed to the sum function | ||
| // Then it should return 0 | ||
| [{ input: [], expected: 0 }].forEach(({ input, expected }) => | ||
| it(`should return ${expected} for [${input}]`, () => { | ||
| expect(sum(input)).toEqual(expected); | ||
| }) | ||
| ); | ||
|
|
||
| // Given an array with just one number | ||
| // When passed to the sum function | ||
| // Then it should return that number | ||
|
|
||
| [{ input: [30], expected: 30 }].forEach(({ input, expected }) => | ||
| it(`should return ${expected} for [${input}]`, () => { | ||
| expect(sum(input)).toEqual(expected); | ||
| }) | ||
| ); | ||
|
|
||
| // Given an array containing negative numbers | ||
| // When passed to the sum function | ||
| // Then it should still return the correct total sum | ||
|
|
||
| [{ input: [-1, -3, -4, -11], expected: -19 }].forEach(({ input, expected }) => | ||
| it(`should return ${expected} for [${input}]`, () => { | ||
| expect(sum(input)).toEqual(expected); | ||
| }) | ||
| ); | ||
|
|
||
| // Given an array with decimal/float numbers | ||
| // When passed to the sum function | ||
| // Then it should return the correct total sum | ||
|
|
||
| [{ input: [0.5, 0.2, 0.11, 0.89, 0.3], expected: 2 }].forEach( | ||
| ({ input, expected }) => | ||
| it(`should return ${expected} for [${input}]`, () => { | ||
| expect(sum(input)).toEqual(expected); | ||
| }) | ||
| ); | ||
|
Comment on lines
+47
to
+52
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
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 this. I have read about the matchers for numbers and floats now
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. I don't see any change made to the relevant Jest test. |
||
|
|
||
| // 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: ["evan", 3, "mike", 20, 6, "", "/", undefined, null, 20], | ||
| expected: 49, | ||
| }, | ||
| ].forEach(({ input, expected }) => | ||
| it(`should return ${expected} for [${input}]`, () => { | ||
| expect(sum(input)).toEqual(expected); | ||
| }) | ||
| ); | ||
|
|
||
| // 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: ["evan", "mike", "", "/", undefined], | ||
| expected: "invalid elements", | ||
| }, | ||
| ].forEach(({ input, expected }) => | ||
| it(`should return ${expected} for [${input}]`, () => { | ||
| expect(sum(input)).toEqual(expected); | ||
| }) | ||
| ); | ||
| }); | ||
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.
This check is not checking if the return value is an empty array.