-
-
Notifications
You must be signed in to change notification settings - Fork 221
Glasgow | 25-ITP-SEP | Fares Bakhet | Sprint 1 | Coursework/sprint 1 #825
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
Changes from all commits
dcdec45
589c8d9
3ebe1f9
8817635
744af5d
61a84ce
1762ae0
bf825f1
c8d3734
cd9b36a
9420f6a
c8f6f8f
89e0e23
8495512
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,6 @@ | ||
| function dedupe() {} | ||
| function dedupe(arr) { | ||
| const deduplicates = arr.filter((item, index) => arr.indexOf(item) === index); | ||
| return deduplicates; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| function findMax(elements) { | ||
| function findMax(arr) { | ||
| if (!Array.isArray(arr) || arr.length === 0) return -Infinity; | ||
|
|
||
| const numbers = arr.filter((n) => typeof n === "number" && !isNaN(n)); | ||
|
|
||
| if (numbers.length > 0) { | ||
| return Math.max(...numbers); | ||
| } | ||
|
|
||
| return arr[arr.length - 1]; | ||
| } | ||
|
|
||
| module.exports = findMax; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,11 @@ | ||
| function sum(elements) { | ||
| let totalSum = 0; | ||
| for (let i = 0; i < elements.length; i++) { | ||
| if (typeof elements[i] === "number") { | ||
| totalSum += elements[i]; | ||
| } | ||
| } | ||
| return totalSum; | ||
| } | ||
|
|
||
| module.exports = sum; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,24 +13,41 @@ 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([])).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 one number, returns that number", () => { | ||
| expect(sum([42])).toBe(42); | ||
| }); | ||
|
|
||
| // 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])).toBe(-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 numbers, returns the correct total sum", () => { | ||
| expect(sum([1.5, 2.5, 3.5])).toBeCloseTo(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 with non-number values, ignores them and returns the sum of numbers", () => { | ||
| expect(sum([10, "hello", 20, true, 30, null])).toBe(60); | ||
| }); | ||
|
|
||
| // 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(["hello", true, null])).toBe(0); | ||
| }); | ||
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 is an odd choice of value to return.
Is there any value you don't expect
findMax()to ever return?