Repository navigation
London | 26-ITP-Sept | Fung Nin Lee | Sprint 1 | Sprint-1 exercise #1496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ca8c1ce
1d434da
e2480de
676c4fa
2479c53
0bc4a3a
fe7131b
97e2315
7b99611
601eba7
a0de919
f5247f1
f64fe71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,5 @@ | ||
| function dedupe() {} | ||
| function dedupe(list) { | ||
| return [...new Set(list)]; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,56 @@ | ||
| const dedupe = require("./dedupe.js"); | ||
| /* | ||
| Dedupe Array | ||
|
|
||
| 📖 Dedupe means **deduplicate** | ||
| //Dedupe Array | ||
|
|
||
| In this kata, you will need to deduplicate the elements of an array | ||
| //📖 Dedupe means **deduplicate** | ||
|
|
||
| E.g. dedupe(['a','a','a','b','b','c']) returns ['a','b','c'] | ||
| E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) returns [5, 1, 2, 3, 8] | ||
| E.g. dedupe([1, 2, 1]) returns [1, 2] | ||
| */ | ||
| //In this kata, you will need to deduplicate the elements of an array | ||
|
|
||
| // Acceptance Criteria: | ||
| describe("dedupe", () => { | ||
| //E.g. dedupe(['a','a','a','b','b','c']) returns ['a','b','c'] | ||
| it("returns for ['a','a','a','b','b','c']", () => { | ||
| expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); | ||
| }); | ||
|
|
||
| // Given an empty array | ||
| // When passed to the dedupe function | ||
| // Then it should return an empty array | ||
| test.todo("given an empty array, it returns an empty array"); | ||
| //E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) returns [5, 1, 2, 3, 8] | ||
| it("returns for [5, 1, 1, 2, 3, 2, 5, 8]", () => { | ||
| expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); | ||
| }); | ||
| //E.g. dedupe([1, 2, 1]) returns [1, 2] | ||
| //*/ | ||
| it("returns for [1, 2, 1]", () => { | ||
| expect(dedupe([1, 2, 1])).toEqual([1, 2]); | ||
| }); | ||
|
|
||
| // Given an array with no duplicates | ||
| // When passed to the dedupe function | ||
| // Then it should return a copy of the original array | ||
| // Acceptance Criteria: | ||
|
|
||
| // Given an array of strings or numbers | ||
| // When passed to the dedupe function | ||
| // Then it should return a new array with duplicates removed while preserving the | ||
| // first occurrence of each element from the original array. | ||
| // Given an empty array | ||
| // When passed to the dedupe function | ||
| // Then it should return an empty array | ||
|
|
||
| it("returns for []", () => { | ||
| expect(dedupe([])).toEqual([]); | ||
| }); | ||
|
|
||
| // Given an array with no duplicates | ||
| // When passed to the dedupe function | ||
| // Then it should return a copy of the original array | ||
|
|
||
| it("returns for [1, 2, 3, 4]", () => { | ||
| const original = [1, 2, 3, 4]; | ||
| const result = dedupe(original); | ||
| expect(result).not.toBe(original); | ||
| }); | ||
|
cjyuan marked this conversation as resolved.
Resolved
Comment on lines
+39
to
+43
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In addition to checking whether |
||
|
|
||
| // Given an array of strings or numbers | ||
| // When passed to the dedupe function | ||
| // Then it should return a new array with duplicates removed while preserving the | ||
| // first occurrence of each element from the original array. | ||
| it("returns for ['apple', 'orange', 'banana', 'banana']", () => { | ||
| expect(dedupe(["apple", "orange", "banana", "banana"])).toEqual([ | ||
| "apple", | ||
| "orange", | ||
| "banana", | ||
| ]); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,10 @@ | ||
| function findMax(elements) { | ||
| if (!Array.isArray(elements)) { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
| if (elements.some((element) => typeof element === "string")) { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
| return Math.max(...elements); | ||
| } | ||
|
|
||
| module.exports = findMax; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The assessment seems do not require to handle NaN and array which are neither numbers nor strings.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,24 @@ | ||
| function calculateMean(list) {} | ||
| function calculateMean(list) { | ||
| if (!Array.isArray(list)) { | ||
| throw new Error("calculateMean requires an array of numbers"); | ||
| } | ||
|
|
||
| if (list.length === 0) { | ||
| throw new Error("calculateMean requires a non-empty array"); | ||
| } | ||
|
|
||
| if (list.some((item) => typeof item === "string")) { | ||
| throw new Error("calculateMean requires an array of numbers"); | ||
| } | ||
|
|
||
| let sum = 0; | ||
| for (const item of list) { | ||
| sum += item; | ||
| } | ||
|
|
||
| const mean = sum / list.length; | ||
|
|
||
| return mean; | ||
| } | ||
|
|
||
| module.exports = calculateMean; |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could also just check whether
itemis NOT a value of type number, or even better, check if theitemis 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: