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
373f55d
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,13 @@ | ||
| function dedupe() {} | ||
| function dedupe(list) { | ||
| if (list.length === 0) { | ||
| return []; | ||
| } | ||
| const newList = []; | ||
| for (const item of list) { | ||
| if (!newList.includes(item)) { | ||
| newList.push(item); | ||
| } | ||
| } | ||
| return newList; | ||
| } | ||
| module.exports = dedupe; |
| 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; | ||
| } | ||
| } | ||
|
cjyuan marked this conversation as resolved.
Resolved
Comment on lines
+13
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: The code on lines 12, 17-21 also works for empty array. |
||
| return maxNum; | ||
| } | ||
| module.exports = findMax; | ||
|
cjyuan marked this conversation as resolved.
Resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,28 +16,87 @@ 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"); | ||
| describe("findMax", () => { | ||
| it("given an empty array,returns -Infinity", () => { | ||
| expect(findMax([])).toEqual(-Infinity); | ||
| }); | ||
|
Comment on lines
+19
to
+22
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. Did you ran the test script after you made the changes?
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 issue is fixed, and the tests are now passing. |
||
|
|
||
| // Given an array with one number | ||
| // When passed to the max function | ||
| // Then it should return that number | ||
| // Given an array with one number | ||
| // When passed to the max function | ||
| // Then it should return that number | ||
| it("given an array with one number,returns that number", () => { | ||
| expect(findMax([5])).toEqual(5); | ||
| expect(findMax([20])).toEqual(20); | ||
| }); | ||
|
|
||
| // Given an array with both positive and negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest number overall | ||
| // Given an array with both positive and negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest number overall | ||
| it("given an array with both positive and negative numbers, return the largest number overall", () => { | ||
| expect(findMax([5, 8, 3, 0, -1, 2, 16, -8, -16])).toEqual(16); | ||
| expect(findMax([-10, -5, -2, -20, 3, -1])).toEqual(3); | ||
| expect(findMax([-100, 50, -30, 25, 0, 75, -5])).toEqual(75); | ||
| }); | ||
|
|
||
| // Given an array with just negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the closest one to zero | ||
| // Given an array with just negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the closest one to zero | ||
| it("given an array with just negative numbers, return the closest one to zero", () => { | ||
| expect(findMax([-5, -8, -3, -76, -2, -16, -4])).toEqual(-2); | ||
| expect(findMax([-1, -2, -9, -100])).toEqual(-1); | ||
| }); | ||
|
|
||
| // Given an array with decimal numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest decimal number | ||
| // Given an array with decimal numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest decimal number | ||
| it("given an array with decimal numbers, returns the largest decimal number", () => { | ||
| expect(findMax([1.5, 3.7, 2.4, 5.9, 4.2])).toEqual(5.9); | ||
| expect(findMax([0.1, 0.9, 0.5])).toEqual(0.9); | ||
| expect(findMax([-1.5, -3.2, -0.5])).toEqual(-0.5); | ||
| }); | ||
|
|
||
| // 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") | ||
| // 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 when given an array containing a value that isn't a number", () => { | ||
| expect(() => findMax(["banana"])).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") | ||
| expect(() => findMax([10, "hello", 20])).toThrow( | ||
| new Error("findMax requires an array of numbers") | ||
| ); | ||
|
|
||
| expect(() => findMax([10, null, 20])).toThrow( | ||
| new Error("findMax requires an array of numbers") | ||
| ); | ||
|
|
||
| expect(() => findMax([10, NaN, 20])).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 when given something that isn't an array", () => { | ||
| expect(() => findMax("hey")).toThrow( | ||
| new Error("findMax requires an array of numbers") | ||
| ); | ||
|
|
||
| expect(() => findMax(42)).toThrow( | ||
| new Error("findMax requires an array of numbers") | ||
| ); | ||
|
|
||
| expect(() => findMax()).toThrow( | ||
| new Error("findMax requires an array of numbers") | ||
| ); | ||
|
|
||
| expect(() => findMax(null)).toThrow( | ||
| new Error("findMax requires an array of numbers") | ||
| ); | ||
|
|
||
| expect(() => findMax({})).toThrow( | ||
| new Error("findMax requires an array of numbers") | ||
| ); | ||
| }); | ||
| }); | ||
| 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().