Repository navigation
Manchester |26-ITP-Sep | Precious Moses | Sprint 1 | Data Groups - #1479
moses77-boop wants to merge 60 commits into
Conversation
…n array of numbers
| test("given a value that is not an array, throws an error", () => { | ||
| expect(() => calculateMean("banana")).toThrow("calculateMean requires an array of numbers"); | ||
| }); | ||
|
|
||
| test("given a value that is not an array, throws an error", () => { | ||
| expect(() => calculateMean(42)).toThrow("calculateMean requires an array of numbers"); | ||
| }); | ||
|
|
||
| test("given a value that is not an array, throws an error", () => { | ||
| expect(() => calculateMean(null)).toThrow("calculateMean requires an array of numbers"); | ||
| }); | ||
|
|
||
| test("given a value that is not an array, throws an error", () => { | ||
| expect(() => calculateMean({})).toThrow("calculateMean requires an array of numbers"); | ||
| }); |
There was a problem hiding this comment.
Could consider grouping these test cases into one category; they have the same test description.
| @@ -0,0 +1,19 @@ | |||
| const fs = require("fs"); | |||
| const input = fs.readFileSync("input.txt", "utf8"); | |||
| const changes = input.trim().split("\n").map(Number); | |||
There was a problem hiding this comment.
Could you explain why you choose to apply .trim() to the input before applying split()?
There was a problem hiding this comment.
.trim() removes the trailing newline so I don't get an empty last entry.
There was a problem hiding this comment.
Note: For large file, calling trim() just to remove the trailing newline is not efficient. Do you know why?
No change needed.
| while (true) { | ||
| for (const change of changes) { | ||
| frequency += change; | ||
| if (seen.has(frequency)) { | ||
| console.log(frequency); | ||
| process.exit(0); | ||
| } | ||
| seen.add(frequency); | ||
| } | ||
| } |
There was a problem hiding this comment.
Is this implementation for the second part of the problem? I think the first part is just adding all the frequencies.
There was a problem hiding this comment.
I actually wrote the part 1 first and refactored the same code for part 2.
Do I need to write both parts of the code separately?
There was a problem hiding this comment.
No need. Knowing how to read lines of data from a file is more important in this exercise.
| let total = 0; | ||
|
|
||
| for (const element of elements) { | ||
| if (typeof element !== "number") { |
There was a problem hiding this comment.
Note: NaN and positive/negative Infinity are also value of type numbers. If it is up to you, how would you design the function when the given array contain such values?
There was a problem hiding this comment.
I have refactored the code.
| if (list.length === 0) { | ||
| throw new Error("calculateMean requires a non-empty array"); | ||
| } | ||
|
|
There was a problem hiding this comment.
Spacing a bit inconsistent after if.
|
Changes look good. Well done. |
|
Note: You should also change the label to "Needs review" after you have made all the changes. |
Self checklist
Task code
CYF-1071
Changelist
In this PR, I refactored codes, implemented functions and test codes to complete the coursework.