Repository navigation
Manchester | 26-ITP-Sep | Mathew Shaibu | Sprint 1 | Module data groups sprint-1 #1480
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
Open
MattCarolus10
wants to merge
72
commits into
CodeYourFuture:main
Choose a base branch
from
MattCarolus10:Module-Data-Groups-sprint-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,013
−890
Open
Changes from all commits
Commits
Show all changes
72 commits
Select commit
Hold shift + click to select a range
13fca37
array of numbers fixed
MattCarolus10 010e59e
non empty array fixed
MattCarolus10 a885859
non list of array fixed
MattCarolus10 cd95950
all test function fixed
MattCarolus10 3cc7cf4
empty array return infinity
MattCarolus10 79f3284
empty array return infinity
MattCarolus10 df508f3
test given an array of one number
MattCarolus10 523c22c
test largest positive and negative numbers
MattCarolus10 92b41f3
fix: correct syntax in test for largest of positive and negative numbers
MattCarolus10 55446f7
closest to zero for all negative numbers
MattCarolus10 59800af
closest to zero for all negative numbers
MattCarolus10 01356bc
test for largest decimal numbers
MattCarolus10 8223706
test containing non-numbers
MattCarolus10 40ab195
test for not given an array
MattCarolus10 f095dbf
fix not array test
MattCarolus10 3660134
length equals zero test fix
MattCarolus10 289a04c
refactor error handling and formatting in findMax function
MattCarolus10 d70f176
fix: correct case of -Infinity in empty array test
MattCarolus10 2163cf1
fix: correct case of -Infinity and fix type checks in findMax function
MattCarolus10 0eb22a9
refactor: remove unnecessary comments and clean up max.test.js
MattCarolus10 794f3b1
refactor: improve formatting and indentation in max.test.js
MattCarolus10 8dc93ce
test: implement test case for empty array in sum function
MattCarolus10 87581d4
test: fix test case for returning a number from an array
MattCarolus10 6fb4efd
test: fix test case for adding negative numbers in sum function
MattCarolus10 cf80450
test: add test cases for decimal numbers and non-number inputs in sum…
MattCarolus10 04f33f8
test: add case to throw error when input is not an array in sum function
MattCarolus10 14a6f66
fix: correct variable name in array check for sum function
MattCarolus10 20c90a2
fix: correct variable name in array check for sum function
MattCarolus10 021392c
fix: correct variable name and implement summation logic in sum function
MattCarolus10 bca8798
fix: correct formatting in sum function for consistency
MattCarolus10 9c02fb8
fix: correct syntax errors in sum function tests
MattCarolus10 718a3b1
fix: correct function calls in sum tests for accuracy
MattCarolus10 75a3863
fix: rename parameter in sum function for clarity
MattCarolus10 c555e30
fix: add missing import statement for findMax in max tests
MattCarolus10 0a8ffc9
fix: remove unnecessary comments and documentation in sum tests for c…
MattCarolus10 7d531a4
fix: implement test for empty array case in dedupe function
MattCarolus10 c9a0b25
fix: add test for dedupe function to verify handling of duplicate ele…
MattCarolus10 b21e531
fix: add test to verify preservation of first occurrence order in ded…
MattCarolus10 586f2b0
feat: implement dedupe function to remove duplicates from an array
MattCarolus10 9fe1206
fix: remove unnecessary comments and documentation in dedupe tests fo…
MattCarolus10 9d715a4
test: implement test for calculateMean function with sample input
MattCarolus10 320a5fa
test: update calculateMean test case to return correct mean for multi…
MattCarolus10 d3d9156
test: add case to verify calculateMean returns correct value for sing…
MattCarolus10 7f9881d
test: add case to verify calculateMean handles negative and decimal n…
MattCarolus10 9ea6b81
test: add case to verify calculateMean throws error for empty array
MattCarolus10 58bc16c
test: add cases to verify calculateMean throws errors for invalid inputs
MattCarolus10 44859cb
test: remove unnecessary comments and documentation from mean test file
MattCarolus10 c15204d
test: implement input validation for calculateMean function
MattCarolus10 e94100e
test: add validation for non-empty array in calculateMean function
MattCarolus10 e912e06
fix: ensure calculateMean validates input as an array of numbers
MattCarolus10 ab6b5a4
fix: correct error message and test description for non-number array …
MattCarolus10 88a8db1
fix: correct typo in test for handling negative and decimal numbers
MattCarolus10 da1d6fd
test: implement test for describeMedian function with valid input
MattCarolus10 edf099c
test: fix expected output for describeMedian function with valid input
MattCarolus10 2d2960d
test: add missing test for non-array input in describeMedian function
MattCarolus10 34a8196
fix: correct error messages in tests for describeMedian function
MattCarolus10 c60bbdf
feat: implement describeMedian function to calculate and return the m…
MattCarolus10 783f7e6
fix: correct median test output and remove unnecessary comments
MattCarolus10 611dd7f
refactor: simplify includes function using for...of loop
MattCarolus10 9a34fb9
fix: update input values in aoc-2018-day1 dataset for accuracy
MattCarolus10 7b077cf
feat: implement frequency calculation from input file
MattCarolus10 ec97a6d
feat: update frequency calculation to handle input parsing and duplic…
MattCarolus10 8a56804
remove prep from sprint branch
MattCarolus10 566ad62
test: add case to verify dedupe function returns a copy when no dupli…
MattCarolus10 1f1e506
test: ensure dedupe function returns a copy of the original array whe…
MattCarolus10 fe5dbde
test: ensure dedupe function returns a new copy when no duplicates exist
MattCarolus10 14b9d1e
test: add case to verify dedupe function removes duplicates with mixe…
MattCarolus10 d338579
test: add case to verify dedupe function removes duplicates across mu…
MattCarolus10 eb38ef7
refactor: simplify type check in sum function to use Number.isFinite
MattCarolus10 6d23a17
refactor: remove duplicate code for reading and processing input changes
MattCarolus10 399fc73
refactor: improve code formatting and consistency in solution.js
MattCarolus10 33a1377
refactor: remove redundant code and streamline input processing in so…
MattCarolus10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,20 @@ | ||
| // Fix this implementation | ||
| // Start by running the tests for this function | ||
| // If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory | ||
|
|
||
| // Hint: Please consider scenarios when 'list' isn't an array, is empty, | ||
| // or contains values that aren't numbers (the function is expected to throw - see the tests). | ||
|
|
||
| function calculateMedian(list) { | ||
| const middleIndex = Math.floor(list.length / 2); | ||
| const median = list.splice(middleIndex, 1)[0]; | ||
| return median; | ||
| if (!Array.isArray(list)) { | ||
| throw new Error("calculateMedian requires an array of numbers"); | ||
| } | ||
| if (list.length === 0) { | ||
| throw new Error("calculateMedian requires a non-empty array"); | ||
| } | ||
| if (!list.every((item) => typeof item === "number")) { | ||
| throw new Error("calculateMedian requires an array of numbers"); | ||
| } | ||
| const sorted = [...list].sort((a, b) => a - b); | ||
| const middleIndex = Math.floor(sorted.length / 2); | ||
| if (sorted.length % 2 === 0) { | ||
| return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2; | ||
| } | ||
|
|
||
| return sorted[middleIndex]; | ||
| } | ||
|
|
||
| module.exports = calculateMedian; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,11 @@ | ||
| function dedupe() {} | ||
| function dedupe(list) { | ||
| const result = []; | ||
| for (const item of list) { | ||
| if (!result.includes(item)) { | ||
| result.push(item); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,30 @@ | ||
| const dedupe = require("./dedupe.js"); | ||
| /* | ||
| Dedupe Array | ||
|
|
||
| 📖 Dedupe means **deduplicate** | ||
| test("given an empty array, it returns an empty array", () => { | ||
| expect(dedupe([])).toEqual([]); | ||
| }); | ||
|
|
||
| In this kata, you will need to deduplicate the elements of an array | ||
| test("returns a a copy with no duplicates", () => { | ||
| expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); | ||
| }); | ||
|
|
||
| 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] | ||
| */ | ||
| test("keeps the first occurrence order", () => { | ||
| expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); | ||
| }); | ||
|
|
||
| // Acceptance Criteria: | ||
| test("returns a copy when there are no duplicates", () => { | ||
| const original = [1, 2, 3, 4]; | ||
| const result = dedupe(original); | ||
| expect(result).toEqual(original); | ||
| expect(result).not.toBe(original); | ||
| }); | ||
|
|
||
| // 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("removes duplicates with mixed types", () => { | ||
| expect(dedupe([1, "1", 2, "2", 1, "2"])).toEqual([1, "1", 2, "2"]); | ||
| }); | ||
|
|
||
| // 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 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. | ||
| test("removes duplicates across multiple cases", () => { | ||
| expect(dedupe([2, 1, 2, 3, 1])).toEqual([2, 1, 3]); | ||
| expect(dedupe(["a", "b", "a"])).toEqual(["a", "b"]); | ||
| expect(dedupe([true, true, false])).toEqual([true, false]); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,18 @@ | ||
| /* Describe the median, or explain why there isn't one | ||
|
|
||
| calculateMedian is given to you at the top of describe-median.js. It throws | ||
| when it can't calculate a median. Don't change it. | ||
|
|
||
| Implement describeMedian, which calls calculateMedian and returns a sentence | ||
| instead of crashing. | ||
|
|
||
| E.g. describeMedian([1, 2, 3]), target output: "The median is 2" | ||
| E.g. describeMedian([]), target output: "Could not calculate a median: calculateMedian requires a non-empty array" | ||
|
|
||
| You'll need a try/catch block for this. The catch block must use the error's | ||
| message, so the caller can see what went wrong. | ||
| */ | ||
|
|
||
| const describeMedian = require("./describe-median.js"); | ||
|
|
||
| // Acceptance criteria: | ||
|
|
||
| // Given an array of numbers | ||
| // When passed to describeMedian | ||
| // Then it should return "The median is " followed by the median | ||
| // Delete this test.todo and replace it with a test. | ||
| test.todo('given [1, 2, 3], returns "The median is 2"'); | ||
| test('given [1, 2, 3], returns "The median is 2"', () => { | ||
| expect(describeMedian([1, 2, 3])).toBe("The median is 2"); | ||
| }); | ||
|
|
||
| // Given an empty array | ||
| // When passed to describeMedian | ||
| // Then it should return "Could not calculate a median: calculateMedian requires a non-empty array" | ||
| test("returns a sentence for an empty array", () => { | ||
| expect(describeMedian([])).toBe( | ||
| "Could not calculate a median: calculateMedian requires a non-empty array" | ||
| ); | ||
| }); | ||
|
|
||
| // Given something that isn't an array of numbers, e.g. "banana" | ||
| // When passed to describeMedian | ||
| // Then it should return "Could not calculate a median: calculateMedian requires an array of numbers" | ||
| test("return a sentence for a non-array", () => { | ||
| expect(describeMedian("banana")).toBe( | ||
| "Could not calculate a median: calculateMedian requires an array of numbers" | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,23 @@ | ||
| function findMax(elements) { | ||
| if (!Array.isArray(elements)) { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
|
|
||
| if (elements.length === 0) { | ||
| return -Infinity; | ||
| } | ||
|
|
||
| if (!elements.every((item) => typeof item === "number")) { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
|
|
||
| let max = elements[0]; | ||
| for (const item of elements) { | ||
| if (item > max) { | ||
| max = item; | ||
| } | ||
| } | ||
| return max; | ||
| } | ||
|
|
||
| module.exports = findMax; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,33 @@ | ||
| /* Find the maximum element of an array of numbers | ||
|
|
||
| In this kata, you will need to implement a function that find the largest numerical element of an array. | ||
|
|
||
| E.g. max([30, 50, 10, 40]), target output: 50 | ||
| E.g. max(['hey', 10, 'hi', 60, 10]) throws Error("findMax requires an array of numbers") (max can't compare non-numerical elements, so it shouldn't guess) | ||
|
|
||
| You should implement this function in max.js, and add tests for it in this file. | ||
|
|
||
| We have set things up already so that this file can see your function from the other file. | ||
| */ | ||
|
|
||
| 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"); | ||
|
|
||
| // Given an array with one number | ||
| // When passed to the max function | ||
| // Then it should return that number | ||
|
|
||
| // 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 just negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the closest one to zero | ||
|
|
||
| // Given an array with decimal numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest decimal number | ||
|
|
||
| // 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 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") | ||
| test("given an empty array, returns -Infinity", () => { | ||
| expect(findMax([])).toBe(-Infinity); | ||
| }); | ||
|
|
||
| test("given an array with one number, returns that number", () => { | ||
| expect(findMax([7])).toBe(7); | ||
| }); | ||
|
|
||
| test("returns the largest of positive and negative numbers", () => { | ||
| expect(findMax([-6, 5, -3, 8])).toBe(8); | ||
| }); | ||
|
|
||
| test("returns the closest to zero for all negatives", () => { | ||
| expect(findMax([-9, -2, -6])).toBe(-2); | ||
| }); | ||
|
|
||
| test("returns the largest decimal number", () => { | ||
| expect(findMax([1.2, 3.8, 2.6])).toBe(3.8); | ||
| }); | ||
|
|
||
| test("throws for an array containing a non-number", () => { | ||
| expect(() => findMax(["hey", 10])).toThrow( | ||
| new Error("findMax requires an array of numbers") | ||
| ); | ||
| }); | ||
|
|
||
| test("throws when not given an array", () => { | ||
| expect(() => findMax("hey")).toThrow( | ||
| new Error("findMax requires an array of numbers") | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,18 @@ | ||
| 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"); | ||
| } | ||
| if (!list.every((item) => typeof item === "number")) { | ||
| throw new Error("calculateMean requires an array of numbers"); | ||
| } | ||
| let total = 0; | ||
| for (const item of list) { | ||
| total += item; | ||
| } | ||
| return total / list.length; | ||
| } | ||
|
|
||
| module.exports = calculateMean; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,50 +1,31 @@ | ||
| /* Calculate the mean of an array of numbers | ||
|
|
||
| In this kata, you will need to implement a function that calculates the mean of an array of numbers. | ||
|
|
||
| E.g. calculateMean([1, 2, 6]), target output: 3 | ||
| E.g. calculateMean([]) throws Error("calculateMean requires a non-empty array") | ||
| E.g. calculateMean("banana") throws Error("calculateMean requires an array of numbers") | ||
|
|
||
| There is no mean of an empty array, and a string isn't an array of numbers, so | ||
| calculateMean should throw rather than return a value. This is the same problem | ||
| calculateMedian solves in the prep and in the fix directory. | ||
|
|
||
| You should implement this function in mean.js, and add tests for it in this file. | ||
|
|
||
| We have set things up already so that this file can see your function from the other file. | ||
| */ | ||
|
|
||
| const calculateMean = require("./mean.js"); | ||
|
|
||
| // Acceptance criteria: | ||
|
|
||
| // Given an array of numbers | ||
| // When passed to calculateMean | ||
| // Then it should return their mean | ||
| // Delete this test.todo and replace it with a test. | ||
| test.todo("given [1, 2, 6], returns 3"); | ||
|
|
||
| // Given an array with a single number | ||
| // When passed to calculateMean | ||
| // Then it should return that number | ||
|
|
||
| // Given an array containing negative or decimal numbers | ||
| // When passed to calculateMean | ||
| // Then it should return the correct mean | ||
|
|
||
| // Given an empty array | ||
| // When passed to calculateMean | ||
| // Then it should throw Error("calculateMean requires a non-empty array") | ||
|
|
||
| // Given a value that isn't an array, e.g. "banana", 42, null or {} | ||
| // When passed to calculateMean | ||
| // Then it should throw Error("calculateMean requires an array of numbers") | ||
|
|
||
| // Given no argument at all | ||
| // When passed to calculateMean | ||
| // Then it should throw Error("calculateMean requires an array of numbers") | ||
|
|
||
| // Given an array containing a non-number value, e.g. [1, "2", 3] | ||
| // When passed to calculateMean | ||
| // Then it should throw Error("calculateMean requires an array of numbers") | ||
| test("given [1, 2, 6], returns 3", () => { | ||
| expect(calculateMean([1, 2, 6])).toBe(3); | ||
| }); | ||
|
|
||
| test("returns the number for a single item array", () => { | ||
| expect(calculateMean([8])).toBe(8); | ||
| }); | ||
|
|
||
| test("handles negative and decimal numbers", () => { | ||
| expect(calculateMean([-2, 4, 3.5])).toBeCloseTo(1.8333); | ||
| }); | ||
|
|
||
| test("throws for an empty array", () => { | ||
| expect(() => calculateMean([])).toThrow( | ||
| new Error("calculateMean requires a non-empty array") | ||
| ); | ||
| }); | ||
|
|
||
| test("throws when not given an array", () => { | ||
| expect(() => calculateMean("banana")).toThrow( | ||
| new Error("calculateMean requires an array of numbers") | ||
| ); | ||
| }); | ||
|
|
||
| test("throws for an array containing a non-number", () => { | ||
| expect(() => calculateMean([1, "2", 3])).toThrow( | ||
| new Error("calculateMean requires an array of numbers") | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,15 @@ | ||
| function sum(elements) { | ||
| function sum(list) { | ||
| if (!Array.isArray(list)) { | ||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
| if (!list.every(Number.isFinite)) { | ||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
| let total = 0; | ||
| for (const item of list) { | ||
| total += item; | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| module.exports = sum; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You missed implementing this test.
Please note that in this test, in addition to checking the returned array has the same elements, you should also test whether the returned array is a different array.