Skip to content

Manchester | 26-ITP-May | Samreen Amjad | Sprint 1 | Exercises - #1291

Open
serveyano wants to merge 1 commit into
CodeYourFuture:mainfrom
serveyano:Module-Data-Groups-Sprint-1
Open

Manchester | 26-ITP-May | Samreen Amjad | Sprint 1 | Exercises #1291
serveyano wants to merge 1 commit into
CodeYourFuture:mainfrom
serveyano:Module-Data-Groups-Sprint-1

Conversation

@serveyano

Copy link
Copy Markdown

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

@serveyano serveyano added 📅 Sprint 1 Assigned during Sprint 1 of this module Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Jul 25, 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"];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also include numerical string to ensure value like "123" is ignored.

Comment thread Sprint-1/implement/sum.js
function sum(list) {
const numbers = list.filter((n) => Number.isFinite(n));
let total = 0;
for (n of numbers) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also declare n.

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);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 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); // false

Can 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

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Reviewed Volunteer to add when completing a review with trainee action still to take. 📅 Sprint 1 Assigned during Sprint 1 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants