Skip to content

LONDON | MAY-2025 | KHILOLA_RUSTAMOVA| MDG/sprint 1 #566

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
16 changes: 13 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@
// 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;
//Keep only the numbers
const numbers = list.filter(item => typeof item === 'number');
// If there are no numbers, return null
if (numbers.length === 0) return null;
// Sort the numbers in ascending order
numbers.sort((a, b) => a - b);
// If the list has an even number of elements, return the average of the two middle
const middleIndex = Math.floor(numbers.length / 2);
if (numbers.length % 2 === 0) {
return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2;
} else //If the count is odd, return the middle number
return numbers[middleIndex];
}

module.exports = calculateMedian;
16 changes: 15 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
function dedupe() {}
function dedupe(arr) {
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;
13 changes: 12 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,22 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2]
// 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 original array", () => {
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
test("given an array with duplicates, removes duplicates preserving 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]);
});
12 changes: 11 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
function findMax(elements) {
function findMax(arr) {
// Keep only the number values
const numbers = arr.filter(item => typeof item === 'number');

// If no numbers found, return -Infinity
if (numbers.length === 0) {
return -Infinity;
}

// Return the biggest number
return Math.max(...numbers);
}

module.exports = findMax;
24 changes: 18 additions & 6 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,40 @@ 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, 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([45])).toBe(45);
});

// 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 returns the largest number overall",
() => {expect(findMax([20,-10,35,-45])).toBe(35)
});
// 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 return the closest one to zero",
() => {expect(findMax([-20,-10,-35,-45])).toBe(-10)});
// 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 return the largest decimal number",
() => {expect(findMax([1.8,2.4,3.5,2.2])).toBe(3.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

// Given an array with only non-number values
test("given an array with non number values return the max and ignore non-numeric values",
() => {expect(findMax(["Hallo", 5, "Then", 20])).toBe(20)});
/// 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 return the least surprising value given how it behaves for all other inputs",
() => {expect(findMax(["Hallo", "Lola", "Event", "party"])).toBe(-Infinity)});
5 changes: 4 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
function sum(elements) {
function sum(arr) {
return arr
.filter(item => typeof item === "number") // keep only numbers
.reduce((acc, curr) => acc + curr, 0); // sum them up starting from 0
}

module.exports = sum;
22 changes: 18 additions & 4 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,38 @@ 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 just one number, returns that number", () => {
expect(sum([30])).toBe(30);
});

// 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 containing negative numbers, return the correct total sum", () => {
expect(sum([60,-10,-25,30])).toBe(55);
});
// 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/float numbers, return the correct total sum", () => {
expect(sum([22.5,15,40,3,5])).toBe(85.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("Given an array containing non-number values, return the sum of the numerical elements", () => {
expect(sum(["Enter",30,"hi", 12])).toBe(42);
});
// 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, return the least surprising value given how it behaves for all other inputs", () => {
expect(sum(["nana","hey","welcome"])).toBe(0);
});
3 changes: 1 addition & 2 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// 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;
}
Expand Down
2 changes: 1 addition & 1 deletion Sprint-1/refactor/includes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test("returns true when the target is in array multiple times", () => {
});

test("returns false for empty array", () => {
const currentOutput = includes([]);
const currentOutput = includes([], "anything");
const targetOutput = false;

expect(currentOutput).toEqual(targetOutput);
Expand Down
17 changes: 17 additions & 0 deletions Sprint-1/stretch/aoc-2018-day1/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@



function calculateFrequency(input) {
let frequency = 0;

// Convert the input string into an array of changes
const changes = input.trim().split('\n');

for (let change of changes) {
frequency += Number(change); // Convert each string to number and add
}


return frequency;
}
module.exports = calculateFrequency;
38 changes: 38 additions & 0 deletions Sprint-1/stretch/aoc-2018-day1/input.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const calculateFrequency = require("./input.js");


test('example from prompt', () => {
// Input as a multiline string (mimics what the puzzle provides)
const input = `+1\n-2\n+3\n+1`;
// Expected result: 0 + 1 - 2 + 3 + 1 = 3
expect(calculateFrequency(input)).toBe(3);
});
// All positive values
test('all positive numbers', () => {
const input = `+1\n+1\n+1`;
// Expected result: 0 + 1 + 1 + 1 = 3
expect(calculateFrequency(input)).toBe(3);
});
// All negative values
test('all negative numbers', () => {
const input = `-1\n-2\n-3`;
// Expected result: 0 - 1 - 2 - 3 = -6
expect(calculateFrequency(input)).toBe(-6);
});
// Mix of positive and negative that cancels out
test('mixed values resulting in zero', () => {
const input = `+1\n+1\n-2`;

// Expected result: 0 + 1 + 1 - 2 = 0
expect(calculateFrequency(input)).toBe(0);
});

// Empty input string should return 0
test('empty input returns 0', () => {
const input = ``;

// No changes, frequency stays at 0
expect(calculateFrequency(input)).toBe(0);
});