generated from CodeYourFuture/Module-Template
-
-
Couldn't load subscription status.
- Fork 165
London | 25-ITP-May | Houssam Lahlah | Sprint 1 | Sprint-1 #785
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
HoussamLh
wants to merge
8
commits into
CodeYourFuture:main
Choose a base branch
from
HoussamLh: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.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
10f6345
fix: correct calculateMedian implementation
HoussamLh a3e3a13
feat: implement dedupe function with tests
HoussamLh e2e920f
feat: implement findMax function with tests
HoussamLh 537861b
feat: implement sum function with tests
HoussamLh c4e5da6
refactor: update includes function to use for...of loop
HoussamLh d4ae6ff
- add Advent of Code Day 1 solution
HoussamLh 70d01d9
Refactor calculateMedian to remove unnecessary splice and avoid mutat…
HoussamLh aa27439
Remove unnecessary package.json from this branch
HoussamLh 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,23 @@ | ||
| // 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) return null; | ||
|
|
||
| // Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null) | ||
| // or 'list' has mixed values (the function is expected to sort only numbers). | ||
| // Filter out non-number values | ||
| const numbers = list.filter(item => typeof item === "number"); | ||
| if (numbers.length === 0) return null; | ||
|
|
||
| function calculateMedian(list) { | ||
| const middleIndex = Math.floor(list.length / 2); | ||
| const median = list.splice(middleIndex, 1)[0]; | ||
| return median; | ||
| // Sort numbers in ascending order | ||
| numbers.sort((a, b) => a - b); | ||
|
|
||
| const middleIndex = Math.floor(numbers.length / 2); | ||
|
|
||
| // If odd number of elements, return middle | ||
| if (numbers.length % 2 !== 0) { | ||
| return numbers[middleIndex]; | ||
| } | ||
|
|
||
| // If even, return average of two middle numbers | ||
| return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2; | ||
| } | ||
|
|
||
| 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 |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "devDependencies": { | ||
| "jest": "^30.1.3" | ||
| }, | ||
| "scripts": { | ||
| "test": "jest" | ||
| } | ||
| } |
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,18 @@ | ||
| 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; | ||
|
|
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,27 +1,31 @@ | ||
| const dedupe = require("./dedupe.js"); | ||
| /* | ||
| Dedupe Array | ||
| describe("dedupe function", () => { | ||
|
|
||
| 📖 Dedupe means **deduplicate** | ||
| // Test 1: empty array | ||
| 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 2: array with no duplicates | ||
| test("given an array with no duplicates, it returns a copy of the original array", () => { | ||
| const arr = [1, 2, 3, 4]; | ||
| expect(dedupe(arr)).toEqual(arr); | ||
| expect(dedupe(arr)).not.toBe(arr); // ensure it returns a new array, not the same reference | ||
| }); | ||
|
|
||
| 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] | ||
| */ | ||
| // Test 3: array with duplicates (strings) | ||
| test("removes duplicate strings, preserving first occurrence", () => { | ||
| expect(dedupe(['a', 'a', 'a', 'b', 'b', 'c'])).toEqual(['a', 'b', 'c']); | ||
| }); | ||
|
|
||
| // Acceptance Criteria: | ||
| // Test 4: array with duplicates (numbers) | ||
| test("removes duplicate numbers, preserving first occurrence", () => { | ||
| expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); | ||
| }); | ||
|
|
||
| // 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 5: mixed array | ||
| test("removes duplicates in mixed arrays", () => { | ||
| expect(dedupe([1, 2, 1])).toEqual([1, 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 with strings or numbers | ||
| // When passed to the dedupe function | ||
| // Then it should remove the duplicate values, preserving the first occurence of each element | ||
| }); |
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,16 @@ | ||
| function findMax(elements) { | ||
| function findMax(arr) { | ||
| if (!Array.isArray(arr) || arr.length === 0) return -Infinity; | ||
|
|
||
| let max = -Infinity; | ||
|
|
||
| for (const item of arr) { | ||
| if (typeof item === "number" && 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,40 @@ | ||
| /* 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]), target output: 60 (sum ignores any non-numerical elements) | ||
|
|
||
| You should implement this function in max.js, and add tests for it in this file. | ||
| const findMax = require("./max.js"); | ||
|
|
||
| We have set things up already so that this file can see your function from the other file. | ||
| */ | ||
| describe("findMax function", () => { | ||
|
|
||
| const findMax = require("./max.js"); | ||
| // Test 1: empty array | ||
| test("given an empty array, returns -Infinity", () => { | ||
| expect(findMax([])).toBe(-Infinity); | ||
| }); | ||
|
|
||
| // 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 2: array with one number | ||
| test("given an array with one number, returns that number", () => { | ||
| expect(findMax([42])).toBe(42); | ||
| }); | ||
|
|
||
| // Given an array with one number | ||
| // When passed to the max function | ||
| // Then it should return that number | ||
| // Test 3: array with positive and negative numbers | ||
| test("returns the largest number from mixed positive and negative numbers", () => { | ||
| expect(findMax([3, -2, 7, -10, 5])).toBe(7); | ||
| }); | ||
|
|
||
| // Given an array with both positive and negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest number overall | ||
| // Test 4: array with only negative numbers | ||
| test("returns the closest number to zero for negative numbers", () => { | ||
| expect(findMax([-10, -2, -30])).toBe(-2); | ||
| }); | ||
|
|
||
| // Given an array with just negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the closest one to zero | ||
| // Test 5: array with decimal numbers | ||
| test("returns the largest decimal number", () => { | ||
| expect(findMax([1.1, 2.5, 2.49])).toBe(2.5); | ||
| }); | ||
|
|
||
| // Given an array with decimal numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest decimal number | ||
| // Test 6: array with non-number values | ||
| test("ignores non-numeric values and returns the largest number", () => { | ||
| expect(findMax([10, "apple", 20, null, 5])).toBe(20); | ||
| }); | ||
|
|
||
| // 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 7: array with only non-number values | ||
| test("array with only non-number values returns -Infinity", () => { | ||
| expect(findMax(["a", null, "b"])).toBe(-Infinity); | ||
| }); | ||
|
|
||
| // 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 | ||
| }); |
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 |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "devDependencies": { | ||
| "jest": "^30.1.3" | ||
| }, | ||
| "scripts": { | ||
| "test": "jest" | ||
| } | ||
| } |
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,10 @@ | ||
| function sum(elements) { | ||
| function sum(arr) { | ||
| if (!Array.isArray(arr)) return 0; | ||
|
|
||
| return arr.reduce((total, item) => { | ||
| return typeof item === "number" ? total + item : total; | ||
| }, 0); | ||
| } | ||
|
|
||
| module.exports = sum; | ||
|
|
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,36 +1,35 @@ | ||
| /* Sum the numbers in an array | ||
|
|
||
| In this kata, you will need to implement a function that sums the numerical elements of an array | ||
|
|
||
| E.g. sum([10, 20, 30]), target output: 60 | ||
| E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical elements) | ||
| */ | ||
|
|
||
| const sum = require("./sum.js"); | ||
|
|
||
| // Acceptance Criteria: | ||
| describe("sum function", () => { | ||
|
|
||
| // Test 1: empty array | ||
| test("given an empty array, returns 0", () => { | ||
| expect(sum([])).toBe(0); | ||
| }); | ||
|
|
||
| // Given an empty array | ||
| // When passed to the sum function | ||
| // Then it should return 0 | ||
| test.todo("given an empty array, returns 0") | ||
| // Test 2: array with one number | ||
| test("given an array with one number, returns that number", () => { | ||
| expect(sum([42])).toBe(42); | ||
| }); | ||
|
|
||
| // Given an array with just one number | ||
| // When passed to the sum function | ||
| // Then it should return that number | ||
| // Test 3: array with positive and negative numbers | ||
| test("returns the correct total sum for positive and negative numbers", () => { | ||
| expect(sum([10, -5, 15, -3])).toBe(17); | ||
| }); | ||
|
|
||
| // Given an array containing negative numbers | ||
| // When passed to the sum function | ||
| // Then it should still return the correct total sum | ||
| // Test 4: array with decimal numbers | ||
| test("returns the correct total sum for decimal/float numbers", () => { | ||
| expect(sum([1.5, 2.3, 3.2])).toBeCloseTo(7.0); // Use toBeCloseTo for floating points | ||
| }); | ||
|
|
||
| // Given an array with decimal/float numbers | ||
| // When passed to the sum function | ||
| // Then it should return the correct total sum | ||
| // Test 5: array with non-number values | ||
| test("ignores non-numeric values and sums numerical elements", () => { | ||
| expect(sum([10, "a", 20, null, 5])).toBe(35); | ||
| }); | ||
|
|
||
| // Given an array containing non-number values | ||
| // When passed to the sum function | ||
| // Then it should ignore the non-numerical values and return the sum of the numerical elements | ||
| // Test 6: array with only non-number values | ||
| test("returns 0 if there are no numeric elements", () => { | ||
| expect(sum(["a", null, "b"])).toBe(0); | ||
| }); | ||
|
|
||
| // Given an array with only non-number values | ||
| // When passed to the sum function | ||
| // Then it should return the least surprising value given how it behaves for all other inputs | ||
| }); |
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 |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "devDependencies": { | ||
| "jest": "^30.1.3" | ||
| }, | ||
| "scripts": { | ||
| "test": "jest" | ||
| } | ||
| } |
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.
Do you need to include this anonymous sorting function as an argument to sort?
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.
Hello LonMcGregor,
Thank you for the feedback!
I double-checked the sort function—using (a, b) => a - b is necessary to sort numbers numerically; otherwise, JavaScript sorts them as strings, which would break the median calculation.
I also removed the splice approach from my earlier attempt to avoid mutating the array—returning numbers**[middleIndex]** directly works cleanly for odd-length arrays.
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.
Good answer!