-
-
Notifications
You must be signed in to change notification settings - Fork 220
Birmingham | 25-ITP-Sep | Joy Opachavalit | Sprint 1 | Sprint-1 #800
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 all commits
7f13c87
7fca1ad
097ab82
6366dff
cceb024
3a1421f
eabe404
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,15 @@ | ||
| function dedupe() {} | ||
|
|
||
| function dedupe(arr) { | ||
| if (!Array.isArray(arr)) return []; | ||
| const seen = new Set(); | ||
| const result = []; | ||
| for (const item of arr) { | ||
| if (!seen.has(item)) { | ||
| seen.add(item); | ||
| result.push(item); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,15 +13,35 @@ E.g. dedupe([1, 2, 1]) target output: [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"); | ||
| test("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 | ||
| test("given an array with no duplicates, returns a copy of the original array", () => { | ||
| expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); | ||
| expect(dedupe(["a", "b", "c"])).toEqual(["a", "b", "c"]); | ||
|
|
||
| // Reference copy check | ||
| const original = [7, 8, 9]; | ||
| const result = dedupe(original); | ||
| expect(result).toEqual(original); // same values | ||
| expect(result).not.toBe(original); // different reference | ||
|
Comment on lines
+34
to
+35
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. Good job. |
||
|
|
||
| }); | ||
|
|
||
| // 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 | ||
| test("given an array with duplicates, removes duplicates and preserves first occurrence", () => { | ||
| 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]); | ||
| expect(dedupe(["x", "y", "x", "z", "y", "x"])).toEqual(["x", "y", "z"]); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,22 @@ | ||
| function findMax(elements) { | ||
| } | ||
| // Filter only numeric values (numbers, not NaN, not null, not undefined) | ||
| const numbers = elements.filter(function(item) { | ||
| return typeof item === 'number' && !isNaN(item); | ||
| }); | ||
|
|
||
| // Treat no numbers the same as empty input | ||
| if (numbers.length === 0) { | ||
| return -Infinity; | ||
| } | ||
|
|
||
| // Find the maximum number | ||
| let max = numbers[0]; | ||
| for (let i = 1; i < numbers.length; i++) { | ||
| if (numbers[i] > max) { | ||
| max = numbers[i]; | ||
| } | ||
| } | ||
| return max; | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,29 +15,59 @@ const findMax = require("./max.js"); | |
| // Given an empty array | ||
| // 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"); | ||
| test("given an empty array, returns -Infinity", () => { | ||
| expect(findMax([])).toBe(-Infinity); | ||
| }); | ||
|
|
||
| // Given an array with one number | ||
| // When passed to the max function | ||
| // Then it should return that number | ||
| test("given an array with one number, returns that number", () => { | ||
| expect(findMax([5])).toBe(5); | ||
| expect(findMax([42])).toBe(42); | ||
| expect(findMax([-10])).toBe(-10); | ||
| }); | ||
|
|
||
| // Given an array with both positive and negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest number overall | ||
| test("given an array with positive and negative numbers, returns the largest", () => { | ||
| expect(findMax([30, 50, 10, 40])).toBe(50); | ||
| expect(findMax([10, -5, 20, -15, 8])).toBe(20); | ||
| expect(findMax([-3, 5, -10, 2])).toBe(5); | ||
| }); | ||
|
|
||
| // Given an array with just negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the closest one to zero | ||
| test("given an array with just negative numbers, returns the closest to zero", () => { | ||
| expect(findMax([-5, -10, -3, -20])).toBe(-3); | ||
| expect(findMax([-100, -50, -1])).toBe(-1); | ||
| }); | ||
|
|
||
| // Given an array with decimal numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest decimal number | ||
| test("given an array with decimal numbers, returns the largest decimal", () => { | ||
| expect(findMax([3.5, 2.1, 4.8, 1.2])).toBe(4.8); | ||
| expect(findMax([0.1, 0.9, 0.5])).toBe(0.9); | ||
| expect(findMax([10.5, 10.7, 10.3])).toBe(10.7); | ||
| }); | ||
|
|
||
| // Given an array with non-number values | ||
| // When passed to the max function | ||
| // Then it should return the max and ignore non-numeric values | ||
| test("given an array with non-number values, ignores them and returns max", () => { | ||
| expect(findMax(["hey", 10, "hi", 60, 10])).toBe(60); | ||
| expect(findMax([5, "hello", 15, null, 10, undefined])).toBe(15); | ||
| expect(findMax([1, "test", 2, NaN, 3])).toBe(3); | ||
|
Comment on lines
+61
to
+63
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. Could also include strings that can usually be safely converted to numbers (e.g., "100", "3e2") to ensure the function can properly ignore values that are not numbers. |
||
| }); | ||
|
|
||
| // Given an array with only non-number values | ||
| // When passed to the max function | ||
| // Then it should return the least surprising value given how it behaves for all other inputs | ||
| test("given an array with only non-number values, returns null", () => { | ||
| expect(findMax(["hello", "world"])).toBe(null); | ||
| expect(findMax([null, undefined, NaN])).toBe(null); | ||
| expect(findMax(["a", "b", "c"])).toBe(null); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,12 @@ | ||
| function sum(elements) { | ||
| function sum(list) { | ||
| if (!Array.isArray(list)) return 0; | ||
| let total = 0; | ||
| for (const item of list) { | ||
| if (typeof item === 'number' && !isNaN(item)) { | ||
| total += item; | ||
| } | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| 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.
Is it necessary to clone
numbers?