Repository navigation
Conversation
| if (list.length === 0) { | ||
| return []; | ||
| } | ||
|
|
||
| for (const item of list) { | ||
| if (!elements.includes(item)) { | ||
| elements.push(item); | ||
| } | ||
| } |
There was a problem hiding this comment.
Note: The loop also works for empty array.
| test("given an empty array [], returns Could not calculate a median", () => { | ||
| const list = []; | ||
| const result = describeMedian(list); | ||
| expect(describeMedian(list)).toEqual(result); | ||
| }); |
There was a problem hiding this comment.
The tests in this file could not yet correctly check whether the function could return the expected value.
There was a problem hiding this comment.
Updated the test with duplicated elements in the array.
There was a problem hiding this comment.
Still have the same issue. AI also identified this bug.
| } | ||
| total = total + item; | ||
| } | ||
| return Number(total.toFixed(2)) / list.length; |
There was a problem hiding this comment.
Why limit the mean to only two decimal places?
| // When passed to calculateMean | ||
| // Then it should throw Error("calculateMean requires an array of numbers") | ||
| test("given a non-array, returns, calculateMean requires an array of numbers", () => { | ||
| expect(() => calculateMean("banana", 42, null, {})).toThrow( |
There was a problem hiding this comment.
In this function call, calculateMean("banana", 42, null, {}), only the first argument is passed to the function, the rest of them are ignored.
Suggestion:
- Use separate
expect(), or - Explore Jest's
test.each()
There was a problem hiding this comment.
Test rewritten in a separate expect()
| } | ||
| total += element; | ||
| } | ||
| return Number(total.toFixed(2)); |
There was a problem hiding this comment.
Why limit the result to only two decimal places?
There was a problem hiding this comment.
The result looked more cleaner. I didn't realise how floating point numbers are calculated.
Now expected value is typed as the calculation.
There was a problem hiding this comment.
What should sum([Infinity, 0, -Infinity]) or sum([0, NaN]) return?
The same also applies to calculateMean().
There was a problem hiding this comment.
Test changed to separate expect()
| try { | ||
| return `The median is ${calculateMedian(list)}`; | ||
| } catch (error) { | ||
| return `Could not calculate a median: calculateMedian requires a non-empty array: ${error.message}`; |
There was a problem hiding this comment.
What exact sentence does this return when calculateMedian throws the empty-array error, compared with the example in the acceptance criteria?
To "like" or "dislike" this comment, please follow this link
| expect(() => sum([null])).toThrow( | ||
| new Error("sum requires an array of numbers") | ||
| ); | ||
| expect(() => sum([3])).toThrow(new Error("sum requires an array of numbers")); |
There was a problem hiding this comment.
These assertions expect sum([3]) to throw, so the suite fails for a valid array of numbers. The purported non-array test repeats array inputs instead of checking a non-array.
To "like" or "dislike" this comment, please follow this link
| } | ||
| total = total + item; | ||
| } | ||
| return Number(total.toFixed(2)) / list.length; |
There was a problem hiding this comment.
What happens to the mean of values with more than two decimal places when the total is rounded before dividing?
To "like" or "dislike" this comment, please follow this link
| test("given [1, 2, 3], returns The median is 2", () => { | ||
| const list = [1, 2, 3]; | ||
| const result = describeMedian(list); | ||
| expect(describeMedian(list)).toEqual(result); |
There was a problem hiding this comment.
What incorrect sentence would either of these assertions catch if describeMedian returned the wrong result?
To "like" or "dislike" this comment, please follow this link
| const list = []; | ||
| const result = describeMedian(list); |
There was a problem hiding this comment.
Does this test pass a non-array value to describeMedian?
To "like" or "dislike" this comment, please follow this link
| // When passed to calculateMean | ||
| // Then it should return that number | ||
| test("given [6], returns 6", () => { | ||
| expect(6).toEqual(6); |
There was a problem hiding this comment.
What function call is this assertion checking?
To "like" or "dislike" this comment, please follow this link
| test("given an empty array, it returns an empty array", () => { | ||
| const list = []; | ||
| const result = dedupe(list); | ||
| expect(dedupe(list)).toEqual(result); |
There was a problem hiding this comment.
What output does this test require for an empty list?
To "like" or "dislike" this comment, please follow this link
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
|
|
||
| if (!elements.length === 1) { |
There was a problem hiding this comment.
For a one-element array, what does !elements.length compare with 1?
To "like" or "dislike" this comment, please follow this link
|
The CYF AI review has left comments. It will only review a PR one time. When you have addressed these comments, please request review again and a volunteer will take a look. |
Learners, PR Template
Self checklist
Task code
CYF-1071
Changelist
Fix, refactor and implement code. Write test cases for the program.