-
-
Notifications
You must be signed in to change notification settings - Fork 286
Manchester | 26-ITP-Jan | Mehroz Munir | Sprint 1 | Coursework exercises #1018
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
Changes from 2 commits
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,7 @@ | ||
| function dedupe() {} | ||
| function dedupe(arr) { | ||
| if (!Array.isArray(arr)) throw new Error(arr + " is not an array"); | ||
| else if (arr.length === 0) return arr; | ||
| else return Array.from(new Set(arr)); | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,18 +10,49 @@ E.g. dedupe(['a','a','a','b','b','c']) target output: ['a','b','c'] | |
| E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) target output: [5, 1, 2, 3, 8] | ||
| E.g. dedupe([1, 2, 1]) target output: [1, 2] | ||
| */ | ||
| describe("dedupe", () => { | ||
| // Given an empty array | ||
| // When passed to the dedupe function | ||
| // Then it should return an empty array | ||
| it("given an empty array it should return an empty array", () => { | ||
| const array = []; | ||
| dedupeArray = dedupe(array); | ||
|
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. I think this one slipped through the crack. Let's add a keyword |
||
| expect(dedupeArray).toEqual([]); | ||
| }); | ||
|
|
||
| // Acceptance Criteria: | ||
| // Given an array with no duplicates | ||
| // When passed to the dedupe function | ||
| // Then it should return a copy of the original array | ||
| [["a", "b", "c"], ["A", 1, "j", "?"], ["c"]].forEach((val) => | ||
| it(`returns copy of the original array if there are no duplicates in [${val}]`, () => | ||
| expect(dedupe(val)).toEqual(val)) | ||
| ); | ||
| // Given an array with strings or numbers | ||
| // When passed to the dedupe function | ||
| // Then it should remove the duplicate values, preserving the first occurrence of each element | ||
| [ | ||
| { input: [1, 2, 1, 3, 1, 2, 10, 5, 0, 10], expected: [1, 2, 3, 10, 5, 0] }, | ||
| { input: [1, 2, 1, 4], expected: [1, 2, 4] }, | ||
| { input: [1, 1, 1, 1, 1], expected: [1] }, | ||
| { | ||
| input: ["banana", "apple", "apple", "banana", "apple", "banana"], | ||
| expected: ["banana", "apple"], | ||
| }, | ||
| { | ||
| input: [" ", "empty", "", " ", "", "empty"], | ||
| expected: [" ", "empty", ""], | ||
| }, | ||
| { input: ["2", "2", "3", "1"], expected: ["2", "3", "1"] }, | ||
| ].forEach(({ input, expected }) => | ||
| it(`returns a copy of array removing the duplicates from [${input}]`, () => | ||
| expect(dedupe(input)).toEqual(expected)) | ||
| ); | ||
|
|
||
| // 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"); | ||
|
|
||
| // Given an array with no duplicates | ||
| // When passed to the dedupe function | ||
| // Then it should return a copy of the original array | ||
|
|
||
| // Given an array with strings or numbers | ||
| // When passed to the dedupe function | ||
| // Then it should remove the duplicate values, preserving the first occurence of each element | ||
| // Given an input value that is not array could be null or undefined or just a number or string | ||
| // When passed to the dedupe function | ||
| // Then it should thrown an error | ||
| [null, 930, "just a string", undefined, {}].forEach((val) => | ||
| it("throw an error if the input is not an array", () => | ||
|
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. This is causing the test name to not be unique. Perhaps there is a better way to do this? Do the iteration inside the test? Or something better? |
||
| expect(() => dedupe(val)).toThrow(val + " is not an array")) | ||
| ); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
| function findMax(elements) { | ||
| function findMax(array) { | ||
| if (!Array.isArray(array)) throw new Error(array + " is not an array"); | ||
| let numbersArray = array.filter((value) => typeof value === "number"); | ||
|
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 variables that are not later reassigned, you can use
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 I will do that from now on. Made this change in the code as well. |
||
| return Math.max(...numbersArray); | ||
| } | ||
|
|
||
| module.exports = findMax; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,12 @@ | ||
| function sum(elements) { | ||
| function sum(array) { | ||
| if (!Array.isArray(array)) throw new Error(array + " is not an array"); | ||
| let numbersArray = array.filter((value) => typeof value === "number"); | ||
| let initialValue = 0; | ||
|
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's good that you're getting in the habit of clean code, but for most things |
||
| let sum = numbersArray.reduce( | ||
| (accumulator, currentValue) => accumulator + currentValue, | ||
| initialValue | ||
| ); | ||
| return sum; | ||
| } | ||
|
|
||
| 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.
Nice, organised return statement. If the above code falls through, this will catch it.
But question ❓ Can a median be
null? Does it have to be a number? Or isnullfine?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.
Yes, it can't be null. I have added a check at the end for that. Let me know if it seems right. Thanks