Skip to content

Conversation

@tyzia
Copy link
Contributor

@tyzia tyzia commented Mar 15, 2025

This PR suggests a few improvements in the proposed solution for calculateMedian() method.

The solution example now will have two advanced features:

  1. Throws an error for invalid input (non-numeric array).
  2. Filters array and calculates median for mixed arrays.

It also adds a very important step in calculating a median - sorts the array.

The PR also updates unit tests. If we don't think that student's implementation should handle non-numeric arrays or throw errors, I can remove tests related to these cases.

@tyzia tyzia requested a review from illicitonion March 15, 2025 19:52
Copy link
Member

@illicitonion illicitonion left a comment

Choose a reason for hiding this comment

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

Thanks! This generally looks good, but I have one question about introducing throw

if (list.length % 2 === 0) { // use the remainder operator (%) to check if the array length is even
return (list[middleIndex - 1] + list[middleIndex]) / 2;
if (!onlyNumberList.length) {
throw new Error('You provided a list without numbers.');
Copy link
Member

Choose a reason for hiding this comment

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

We haven't taught our trainees about throwing at this point (or in fact really at all in the course, which we should) - given the solutions branch is meant to be "What we'd expect our trainees to produce with their current knowledge" rather than "Ideal code", I'd be tempted to return null here?

Alternatively, we could introduce throw and try/catch at the start of this sprint, which would probably make sense? WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@illicitonion Hey, Daniel. I totally agree with you. After I created this PR I was thinking that this is probably too complicated and as you rightly say - you probably didn't teach throw and it is not correct to add it to the solution.

I would suggest to have two solutions: one just a good solution with return null, and one, as an example, with throw as an illustration how this method can be improved. I will push update so that you can take a look.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @illicitonion please take a look at updated version:

  • removed throw error
  • return null in case not an array or array with non-numeric values provided

I also keep a commit with both versions of this function (returning null and trowing an error) and related unit tests just in case we would like to add throw to learning materials and this example will be needed: a6fd706

Once this PR is approved and merged, I will create the same tests for main branch.

expect(list).toEqual([1, 2, 3]);
});

[ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val =>
Copy link
Member

Choose a reason for hiding this comment

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

We should add these tests to the main branch too, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Absolutely, the main should also be updated. I will first update the tests as to my comment above - not to expect throw and after we merge these changes into solutions, I will create similar change to main.

@tyzia tyzia requested a review from illicitonion March 21, 2025 17:36
Copy link
Member

@illicitonion illicitonion left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@tyzia
Copy link
Contributor Author

tyzia commented Mar 28, 2025

Hi @illicitonion Do I need to get any more approvals or you can merge it?

@illicitonion illicitonion merged commit 7e39c55 into CodeYourFuture:solutions Mar 28, 2025
@illicitonion
Copy link
Member

Merged! Thank you! If you can back-port the new/updated tests to main that'd be handy too!

tyzia added a commit to tyzia/Module-Data-Groups that referenced this pull request Mar 29, 2025
…ure#390)

* Update solution and tests for Sprint 1 'fix' exercise with median

* Add tests to check for errors and mixed arrays (advanced implementation of median)

* Create two vesions of calculate median function: with throw error and with return null

* Remove calculate median with throwing error and related tests
@tyzia
Copy link
Contributor Author

tyzia commented Mar 29, 2025

Created similar change for main branch. @illicitonion please review: #452

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants