Repository navigation
London | 26-ITP-Sep | Shirin Panahian | Sprint 1 | Complete sprint 1 exercises #1498
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
91cb53c
84b3b38
ee07d79
0bbf183
81b6d82
0aeba8f
0f21d3d
133a259
0a9d415
1842096
8210bdd
bae2058
68fe295
92d12fe
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,14 @@ | ||
| function dedupe() {} | ||
| function dedupe(list) { | ||
| if (list.length === 0) { | ||
| return []; | ||
| } | ||
| const newList = []; | ||
| for (const item of list) { | ||
| if (newList.includes(item)) { | ||
| continue; | ||
| } | ||
|
Comment on lines
+7
to
+9
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. Note: Code on lines 7-11 could also be expressed without using
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. Thanks for the suggestion! I've simplified the code by removing the unnecessary |
||
| newList.push(item); | ||
| } | ||
| return newList; | ||
| } | ||
| module.exports = dedupe; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ Dedupe Array | |
|
|
||
| 📖 Dedupe means **deduplicate** | ||
|
|
||
| In this kata, you will need to deduplicate the elements of an array | ||
| 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] | ||
|
|
@@ -16,13 +16,43 @@ E.g. dedupe([1, 2, 1]) returns [1, 2] | |
| // 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"); | ||
| describe("dedupe", () => { | ||
| it("given an empty array, it returns 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("given an array, it returns an deduplicate array", () => { | ||
| expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); | ||
| expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); | ||
| expect(dedupe([1, 2, 1])).toEqual([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 | ||
| // first occurrence of each element from the original array. | ||
| // Given an array with no duplicates | ||
| // When passed to the dedupe function | ||
| // Then it should return a copy of the original array | ||
| it("Given an array with no duplicates, it returns a copy of the original array", () => { | ||
| const originalArray = ["d", "s", "t", "c"]; | ||
| const result = dedupe(originalArray); | ||
|
|
||
| expect(result).toEqual(originalArray); | ||
| expect(result).not.toBe(originalArray); | ||
|
|
||
| expect(dedupe([5, 4, 3, 2, 1])).toEqual([5, 4, 3, 2, 1]); | ||
| expect(dedupe([7, 8, 9])).toEqual([7, 8, 9]); | ||
| }); | ||
|
Comment on lines
+30
to
+42
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. Your function implementation is correct. However, this test could be improved to better ensure
This test should fail if the function returns the original array (instead of a copy of the original array). The current tests check only if both the original array and the returned array contain identical elements. Could you find out what this additional check is, and then implement it for one of the test cases?
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. Thanks for the suggestion! I've added an additional check using |
||
|
|
||
| // 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("Given an array with duplicates, it removes duplicates while preserving the first occurrence", () => { | ||
| expect(dedupe(["d", "s", "s", "t", "c", "d", "s", "t", "c", "d"])).toEqual([ | ||
| "d", | ||
| "s", | ||
| "t", | ||
| "c", | ||
| ]); | ||
| expect(dedupe([5, 4, 5, 4, 2, 1, 5, 3, 4])).toEqual([5, 4, 2, 1, 3]); | ||
| expect(dedupe([7, 2, 7, 8, 9])).toEqual([7, 2, 8, 9]); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,24 @@ | ||
| function findMax(elements) { | ||
| } | ||
| if (!Array.isArray(elements)) { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
| for (const value of elements) { | ||
| if (!Number.isFinite(value)) { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
| } | ||
|
|
||
| const arrLength = elements.length; | ||
| let maxNum = -Infinity; | ||
| if (arrLength === 0) { | ||
| return [-Infinity]; | ||
| } | ||
|
|
||
| for (const item of elements) { | ||
| if (maxNum < item) { | ||
| maxNum = item; | ||
| } | ||
| } | ||
|
Comment on lines
+17
to
+21
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. Note: This for loop alone could handle an array of any size if you initialise
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. Thanks for the suggestion! I've updated the initial value to |
||
| return maxNum; | ||
| } | ||
| 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. How should the function handle an array containing
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. Arrays containing |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,27 @@ | ||
| 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"); | ||
| } | ||
|
|
||
| for (const element of list) { | ||
| if (!Number.isFinite(element)) { | ||
| throw new Error("calculateMean requires an array of numbers"); | ||
| } | ||
| } | ||
|
|
||
| let sum = 0; | ||
|
|
||
| list.forEach((item) => { | ||
| sum += item; | ||
| }); | ||
|
|
||
| const mean = sum / list.length; | ||
|
|
||
| return mean; | ||
| } | ||
|
|
||
| module.exports = calculateMean; |
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 use
Array.prototype.toSorted().