Skip to content

Manchester | 26-ITP-May | Yee Man Tsang | Sprint 1 | Data Group Coursework - #1359

Open
lintsang wants to merge 2 commits into
CodeYourFuture:mainfrom
lintsang:Coursework/Sprint-1
Open

Manchester | 26-ITP-May | Yee Man Tsang | Sprint 1 | Data Group Coursework#1359
lintsang wants to merge 2 commits into
CodeYourFuture:mainfrom
lintsang:Coursework/Sprint-1

Conversation

@lintsang

@lintsang lintsang commented Aug 2, 2026

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

Finished all exercises in Sprint 1 folder

@lintsang lintsang added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Aug 2, 2026
@cjyuan cjyuan added Review in progress This review is currently being reviewed. This label will be replaced by "Reviewed" soon. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Aug 7, 2026

@cjyuan cjyuan left a comment

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.

Tests are quite comprehensive.!

Comment thread Sprint-1/fix/median.js
Comment on lines +11 to +13
const listNumberOnlyAsc = [...list].filter(
(item) => !isNaN(item) && item !== null
);

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 clone the original array when using .filter()? (This comment applies to all the code that that uses .filter().)

Comment thread Sprint-1/fix/median.js
const listNumberOnlyAsc = [...list].filter(
(item) => !isNaN(item) && item !== null
);
if (listNumberOnlyAsc == undefined || listNumberOnlyAsc == 0) {

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.

Will the condition on line 14 ever be true?

Comment thread Sprint-1/fix/median.js
Comment on lines +17 to +19
const listPureNumber = [...listNumberOnlyAsc].filter(
(item) => typeof item == "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.

  • Why not include this filtering condition in the filter on lines 11-13 so that we only need to filter the array once?

  • You may also want to check out the methods in Number to see if there is any suitable method you can use to retain valid numbers.

Comment thread Sprint-1/fix/median.js

if (listSorted.length % 2 === 1) {
const middleIndex = Math.floor(listSorted.length / 2);
const median = [...listSorted].splice(middleIndex, 1)[0]; //code '.splice(middleIndex,1)' means remove the middle index num in array and put it in a new array, '[0]' is the first item in this 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.

Why not use the same syntax you are using on line 30?

Comment thread Sprint-1/fix/median.js
Comment on lines +33 to +35
} else {
return null;
}

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.

We could also express

  if (condition) {
    ... // Code to handle normal cases
  } else {
    return null; 
  }

as

  if (!condition) { // Opposite condition
    return null;
  }

  ... // Code to handle normal cases

This way, we don't need to use else. This is possible only if the else part has a return statement.

Comment thread Sprint-1/implement/max.js
Comment on lines +12 to +14
const elementsSorted = elementsPureNumber.sort((a, b) => a - b);
const elementsMax = elementsSorted.toSpliced(0, elementsSorted.length - 1);
return elementsMax[0];

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.

  • You could also just return the last element of the array without first deleting elements from the array.

  • You could also sort the array in reverse order and access its first element.

Comment thread Sprint-1/implement/sum.js
Comment on lines +2 to +5
const elementsNumberOnly = [...elements].filter((element) => !isNaN(element));
const elementsPureNumber = [...elementsNumberOnly].filter(
(item) => typeof item == "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.

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

Comment thread Sprint-1/implement/sum.js
} else if (elementsPureNumber.length > 1) {
const iterator = elementsPureNumber.values();
let sumOfArray = 0;
for (const value of iterator) {

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.

for-of loop works on array directly (arrays are iterable). You could just write for (const value of elementsPureNumber) { .

Comment thread Sprint-1/implement/sum.js
Comment on lines +7 to +18
if (elementsPureNumber.length === 0) {
return 0;
} else if (elementsPureNumber.length === 1) {
return elementsPureNumber[0];
} else if (elementsPureNumber.length > 1) {
const iterator = elementsPureNumber.values();
let sumOfArray = 0;
for (const value of iterator) {
sumOfArray += value;
}
return sumOfArray;
}

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.

The code on lines 13-17 also works for array of any size. If you prefer having less code, you could consider using the same code to handle all cases. That way, you don't need to use if-else.

Comment on lines +46 to +52
{ input: [1.1, 1.2], expected: 2.3 },
{ input: [-1.1, -2.4, 1.1], expected: -2.4 },
{ input: [-15.38, -3.2, -5.01, -10.0], expected: -33.59 },
].forEach(({ input, expected }) =>
it(`returns the sum in array contains decimal number [${input}]`, () =>
expect(sum(input)).toEqual(expected))
);

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 Review in progress This review is currently being reviewed. This label will be replaced by "Reviewed" soon. labels Aug 7, 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