Skip to content

Manchester | 26-ITP-Sep | Salah Alsabhi | Sprint 1 | Coursework - #1482

Open
salahalsabhi wants to merge 8 commits into
CodeYourFuture:mainfrom
salahalsabhi:coursework/sprint-1
Open

salahalsabhi wants to merge 8 commits into
CodeYourFuture:mainfrom
salahalsabhi:coursework/sprint-1

Conversation

@salahalsabhi

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

Task code

CYF-1071

Changelist

Sprint 1: fix, implement, refactor, stretch

Questions

No questions

@github-actions

This comment has been minimized.

@salahalsabhi salahalsabhi changed the title Manchester | 26-ITP-Sep | Salah Alsabhi | Sprint-1 | Coursework/sprint 1 Manchester | 26-ITP-Sep | Salah Alsabhi | Sprint-1 Nov 1, 2026
@github-actions

This comment has been minimized.

@salahalsabhi salahalsabhi changed the title Manchester | 26-ITP-Sep | Salah Alsabhi | Sprint-1 Manchester | 26-ITP-Sep | Salah Alsabhi | coursework | Sprint-1 Nov 1, 2026
@github-actions

This comment has been minimized.

@salahalsabhi salahalsabhi changed the title Manchester | 26-ITP-Sep | Salah Alsabhi | coursework | Sprint-1 Manchester | 26-ITP-Sep | Salah Alsabhi | Sprint 1 | Coursework Nov 1, 2026
@salahalsabhi salahalsabhi added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Nov 1, 2026
@salahalsabhi

Copy link
Copy Markdown
Author

Ready for review.

Completed Sprint 1:

  • fix: calculateMedian
  • implement: findMax, sum, dedupe, mean, describeMedian
  • refactor: includes (for...of)
  • stretch: AoC 2018 day 1

All tests passing locally with npm test.

Comment on lines 23 to +28
// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
test("Given an array with no duplicates, should return a copy of the original array", () => {
expect([5, 1, 2, 3, 8]).toEqual([5, 1, 2, 3, 8]);
});

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.

Your function implementation is correct. However, this test could be improved to better ensure
that any future changes continue to align with the expected behavior described on line 25:

Then it should return a copy of the original array

This test should fail if the function returns the original array (instead of a copy of the original array).

The current test checks only if both the original array and the returned array contain identical elements.
In order to validate the returned array is a different array, we need an additional check.

Can you find out what this additional check is?

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.

How should the function handle an array containing NaN?

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 should calculateMean([Infinity, 0, -Infinity]) or calculateMean([0, NaN]) return?

Comment on lines +44 to +50
test("Given an array containing decimal numbers, should return the correct mean", () => {
expect(calculateMean([2.2, 1.9, 3.5, 4.1])).toEqual(2.925);
});

test("Given an array containing both negative and decimal numbers, should return the correct mean", () => {
expect(calculateMean([2.2, -1.9, 3.5, -4.1])).toEqual(-0.07499999999999984);
});

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

Comment on lines +4 to +9
// Puzzle input
const changes = fs
.readFileSync(new URL("./input.txt", import.meta.url), "utf8")
.trim()
.split("\n")
.map(Number);

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 you explain the purpose of calling trim() on the input before applying split('\n')?

Comment on lines +15 to +17
for (let change of changes) {
frequency += change;
}

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.

Why not declare the loop variable change using const?

@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 Nov 4, 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants