Skip to content

Glasgow | 26-ITP-May | Niangh Ciang | Sprint 1 | Exercises#1241

Open
Niangh-Ciang wants to merge 10 commits into
CodeYourFuture:mainfrom
Niangh-Ciang:coursework/sprint-1
Open

Glasgow | 26-ITP-May | Niangh Ciang | Sprint 1 | Exercises#1241
Niangh-Ciang wants to merge 10 commits into
CodeYourFuture:mainfrom
Niangh-Ciang:coursework/sprint-1

Conversation

@Niangh-Ciang

Copy link
Copy Markdown

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

Completed Sprint 1 exercises.

@Niangh-Ciang Niangh-Ciang 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. Module-Data-Groups The name of the module. labels Jul 10, 2026
@LonMcGregor LonMcGregor added the Review in progress This review is currently being reviewed. This label will be replaced by "Reviewed" soon. label Jul 14, 2026

@LonMcGregor LonMcGregor left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good work on these tasks. I have some questions in the comments to check you fully understand the solutions that you've written. Could you answer them please?

Comment thread Sprint-1/fix/median.js Outdated
if (!Array.isArray(list)) {
return null;
}
const numbersOnly = list.filter((value) => typeof value === "number");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do you know of any values which would pass this typeof check, but which are not actually numbers?

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.

I researched more about this, and I now understand that NaN, Infinity, and -Infinity would all pass the typeof value === "number" check, even though they are not valid finite numbers. These values are treated as “number” by JavaScript’s typeof operator, so they would still be included by my current filter.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good idea - is there an addition you can make to the filter that would catch these?

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.

I changed the filter to list.filter(Number.isFinite) so NaN, Infinity, and -Infinity don’t get included.

Comment thread Sprint-1/fix/median.js
return null;
}

const sortedList = [...numbersOnly].sort((a, b) => a - b);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What is the spread operator doing here?

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.

The spread operator makes a copy of the numbersOnly array before sorting it. Because .sort() changes the original array, using [...numbersOnly] lets me sort a copy instead of modifying the original.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Does it matter if sort changes the original array? Why do you need to avoid that here?

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.

Since numbersOnly is already a new array created by filter(), sorting it doesn’t modify the original list. That means the spread operator isn’t strictly necessary for this function to work. Using numbersOnly.sort((a, b) => a - b) would also pass all the tests — including the one that checks the input array is not changed (expect(list).toEqual([3, 1, 2])). I used [...numbersOnly] simply to avoid unexpected side effects

Comment thread Sprint-1/implement/sum.js
total += element;
}
}
total = Number(total.toFixed(2));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why are you using toFixed here?

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.

I used toFixed(2) to avoid floating‑point precision errors. In my decimal test (expect(sum([-5, 2.3, 7.5, 3.9, 2.1])).toEqual(10.8)), JavaScript produces 10.799999999999 instead of 10.8. Rounding to 2 decimal places ensures the test passes correctly, which is why I included it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

OK. This approach makes sense. Just be careful in future if you are working with arithmetic that you don't accidentally introduce inaccuracy by rounding more than you need to.

@LonMcGregor LonMcGregor 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 14, 2026
@Niangh-Ciang Niangh-Ciang added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Jul 14, 2026
@LonMcGregor LonMcGregor 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 15, 2026
@LonMcGregor

Copy link
Copy Markdown

Good work - this task is done. Your reasoning for using [... array] makes sense, but I would suggest that if it's not strictly required you shouldn't use it as it adds a bit of unnecessary complexity to code.

@LonMcGregor LonMcGregor added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. Review in progress This review is currently being reviewed. This label will be replaced by "Reviewed" soon. labels Jul 15, 2026
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. Module-Data-Groups The name of the module. 📅 Sprint 1 Assigned during Sprint 1 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants