Skip to content

LONDON | ITP-MAY-25 | TEWODROS BEKERE | M-D-G | SPRINT-1 #624

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 6 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
22 changes: 18 additions & 4 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,23 @@
// 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;
if (!Array.isArray(list)) {
return null;
}
const numbers = list.filter(item => typeof item === 'number'); // Filter to keep only numbers
if (numbers.length === 0) {
return null; // If there are no numbers, return null
}
numbers.sort((a, b) => a - b); // Sort the numbers

const middleIndex = Math.floor(numbers.length / 2); // Find the middle index

if (numbers.length % 2 === 0) {
return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2; //Even number of elements → average of two middle numbers
}else {
return numbers[middleIndex]; // Middle number for odd length
}
}

module.exports = calculateMedian;

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

});



17 changes: 16 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
function dedupe() {}
function dedupe(arr) {
const result = [];
const seen = new Set();

for (let i = 0; i < arr.length; i++) {
if (!seen.has(arr[i])) {
seen.add(arr[i]);
result.push(arr[i]);
}
}

return result;
}


module.exports = dedupe;
29 changes: 28 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,39 @@ 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.todo("given an empty array, it returns an empty array");

describe('dedupe', () => {
it("returns an empty array when input is 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

it("returns a copy of the original array when there are no duplicates", () => {
expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]);
expect(dedupe(['a', 'b', 'c'])).toEqual(['a', 'b', 'c']);
});

// 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

it("removes duplicate numbers and preserves order", () => {
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
});

it("removes duplicate strings and preserves order", () => {
expect(dedupe(['a', 'a', 'a', 'b', 'b', 'c'])).toEqual(['a', 'b', 'c']);
});
it("handles mixed types and removes duplicates", () => {
expect(dedupe([1, 'a', 1, 'b', 'a', 2])).toEqual([1, 'a', 'b', 2]);
});
it("handles arrays with only duplicates", () => {
expect(dedupe(['x', 'x', 'x'])).toEqual(['x']);
expect(dedupe([2, 2, 2, 2])).toEqual([2]);
});

});
15 changes: 15 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
function findMax(elements) {
if (elements.length === 0) { // If the array is empty, return -Infinity
return -Infinity;
}
let max = -Infinity; // Initialize max with the first numeric value found

for (const element of elements) {
if (typeof element === 'number') { // Check if the element is a number

if (element > max) {
max = element;
}
}
}

return max;
}

module.exports = findMax;
33 changes: 32 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,59 @@ 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.todo("given an empty array, returns -Infinity");

// Empty array → return -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 one number in the array, 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("given positive and negative numbers, returns the largest number", () => {
expect(findMax([-10, 0, 50, -20, 30])).toBe(50);
});


// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero

test("given only negative numbers, returns the one closest to zero", () => {
expect(findMax([-10, -5, -30])).toBe(-5);
});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number

test("given decimal numbers, returns the largest decimal number", () => {
expect(findMax([1.5, 2.75, 0.99])).toBe(2.75);
});

// 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 numbers and non-numbers, returns max of numeric values", () => {
expect(findMax(["hi", 10, null, 60, "test"])).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

// Only non-numeric values → return -Inf
test("given an array with only non-numeric values, returns -Infinity", () => {
expect(findMax(["a", "b", null, undefined, {}, []])).toBe(-Infinity);
});
10 changes: 10 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
function sum(elements) {
let total = 0;
for (let i = 0; i < elements.length; i++) {
let value = elements[i];
if (typeof value === 'number' && !isNaN(value)) { // Check if the value is a number
total = total + value;
}
}
return total;
}



module.exports = sum;
27 changes: 26 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,49 @@ 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("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("given an array with negative numbers, returns correct sum", () => {
expect(sum([-5, -10, 15])).toBe(0);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum

test("given an array with decimal numbers, returns correct sum", () => {
expect(sum([1.5, 2.5, 3.0])).toBe(7.0);
});

// 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("given an array with numbers and non-numbers, returns sum of numbers only", () => {
expect(sum(["hello", 10, "world", 20, null, 30])).toBe(60);
});

// 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("given an array with only non-number values, returns 0", () => {
expect(sum(["a", "b", null, undefined, {}, []])).toBe(0);
});


5 changes: 3 additions & 2 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Refactor the implementation of includes to use a for...of loop

function includes(list, target) {
for (let index = 0; index < list.length; index++) {
const element = list[index];
for (const element of list) {
if (element === target) {
return true;
}
}
return false;
}



module.exports = includes;
Loading