Repository navigation
London | 26-ITP-Sep | Sakiya Mayow | Sprint 1| coursework/data groups - #1504
zakiaao-tech wants to merge 12 commits into
Conversation
cjyuan
left a comment
There was a problem hiding this comment.
Code looks good. Good job.
There was a problem hiding this comment.
How should the function handle an array containing NaN?
There was a problem hiding this comment.
What should calculateMean([Infinity, 0, -Infinity]) or calculateMean([0, NaN]) return?
The same also applies to sum().
| test("given negative and decimal numbers, returns the correct mean", () => { | ||
| expect(calculateMean([-2, 3.5, 4.5])).toBe(2); | ||
| }); | ||
|
|
There was a problem hiding this comment.
Decimal numbers in most programming languages (including JS) are internally represented in "floating point number" format. Floating point arithmetic is not exact. For example, the result of 46.5678 - 46 === 0.5678 is false because 46.5678 - 46 only yield a value that is very close to 0.5678. Even changing the order in which the program add/subtract numbers can yield different values.
So the following could happen
expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.805 ); // This fail
expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.8049999999999997 ); // This pass
expect( 0.005 + 0.6 + 1.2 ).toEqual( 1.8049999999999997 ); // This fail
console.log(1.2 + 0.6 + 0.005 == 1.805); // false
console.log(1.2 + 0.6 + 0.005 == 0.005 + 0.6 + 1.2); // falseSuggestion: Look up
- Checking equality in floating point arithmetic in JavaScript
- Checking equality in floating point arithmetic with Jest
| // Refactor the implementation of includes to use a for...of loop | ||
|
|
||
| function includes(list, target) { | ||
| for (let index = 0; index < list.length; index++) { | ||
| const element = list[index]; | ||
| if (element === target) { | ||
| return true; | ||
| } | ||
| } |
There was a problem hiding this comment.
Note: The objective of this exercise is to change the normal for-loop to for-of-loop (per the Instruction on line 1), as oppose to totally rewrite the whole function.
Learners, PR Template
Self checklist
Task code
CYF-1071
Changelist
This PR includes Fix, Implement and Refactor exercises. It includes fixing existing functions, implementing new functions and refactoring code to make it simpler and cleaner while ensuring the tests pass.