Repository navigation
London | 26-ITP-Sept | Fung Nin Lee | Sprint 1 | Sprint-1 exercise - #1496
leerogerfn wants to merge 13 commits into
Conversation
There was a problem hiding this comment.
-
How should the function handle an array containing
NaN? -
How should the function handle an array containing elements that are not numbers or strings (e.g.,
null,undefined,true,console(object), etc.)?
There was a problem hiding this comment.
The assessment seems do not require to handle NaN and array which are neither numbers nor strings.
There was a problem hiding this comment.
-
NaNis also a value of type "number". HandlingNaNis optional in this exercise but you should find out how to check if a value is notNaN. -
The comment in
max.test.jsdoes mention "any value that isn't a number"// Given an array containing a value that isn't a number // When passed to the max function // Then it should throw Error("findMax requires an array of numbers")So the function should also be able to throw an error when the given array contains any value that is not a number.
There was a problem hiding this comment.
What should calculateMean([Infinity, 0, -Infinity]) or calculateMean([0, NaN]) return?
The same also applies to sum().
There was a problem hiding this comment.
The assessment seems do not require to handle the scenario you mentioned.
There was a problem hiding this comment.
-Infinity and Infinity are also values of type "number". Change is optional, but you should find out how to check if a number is not one of them.
| if (list.some((item) => typeof item === "string" || item === null || item === undefined)) { | ||
| throw new Error("calculateMedian requires an array of numbers"); | ||
| } |
There was a problem hiding this comment.
Could also just check whether item is NOT a value of type number, or even better, check if the item is NOT a finite number.
Even though the test script does not include any test cases involving all possible non-number values ,the comment in the test script does mention:
Arrays containing any non-number value should throw
| function describeMedian(list) { | ||
| const result = calculateMedian(list); | ||
| return `The median is ${result}` | ||
| } |
There was a problem hiding this comment.
The objective of this exercise is to practice using try-catch block to catch and handle the error thrown by a function. calculateMedian() should remain unchanged (per instruction on line 1).
Could you restore the original code of calculateMedian(), and then use a try-catch block in descrubeMedian() accordingly to achieve the objective?
| it("returns for [1, 2, 3, 4]", () => { | ||
| const original = [1, 2, 3, 4]; | ||
| const result = dedupe(original); | ||
| expect(result).not.toBe(original); | ||
| }); |
There was a problem hiding this comment.
In addition to checking whether result is a copy of original, this test should also check if result and original contains identical elements.
Learners, PR Template
Self checklist
Task code
CYF-1071
Changelist
completed exercises of Data Group Sprint-1