Repository navigation
Cape Town | 26-ITP-Sep | Quentin Gwele | Sprint 1 | Data-group #1576
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
lwandogwele52-ui
wants to merge
8
commits into
CodeYourFuture:main
Choose a base branch
from
lwandogwele52-ui: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.
+222
−198
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
eab3aef
Fix calculateMedian implementation
lwandogwele52-ui 614473b
Implement findMax and unit tests
lwandogwele52-ui 1da3389
Implement sum and unit tests
lwandogwele52-ui e853ae3
Implement dedupe and unit tests
lwandogwele52-ui 994c8c7
Implement calculateMean and unit tests
lwandogwele52-ui eb48e31
Implement describeMedian and unit tests
lwandogwele52-ui 72256ea
Remove stray markdown from describe-median.js
lwandogwele52-ui dcd5bc6
Remove duplicate calculateMedian code and clean up trailing syntax
lwandogwele52-ui 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
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 | ||
| function calculateMedian(list) { | ||
| if ( | ||
| !Array.isArray(list) || | ||
| list.length === 0 || | ||
| list.some((item) => typeof item !== "number" || Number.isNaN(item)) | ||
| ) { | ||
| throw new Error("calculateMedian requires an array of numbers"); | ||
| } | ||
|
|
||
| // 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). | ||
| const sorted = [...list].sort((a, b) => a - b); | ||
| const middleIndex = Math.floor(sorted.length / 2); | ||
|
|
||
| function calculateMedian(list) { | ||
| const middleIndex = Math.floor(list.length / 2); | ||
| const median = list.splice(middleIndex, 1)[0]; | ||
| return median; | ||
| if (sorted.length % 2 === 0) { | ||
| return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2; | ||
| } | ||
|
|
||
| return sorted[middleIndex]; | ||
| } | ||
|
|
||
| module.exports = calculateMedian; | ||
| 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,5 @@ | ||
| function dedupe() {} | ||
| function dedupe(elements) { | ||
| return [...new Set(elements)]; | ||
| } | ||
|
|
||
| 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,20 @@ | ||
| const dedupe = require("./dedupe.js"); | ||
| /* | ||
| Dedupe Array | ||
|
|
||
| 📖 Dedupe means **deduplicate** | ||
| describe("dedupe", () => { | ||
| 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("given an array with no duplicates, it returns a copy of the original array", () => { | ||
| const input = [1, 2, 3]; | ||
| const result = dedupe(input); | ||
| expect(result).toEqual([1, 2, 3]); | ||
| expect(result).not.toBe(input); | ||
| }); | ||
|
|
||
| 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] | ||
| */ | ||
|
|
||
| // 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"); | ||
|
|
||
| // 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("given an array of strings or numbers, it returns a new array with duplicates removed while preserving the 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]); | ||
| }); | ||
| }); |
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,22 @@ | ||
| /* 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"'); | ||
|
|
||
| // Given an empty array | ||
| // When passed to describeMedian | ||
| // Then it should return "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" | ||
| describe("describeMedian", () => { | ||
| test('given [1, 2, 3], returns "The median is 2"', () => { | ||
| expect(describeMedian([1, 2, 3])).toBe("The median is 2"); | ||
| }); | ||
|
|
||
| test("given an empty array, returns an explanation using the error message", () => { | ||
| expect(describeMedian([])).toBe( | ||
| "Could not calculate a median: calculateMedian requires a non-empty array" | ||
| ); | ||
| }); | ||
|
|
||
| test("given something that isn't an array of numbers, returns an explanation using the error message", () => { | ||
| expect(describeMedian("banana")).toBe( | ||
| "Could not calculate a median: calculateMedian requires an array of numbers" | ||
| ); | ||
| expect(describeMedian([1, "two", 3])).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,12 @@ | ||
| function findMax(elements) { | ||
| if ( | ||
| !Array.isArray(elements) || | ||
| elements.some((item) => typeof item !== "number" || Number.isNaN(item)) | ||
| ) { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
|
|
||
| return Math.max(...elements); | ||
| } | ||
|
|
||
| module.exports = findMax; | ||
| 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,41 @@ | ||
| /* 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") | ||
| describe("findMax", () => { | ||
| test("given an empty array, returns -Infinity", () => { | ||
| expect(findMax([])).toBe(-Infinity); | ||
| }); | ||
|
|
||
| test("given an array with one number, returns that number", () => { | ||
| expect(findMax([42])).toBe(42); | ||
| }); | ||
|
|
||
| test("given an array with both positive and negative numbers, returns the largest number overall", () => { | ||
| expect(findMax([-10, 50, 0, -25, 30])).toBe(50); | ||
| }); | ||
|
|
||
| test("given an array with just negative numbers, returns the closest one to zero", () => { | ||
| expect(findMax([-30, -5, -100, -12])).toBe(-5); | ||
| }); | ||
|
|
||
| test("given an array with decimal numbers, returns the largest decimal number", () => { | ||
| expect(findMax([1.5, 3.75, 2.1, 3.74])).toBe(3.75); | ||
| }); | ||
|
|
||
| test("given an array containing a value that isn't a number, throws an Error", () => { | ||
| expect(() => findMax(["hey", 10, "hi", 60, 10])).toThrow( | ||
| new Error("findMax requires an array of numbers") | ||
| ); | ||
| }); | ||
|
|
||
| test("given something that isn't an array at all, throws an Error", () => { | ||
| 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") | ||
| ); | ||
| }); | ||
| }); |
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"); | ||
| } | ||
|
|
||
| module.exports = calculateMean; | ||
| if (list.length === 0) { | ||
| throw new Error("calculateMean requires a non-empty array"); | ||
| } | ||
|
|
||
| if (list.some((item) => typeof item !== "number" || Number.isNaN(item))) { | ||
| throw new Error("calculateMean requires an array of numbers"); | ||
| } | ||
|
|
||
| const total = list.reduce((sum, num) => sum + num, 0); | ||
| 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,49 @@ | ||
| /* 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") | ||
| describe("calculateMean", () => { | ||
| test("given an array of numbers, returns their mean", () => { | ||
| expect(calculateMean([1, 2, 6])).toBe(3); | ||
| }); | ||
|
|
||
| test("given an array with a single number, returns that number", () => { | ||
| expect(calculateMean([10])).toBe(10); | ||
| }); | ||
|
|
||
| test("given an array containing negative or decimal numbers, returns the correct mean", () => { | ||
| expect(calculateMean([-10, 20, -4])).toBe(2); | ||
| expect(calculateMean([1.5, 2.5, 3.5])).toBeCloseTo(2.5); | ||
| }); | ||
|
|
||
| test("given an empty array, throws Error('calculateMean requires a non-empty array')", () => { | ||
| expect(() => calculateMean([])).toThrow( | ||
| new Error("calculateMean requires a non-empty array") | ||
| ); | ||
| }); | ||
|
|
||
| test("given a value that isn't an array, throws Error('calculateMean requires an array of numbers')", () => { | ||
| expect(() => calculateMean("banana")).toThrow( | ||
| new Error("calculateMean requires an array of numbers") | ||
| ); | ||
| expect(() => calculateMean(42)).toThrow( | ||
| new Error("calculateMean requires an array of numbers") | ||
| ); | ||
| expect(() => calculateMean(null)).toThrow( | ||
| new Error("calculateMean requires an array of numbers") | ||
| ); | ||
| expect(() => calculateMean({})).toThrow( | ||
| new Error("calculateMean requires an array of numbers") | ||
| ); | ||
| }); | ||
|
|
||
| test("given no argument at all, throws Error('calculateMean requires an array of numbers')", () => { | ||
| expect(() => calculateMean()).toThrow( | ||
| new Error("calculateMean requires an array of numbers") | ||
| ); | ||
| }); | ||
|
|
||
| test("given an array containing a non-number value, throws Error('calculateMean requires an array of numbers')", () => { | ||
| expect(() => calculateMean([1, "2", 3])).toThrow( | ||
| new Error("calculateMean requires an array of numbers") | ||
| ); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Have you actually run this test case? I cannot see how this test passed. Since the first line of "Sprint-1/implement/describe-median.js"
// Don't change this function.and the first few lines in the functioncalculateMedian()has been deleted (maybe accidentally), you may need to restore only the function (but keeping your code indescribeMedian()untouched). This test case should be kept as well. If you don't know how to fix this safely, please feel free to ask a volunteer in Saturday workshop. Thank you.