-
-
Notifications
You must be signed in to change notification settings - Fork 283
Sheffield | 26-ITP-Jan | Mahmoud Shaabo | Sprint 1 | Module-Data-Groups #1014
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
Closed
mahmoudshaabo1984
wants to merge
2
commits into
CodeYourFuture:main
from
mahmoudshaabo1984:sprint-1-exercises
Closed
Changes from all commits
Commits
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
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,12 @@ | ||
| function dedupe() {} | ||
| // dedupe.js | ||
| // Remove duplicate values from an array, preserving the first occurrence of each element | ||
|
|
||
| function dedupe(arr) { | ||
| // Return empty array if input is empty | ||
|
|
||
| // Use Set to automatically remove duplicates, then spread back into an array | ||
| // Set keeps only unique values in insertion order | ||
| return [...new Set(arr)]; | ||
| } | ||
|
|
||
| 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,42 @@ | ||
| const dedupe = require("./dedupe.js"); | ||
| /* | ||
| Dedupe Array | ||
|
|
||
| 📖 Dedupe means **deduplicate** | ||
|
|
||
| In this kata, you will need to deduplicate the elements of an array | ||
|
|
||
| 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] | ||
| */ | ||
|
|
||
| // 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 array", () => { | ||
| const original = [1, 2, 3]; | ||
| const result = dedupe(original); | ||
| expect(result).toEqual([1, 2, 3]); | ||
|
|
||
| // التعديل المطلوب من CJ: التأكد أن النتيجة نسخة جديدة وليست نفس المرجع | ||
| expect(result).not.toBe(original); | ||
| }); | ||
|
|
||
| // Given an array with duplicate strings | ||
| // When passed to the dedupe function | ||
| // Then it should remove duplicates, preserving first occurrence | ||
| test("removes duplicate strings", () => { | ||
| expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]); | ||
| }); | ||
|
|
||
| // Given an array with duplicate numbers | ||
| // When passed to the dedupe function | ||
| // Then it should remove duplicates, preserving first occurrence | ||
| test("removes duplicate numbers", () => { | ||
| expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); | ||
| }); | ||
|
|
||
| // Given an array with strings or numbers | ||
| // Given an array with mixed types (numbers and strings) | ||
| // When passed to the dedupe function | ||
| // Then it should remove the duplicate values, preserving the first occurence of each element | ||
| // Then it should treat numbers and strings as different even if they look same | ||
| test("removes duplicates in mixed array and distinguishes numbers from strings", () => { | ||
| // التعديل المطلوب من CJ: التأكد أن الرقم 1 يختلف عن النص "1" | ||
| expect(dedupe([1, "1", 2, "1", 1])).toEqual([1, "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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,18 @@ | ||
| // max.js | ||
| // Find the largest numerical element in an array | ||
| // Non-numeric values are ignored | ||
|
|
||
| function findMax(elements) { | ||
| // Filter to keep only numbers (ignores strings, null, undefined, etc.) | ||
| const numbersOnly = elements.filter( | ||
| (item) => typeof item === "number" && !isNaN(item) | ||
| ); | ||
|
|
||
| // If no numbers remain, return -Infinity (consistent with Math.max() behaviour) | ||
| if (numbersOnly.length === 0) return -Infinity; | ||
|
|
||
| // Math.max with spread returns the largest number | ||
| return Math.max(...numbersOnly); | ||
| } | ||
|
|
||
| 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,55 @@ | ||
| /* 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. | ||
|
|
||
| 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"); | ||
| 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([42])).toBe(42); | ||
| }); | ||
|
|
||
| // Given an array with both positive and negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest number overall | ||
| test("returns the largest number from positive and negative values", () => { | ||
| expect(findMax([-10, -3, 0, 5, 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("returns the closest to zero when all numbers are negative", () => { | ||
| expect(findMax([-5, -1, -3])).toBe(-1); | ||
| }); | ||
|
|
||
| // Given an array with decimal numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest decimal number | ||
| test("returns the largest decimal number", () => { | ||
| expect(findMax([1.1, 2.5, 0.9, 2.4])).toBe(2.5); | ||
| }); | ||
|
|
||
| // 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("ignores non-numeric values and returns the max number", () => { | ||
| expect(findMax(["hey", 10, "hi", 60, 10])).toBe(60); | ||
|
|
||
| // CJ's feedback: Ensure numeric strings like "300" are ignored. | ||
| // The function should not coerce strings into numbers during comparison. | ||
| // In this case, 10 is the maximum because "300" is a string and should be skipped. | ||
| expect(findMax(["300", 10, 2])).toBe(10); | ||
| }); | ||
|
cjyuan marked this conversation as resolved.
|
||
|
|
||
| // 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 | ||
| // Then it should return -Infinity (same as empty array behaviour) | ||
| test("returns -Infinity when array contains only non-numeric values", () => { | ||
| expect(findMax(["a", "b", null])).toBe(-Infinity); | ||
| }); | ||
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,19 @@ | ||
| // sum.js | ||
| // Sum all numerical elements in an array | ||
| // Non-numeric values are ignored | ||
|
|
||
| function sum(elements) { | ||
| // Filter to keep only numbers (ignores strings, null, undefined, etc.) | ||
| const numbersOnly = elements.filter( | ||
| (item) => typeof item === "number" && !isNaN(item) | ||
| ); | ||
|
|
||
| // If no numbers found, return 0 (consistent with empty array behaviour) | ||
| if (numbersOnly.length === 0) return 0; | ||
|
|
||
| // reduce() accumulates all numbers into a single total | ||
| // 0 is the initial value of the accumulator | ||
| return numbersOnly.reduce((total, num) => total + num, 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,45 @@ | ||
| /* 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: | ||
|
|
||
| // Given an empty array | ||
| // When passed to the sum function | ||
| // Then it should return 0 | ||
| test.todo("given an empty array, returns 0") | ||
| test("given an empty array, returns 0", () => { | ||
| expect(sum([])).toBe(0); | ||
| }); | ||
|
|
||
| // Given an array with just one number | ||
| // When passed to the sum function | ||
| // Then it should return that number | ||
| test("given an array with one number, returns that number", () => { | ||
| expect(sum([5])).toBe(5); | ||
| }); | ||
|
|
||
| // Given an array containing negative numbers | ||
| // When passed to the sum function | ||
| // Then it should still return the correct total sum | ||
| test("correctly sums arrays containing negative numbers", () => { | ||
| expect(sum([10, -3, 5])).toBe(12); | ||
| }); | ||
|
|
||
| // Given an array with decimal/float numbers | ||
| // When passed to the sum function | ||
| // Then it should return the correct total sum | ||
| test("correctly sums decimal numbers", () => { | ||
| // تم التعديل هنا باستخدام toBeCloseTo لضمان دقة حساب الأرقام العشرية | ||
| expect(sum([1.5, 2.5, 3.0])).toBeCloseTo(7); | ||
| expect(sum([0.1, 0.2])).toBeCloseTo(0.3); | ||
| }); | ||
|
cjyuan marked this conversation as resolved.
|
||
|
|
||
| // 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("ignores non-numeric values and sums only numbers", () => { | ||
| expect(sum(["hey", 10, "hi", 60, 10])).toBe(80); | ||
| }); | ||
|
|
||
| // 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 | ||
| // Then it should return 0 (same as empty array behaviour) | ||
| test("returns 0 when array contains only non-numeric values", () => { | ||
| expect(sum(["a", null, undefined])).toBe(0); | ||
| }); | ||
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
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.