Skip to content

London | 26-ITP-Jan | Zadri Abdule | Sprint 1 | Data - Groups#1090

Open
Zadri415 wants to merge 6 commits intoCodeYourFuture:mainfrom
Zadri415:coursework/sprint-1
Open

London | 26-ITP-Jan | Zadri Abdule | Sprint 1 | Data - Groups#1090
Zadri415 wants to merge 6 commits intoCodeYourFuture:mainfrom
Zadri415:coursework/sprint-1

Conversation

@Zadri415
Copy link
Copy Markdown

@Zadri415 Zadri415 commented Mar 24, 2026

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

  • fix: corrected calculateMedian in median.js and added tests
  • feat: implemented dedupe with tests (dedupe.js, dedupe.test.js)
  • feat: implemented findMax and tests (max.js, max.test.js)
  • test: added tests for sum (sum.test.js)
  • refactor: switched includes loop to for...of (includes.js)
  • feat: added AoC 2018 Day 1 solution (solution.js)

Questions

N/A

@github-actions

This comment has been minimized.

@Zadri415 Zadri415 added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. 📅 Sprint 1 Assigned during Sprint 1 of this module Module-Data-Groups The name of the module. labels Mar 24, 2026
Comment on lines +23 to +28
test("array with no duplicates returns a copy", () => {
const arr = [1, 2, 3];
const out = dedupe(arr);
expect(out).toEqual(arr);
expect(out).not.toBe(arr); // should be a new array
});
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.

There is a chance that, even though out has incorrect elements (for example, []),
the two tests on lines 27-28 could still pass. Can you figure out why, and then fix the tests accordingly?

Comment on lines +53 to +62
test("given an array with non-number values, returns the max and ignores non-numeric values", () => {
expect(findMax(["hey", 10, "hi", 60, 10])).toBe(60);
});

// Given an array with only non-number values
// When passed to the max 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 -Infinity", () => {
expect(findMax(["hey", "hi", "there"])).toBe(-Infinity);
});
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.

When a string representing a valid numeric literal (for example, "300") is compared to a number,
JavaScript first converts the string into its numeric equivalent before performing the comparison.
As a result, the expression 20 < "300" evaluates to true.

To test if the function can correctly ignore non-numeric values,
consider including a string such as "300" in the relevant test cases.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks. I'll add a test that includes a numeric-looking string("300") to ensure those strings are ignored, and implement findMax to only consider real number values.

Comment on lines +5 to +9
for (const el of elements) {
if (typeof el === "number") {
total += el;
}
}
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.

What do you expect from the following function calls (on extreme cases)?
Does your function return the value you expected?

sum([NaN, 1]);
sum([Infinity, -Infinity]);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks - great question. I chose to ignore non finite values so the function only adds finite numbers. That means:
sum[NaN, 1] -> 1
sum [Infinity, -Infinity]) -> 0

Comment on lines +42 to +44
test("given an array with decimal numbers, returns the correct sum", () => {
expect(sum([1.5, 2.5, 3.5])).toBe(7.5);
});
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 only yield 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 Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Module-Data-Groups The name of the module. 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