Skip to content

Conversation

@jakr-s
Copy link

@jakr-s jakr-s commented Dec 6, 2025

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

Complete all mandatory tasks

@jakr-s jakr-s added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Dec 6, 2025
Copy link
Contributor

@cjyuan cjyuan left a comment

Choose a reason for hiding this comment

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

Code is working great.
I only have a few suggestions.

Comment on lines 25 to +29
// Then it should return a copy of the original array
test("given an array with no duplicates, it returns a copy of the original array", () => {
expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]);
expect(dedupe(["a", "b", "c"])).toEqual(["a", "b", "c"]);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

This test is expected to check also whether the returned array is a copy of the original array --
that is, whether the argument and the returned value of dedupe(array) are two different arrays.

The following test only checks whether both arrays contain the same elements;
it cannot check whether the arrays are actually different objects:

  expect(array1).isEqual(array2);

Suggestion: Find out how to test whether two arrays are different arrays in Jest.

Comment on lines +6 to +8
const numbers = elements
.filter((val) => typeof val === "number")
.sort((a, b) => a - b);
Copy link
Contributor

Choose a reason for hiding this comment

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

  • Sorting is unnecessary before calling Math.max()
  • Could also consider filter out NaN (also a value of type number)

Comment on lines +37 to +39
test("given an array with decimal numbers, returns correct sum", () => {
expect(sum([10.5, 20.3, 5.2])).toBe(36);
});
Copy link
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 JavaScript) are internally represented in floating-point format.
Floating-point arithmetic is not exact. For example, the expression 46.5678 - 46 === 0.5678 evaluates to false
because 46.5678 - 46 produces a value that is only very close to 0.5678, not exactly equal.
Even changing the order in which numbers are added or subtracted can lead to slightly different results.

So the following could happen

  expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.805 );                // This will fail
  expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.8049999999999997 );   // This will pass
  expect( 0.005 + 0.6 + 1.2 ).toEqual( 1.8049999999999997 );   // This will 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

  • how to check equality in floating-point arithmetic in JavaScript
  • how to check equality in floating-point arithmetic using Jest

@cjyuan cjyuan added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Dec 6, 2025
@jakr-s jakr-s closed this Dec 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants