Skip to content

London | May-2025 | Reza Jahanimir | sprint1 #531

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
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,40 @@
// or 'list' has mixed values (the function is expected to sort only numbers).

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;

// Validate input — must be an array
if (!Array.isArray(list)) {
return null;
}

// Check if the input is an array and contains numeric values
const sortedList = list.filter(item => typeof item === 'number').sort((a, b) => a - b)

// find the middle index
const middleIndex = Math.floor(sortedList.length / 2);


// If the sorted list is empty, return null
if (sortedList.length === 0) {
return null; // Return null if there are no numeric values
}

// If the length of the sorted list is even, add 0.5 to the middle number
if (sortedList.length % 2 === 0) {

// this is my wrong attempt for solving this logic but it will cause error
// return (sortedList[middleIndex - 1] + 0.5);

// tis is the correct way of finding the median when the length is even
return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2;
}else {
// If the length of the sorted list is odd, return the middle number
return sortedList[middleIndex];
}


// const median = list.splice(middleIndex, 1)[0];
// return median;
}

module.exports = calculateMedian;
5 changes: 5 additions & 0 deletions Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ describe("calculateMedian", () => {
].forEach(({ input, expected }) =>
it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
);
});

// Failing Test Case for finding the middle number
TestCase("If the length of the sorted list is even, add 0.5 to the middle number", () => {
expect(calculateMedian([4, 10])).toBe(7);
});
23 changes: 22 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
function dedupe() {}
function dedupe(array) {
// Validate input
if (!Array.isArray(array)) return [];

// set is a collection of unique values
// It will automatically handle duplicates for us
const seen = new Set(); // {}
const result = [];

// Iterate through the array
// If the item is not in the set, add it to both the set and the result array
for (const item of array) {
if (!seen.has(item)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work! Can you explain why the if condition is necessary here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// if will compare the item with the item inside seen and as seen is a set it will not allow duplicate values
// when we loop through the array it will check if the item is already in the set or not
// if it is the if condition will be false and it will not add the item to the result array
// if it is not in the set it will add the item to the set and also to the result array
// so at the end we will have a result array with unique values only

seen.add(item);
result.push(item);
}
}

return result;
}

module.exports = dedupe;
15 changes: 14 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,25 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2]
// 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 should return 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, it should return a copy of the original array", () => {
const input = [1, 2, 3];
expect(dedupe(input)).toEqual(input);
});

// 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 strings or numbers, it should remove the duplicate values, preserving the first occurrence of each element", () => {
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]);
});


14 changes: 14 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
function findMax(elements) {
// Return -Infinity if the input is not an array
if (!Array.isArray(elements)) {
return -Infinity;
}

// Check if the input is an array and contains numeric values
const numericValues = elements
.filter((elements) => typeof elements === "number")

// If the sorted list is empty, return null
if (numericValues.length === 0) return -Infinity;

// Return the largest numeric value found in the array
return Math.max(...numericValues);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good code, but the comment has an inaccuracy. Please review the comment above this line

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your are right I am not working with a sorted array, i am simply filtering numeric values and getting the max

}

module.exports = findMax;
22 changes: 21 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,48 @@ const findMax = require("./max.js");
// 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, it should return -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, it should return 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("Given an array with both positive and negative numbers, it should return the largest number overall", () => {
expect(findMax([-10, 20, -30, 40])).toBe(40);
});

// 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, it should return the closest one to zero", () => {
expect(findMax([-5, -10, -3])).toBe(-3);
});

// 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, it should return the largest decimal number", () => {
expect(findMax([1.5, 2.3, 0.7])).toBe(2.3);
});

// 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, it should return the max and ignore non-numeric values", () => {
expect(findMax(['hey', 10, 'hi', 60, 10])).toBe(60);
});

// 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, it should return -Infinity", () => {
expect(findMax(['a', 'b', 'c'])).toBe(-Infinity);
});
19 changes: 19 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
function sum(elements) {
// Return -Infinity if the input is not an array
if (!Array.isArray(elements)) {
return null;
}

// Check if the input is an array and contains numeric values
const numericValues = elements.filter(
(elements) => typeof elements === "number"
);

// If there are no numeric values, return 0
if (numericValues.length === 0) return 0;

// Return the sum of all numeric values in the array
let sum = 0;
for (let i = 0; i < numericValues.length; i++) {
sum += numericValues[i];
}
return sum;
}

module.exports = sum;
19 changes: 18 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,41 @@ const sum = require("./sum.js");
// Given an empty array
// When passed to the sum function
// Then it should return 0
test.todo("given an empty array, returns 0")
test("sum([]) 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("sum([5]) returns 5", () => {
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("sum([-5, -10, -15]) returns -30", () => {
expect(sum([-5, -10, -15])).toBe(-30);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test("sum([1.5, 2.5, 3.5]) returns 7.5", () => {
expect(sum([1.5, 2.5, 3.5])).toBe(7.5);
});

// 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("sum(['a', 10, 'b', 20]) returns 30", () => {
expect(sum(["a", 10, "b", 20])).toBe(30);
});

// 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
test("sum(['a', 'b', 'c']) returns 0", () => {
expect(sum(["a", "b", "c"])).toBe(0);
});
Loading