Repository navigation
London | 26-ITP-Sept | Hugh Mills | Sprint 1 | Sprint 1 Exercises #1487
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
69c0396
6c18a29
a992437
328458f
fcd612f
28f23bb
e3a93cc
3e41283
9590c71
8fc182a
1f84354
a8d6728
473b428
fed43f7
e6a2a7f
bf24000
b6fc9e2
349d421
0eb9caf
d48bbb7
5090836
6f6333d
3cfe575
7ddb8cb
92cea6a
59cd442
904a8ae
9977484
c4c5fbc
9d8c1e2
31b226e
cfe837f
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(array) { | ||
| return [...new Set(array)]; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,58 @@ | ||
| const dedupe = require("./dedupe.js"); | ||
| /* | ||
| Dedupe Array | ||
|
|
||
| 📖 Dedupe means **deduplicate** | ||
|
|
||
| */ | ||
| describe("deduplicate", () => { | ||
| /* | ||
| In this kata, you will need to deduplicate the elements of an array | ||
|
|
||
| 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] | ||
| */ | ||
|
|
||
| // Acceptance Criteria: | ||
|
|
||
| // 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"); | ||
| // Acceptance Criteria: | ||
|
|
||
| // Given an empty array | ||
| // When passed to the dedupe function | ||
| // Then it should return an empty array | ||
| it(`returns the empty array when given an empty array`, () => { | ||
| 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("doesn't modify the input array [3, 1, 1, 2]", () => { | ||
| const array = [3, 1, 1, 2]; | ||
| dedupe(array); | ||
| expect(array).toEqual([3, 1, 1, 2]); | ||
| }); | ||
|
|
||
| // 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 | ||
| // Then it should return a new array with duplicates removed while preserving the | ||
| // first occurrence of each element from the original array. | ||
| it(`returns the deduplicate for string letters`, () => { | ||
| expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); | ||
| }); | ||
|
|
||
| it(`returns the deduplicate for numbers`, () => { | ||
| expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); | ||
| }); | ||
|
|
||
| it(`returns the deduplicate while not numerical in order`, () => { | ||
| expect(dedupe([1, 2, 1])).toEqual([1, 2]); | ||
| }); | ||
|
|
||
| it(`returns the deduplicate for string and numbers `, () => { | ||
| expect(dedupe([5, 1, 1, 2, 3, "a", "a", "a", "c"])).toEqual([ | ||
| 5, | ||
| 1, | ||
| 2, | ||
| 3, | ||
| "a", | ||
| "c", | ||
| ]); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,19 @@ | ||
| function findMax(elements) { | ||
| if (elements.length === 0) { | ||
| return "Infinity"; | ||
| } | ||
| if (!Array.isArray(elements)) { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
|
|
||
| const copyElements = Array.from(elements); | ||
| for (const item of copyElements) { | ||
| if (typeof item !== "number") { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
| } | ||
| let sortElements = copyElements.sort((a, b) => a - b); | ||
| return sortElements.at(-1); | ||
| } | ||
|
|
||
| module.exports = findMax; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,28 +16,52 @@ const findMax = require("./max.js"); | |
| // When passed to the max function | ||
| // Then it should return -Infinity | ||
| // Delete this test.todo and replace it with a test. | ||
| test.todo("given an empty array, returns -Infinity"); | ||
| it("returns the Infinity for []", () => { | ||
| expect(findMax([])).toEqual("Infinity"); | ||
| }); | ||
|
Comment on lines
+19
to
+21
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. It seems not quite accurate to me. Please consider reading line 17 very carefully, and fix the typo in this test case. Thank you. |
||
|
|
||
| // Given an array with one number | ||
| // When passed to the max function | ||
| // Then it should return that number | ||
| it("returns the median for [40]", () => { | ||
| expect(findMax([40])).toEqual(40); | ||
| }); | ||
|
|
||
| // Given an array with both positive and negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest number overall | ||
| it("returns the median for [20, 30, 10, 40]", () => { | ||
| expect(findMax([20, 30, 10, 40])).toEqual(40); | ||
| }); | ||
|
|
||
| // Given an array with just negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the closest one to zero | ||
| it("returns the median for [-40, -20, -30, -70]", () => { | ||
| expect(findMax([-40, -20, -30, -70])).toEqual(-20); | ||
| }); | ||
|
|
||
| // Given an array with decimal numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest decimal number | ||
| it("returns the median for [-40.4, -20.7, -30.3, -70.6]", () => { | ||
| expect(findMax([-40.4, -20.7, -30.3, -70.6])).toEqual(-20.7); | ||
| }); | ||
|
|
||
| // Given an array containing a value that isn't a number | ||
| // When passed to the max function | ||
| // Then it should throw Error("findMax requires an array of numbers") | ||
| it("Throws error 'findMax requires an array of numbers' when containing a value that isn't a number", () => { | ||
| expect(() => findMax([10, 40, "hello", 30])).toThrow( | ||
| new Error("findMax requires an array of numbers") | ||
| ); | ||
| }); | ||
|
|
||
| // Given something that isn't an array at all, such as "hey", 42 or no argument | ||
| // When passed to the max function | ||
| // Then it should throw Error("findMax requires an array of numbers") | ||
| it("Throws error 'findMax requires an array of numbers' Given something that isn't an array at all", () => { | ||
| expect(() => findMax(42)).toThrow( | ||
| new Error("findMax requires an array of numbers") | ||
| ); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,18 @@ | ||
| function calculateMean(list) {} | ||
| function calculateMean(list) { | ||
| if (list.length === 0) { | ||
| throw new Error("calculateMean requires a non-empty array"); | ||
| } | ||
|
Comment on lines
+2
to
+4
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.
|
||
|
|
||
| if (!Array.isArray(list)) { | ||
| throw new Error("calculateMean requires an array of numbers"); | ||
| } | ||
|
|
||
| const copyList = Array.from(list); | ||
| let total = 0; | ||
| for (let i = 0; i < copyList.length; i++) { | ||
| total += copyList[i]; | ||
| } | ||
| return total / copyList.length; | ||
| } | ||
|
|
||
| module.exports = calculateMean; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,26 @@ | ||
| function sum(elements) { | ||
| if (elements.length === 0) { | ||
| return 0; | ||
| } | ||
|
Comment on lines
+2
to
+4
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.
|
||
| if (!Array.isArray(elements)) { | ||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
| for (const item of elements) { | ||
| if (typeof item !== "number") { | ||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
|
|
||
| const copyElements = Array.from(elements); | ||
|
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. For your future reference, copy an array is not needed as your algorithm just reads but does not modify any elements in it. |
||
| let total = 0; | ||
| for (let i = 0; i < copyElements.length; i++) { | ||
| total += copyElements[i]; | ||
| } | ||
| if (Number.isInteger(total)) { | ||
| return total; | ||
| } else { | ||
| return Number(total.toFixed(2)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| module.exports = sum; | ||
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.
elementsdoes not necessary have the.lengthproperty/attribute. A better approach can be making sure it is an array first. Please fix it. Thank you.