Manchester | 26-ITP-May | Samreen Amjad | Sprint 1 | Exercises - #1291
Open
serveyano wants to merge 1 commit into
Open
Manchester | 26-ITP-May | Samreen Amjad | Sprint 1 | Exercises #1291serveyano wants to merge 1 commit into
serveyano wants to merge 1 commit into
Conversation
cjyuan
reviewed
Jul 30, 2026
| // Then it should return the max and ignore non-numeric values | ||
|
|
||
| it("array with non-number values, returns the max and ignore non-numeric values", () => { | ||
| const list = [1, "a", 2, "b", 3, "c"]; |
Contributor
There was a problem hiding this comment.
Could also include numerical string to ensure value like "123" is ignored.
| function sum(list) { | ||
| const numbers = list.filter((n) => Number.isFinite(n)); | ||
| let total = 0; | ||
| for (n of numbers) { |
Comment on lines
+43
to
+46
| it("array with decimal numbers returns correct sum", () => { | ||
| const list = [1.1, 2.2, 3.3]; | ||
| expect(sum(list)).toEqual(6.6); | ||
| }); |
Contributor
There was a problem hiding this comment.
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 46.5678 - 46 === 0.5678 is false because 46.5678 - 46 is evaluated to a value that is very close to 0.5678. Even changing the order in which the program add/subtract numbers can yield different values.
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
- Checking equality in floating point arithmetic in JavaScript
- Checking equality in floating point arithmetic with Jest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Learners, PR Template
Self checklist
I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
My changes meet the requirements of the task
I have tested my changes
My changes follow the style guide
Changelist
Sprint-1 exercises completed