-
-
Notifications
You must be signed in to change notification settings - Fork 279
Glasgow| 2026-ITP-Jan | Tuan Nguyen| Sprint 1 | Array-and-Loop. #1189
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
ff7f2fc
7fb1d60
558b61f
d17be19
a647b10
530533c
adf5466
bbd2d7e
591925a
bf6c830
1f124e8
1665dd5
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(inputArray) { | ||
| if (inputArray.length === 0) return inputArray; | ||
| let fixArray = []; | ||
| for (const item of inputArray) { | ||
| if (!fixArray.includes(item)) { | ||
| fixArray.push(item); | ||
| } | ||
| } | ||
| return fixArray; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| function findMax(elements) { | ||
| if (elements.length === 0) return -Infinity; | ||
|
|
||
| const filteredArray = elements.filter( | ||
| (item) => typeof item === "number" && !isNaN(item) | ||
| ); | ||
| if (filteredArray.length === 0) return null; | ||
| const sortedArray = filteredArray.sort((a, b) => a - b); | ||
| const maxArrayValue = sortedArray[sortedArray.length - 1]; | ||
| return maxArrayValue; | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,14 @@ | ||
| function sum(elements) { | ||
| if (elements.length === 0) return 0; | ||
| let filteredArray = elements.filter( | ||
| (items) => typeof items === "number" && !isNaN(items) | ||
| ); | ||
| if (filteredArray.length === 0) return null; | ||
| let sumTotal = 0; | ||
| for (let i = 0; i < filteredArray.length; i++) { | ||
| sumTotal += filteredArray[i]; | ||
| } | ||
| return sumTotal; | ||
| } | ||
|
|
||
| module.exports = sum; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,24 +13,47 @@ 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", () => { | ||
| let emptyArray = []; | ||
| expect(sum(emptyArray)).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 1 number value, returns that number value", () => { | ||
| let mixArray = ["1", "Hello", 20, null, undefined]; | ||
| expect(sum(mixArray)).toBe(20); | ||
| }); | ||
|
|
||
| // 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 with 1 number value, returns that number value", () => { | ||
| let negativeArray = [-1, -20, -30, -50, -60]; | ||
| expect(sum(negativeArray)).toBe(-161); | ||
| }); | ||
|
|
||
| // 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 1 number value, returns that number value", () => { | ||
| let negativeArray = [1.5, 20.3, 50.43, 6.7, 10.5]; | ||
| expect(sum(negativeArray)).toBeCloseTo(89.43,2); | ||
| }); | ||
|
|
||
| // 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 with 1 number value, returns that number value", () => { | ||
| let mixedArray2 = ["Hello", 20.17, null, 9.7, undefined]; | ||
| expect(sum(mixedArray2)).toBe(29.87); | ||
| }); | ||
|
Comment on lines
+48
to
+51
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 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 1 number value, returns that number value", () => { | ||
| let nonNumberArray = ["Hello", "2", null, "@", undefined]; | ||
| expect(sum(nonNumberArray)).toBe(null); | ||
| }); | ||
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 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?
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.
Hello thank you for the feed back, I could see why you point this out. I have check with the function and see that error pop up since. If I pass the normalArray and make any change that when return a new normalArray that do not have the same as the content if will result in error. So here my code for additional check:
test("with input of array with no duplicates should return a copy of original array", () => {
let normalArray = [1, 2, 3, 4, 5, 6, 10];
expect(dedupe(normalArray)).toEqual(normalArray);
expect(dedupe(normalArray)).not.toBe(normalArray);
});