Uh oh!
There was an error while loading. Please reload this page.
London | 26-ITP-May | Damilola Odumosu | Sprint 1 | Coursework - #1296
London | 26-ITP-May | Damilola Odumosu | Sprint 1 | Coursework#1296d-odumosu wants to merge 10 commits into
Conversation
… of loop. all tests pass.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| if (elementLists.length === 0) { | ||
| return undefined; | ||
| } |
There was a problem hiding this comment.
When a function is expected to return a number, it is better to design the function to always returned a value of type number for consistency.
Technically, an empty array also an array containing no numbers.
There was a problem hiding this comment.
What's your decision process to select 0 as the return value when an array does not contain any numbers?
There was a problem hiding this comment.
since the function is supposed to return a number i thought it best that if the array is empty /contain to numbers it should return 0
There was a problem hiding this comment.
An empty array is also an array containing no numbers. So the function could also return the same value in both cases. However, if the function needs to distinguish an empty array from an array containing no numbers, then your choice would make sense.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| let sum = 0; | ||
| for (let i = 0; i < elementsList.length; i++) { | ||
| sum += elementsList[i]; | ||
| } | ||
| return sum; |
There was a problem hiding this comment.
Could this code work for an array of any length?
There was a problem hiding this comment.
yes, so i think i dont have to check and return 0 if its empty because the loop will do that anyway, thank you, fixed
There was a problem hiding this comment.
You also don't have to check if the array length is 1.
Uh oh!
There was an error while loading. Please reload this page.
| if (elements.length === 0) { | ||
| return []; | ||
| } | ||
| return elements.filter((item, index) => elements.indexOf(item) === index); | ||
| return [...new Set(elements)]; |
There was a problem hiding this comment.
Using Set() also works. The point I wanted to make was, checking elements.length === 0 is optional.
| testCaseNoDuplicates.forEach(({ input, expected }) => { | ||
| test("given an array with no duplicates, return a copy of the array", () => { | ||
| expect(dedupe(input)).toEqual(expected); | ||
| expect(result).not.toBe(input); |
There was a problem hiding this comment.
result saves the function call of dedeupe
There was a problem hiding this comment.
Is result declared and assigned any value?
There was a problem hiding this comment.
yes i see the problem now, i have fixed it
| if (elementLists.length === 0) { | ||
| return undefined; | ||
| } |
There was a problem hiding this comment.
What's your decision process to select 0 as the return value when an array does not contain any numbers?
| const elementsList = elements.filter((element) => { | ||
| return typeof element === "number"; | ||
| return ( | ||
| typeof element === "number" && | ||
| !Number.isNaN(element) && | ||
| Number.isFinite(element) | ||
| ); | ||
| }); |
There was a problem hiding this comment.
Some of the checks are redundant. Could you use AI to research how you could simplify the code on line 2-8?
There was a problem hiding this comment.
yes, i have done some research and see that Number.isfinite sufficess
| let sum = 0; | ||
| for (let i = 0; i < elementsList.length; i++) { | ||
| sum += elementsList[i]; | ||
| } | ||
| return sum; |
There was a problem hiding this comment.
You also don't have to check if the array length is 1.
cjyuan
commented
Aug 21, 2026
All good. |
Self checklist
Changelist
Sprint 1 Tasks
fix :
implement:
dedupe
max
sum
refactor: