Skip to content
Closed
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 @@ -5,10 +5,24 @@
// 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).

// median.js
function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
if (!Array.isArray(list) || list.length === 0) return null;

const numbers = list.filter((item) => typeof item === "number");
if (numbers.length === 0) return null;

// Only ONE array — this sort does not mutate the original list
numbers.sort((a, b) => a - b);

const middle = Math.floor(numbers.length / 2);

if (numbers.length % 2 === 0) {
return (numbers[middle - 1] + numbers[middle]) / 2;
}

return numbers[middle];
}

module.exports = calculateMedian;

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



14 changes: 14 additions & 0 deletions Sprint-1/fix/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "cyf-sprint-1",
"version": "1.0.0",
"description": "Sprint 1 solutions",
"main": "index.js",
"scripts": {
"test": "jest"
},
"devDependencies": {
"jest": "^29.0.0"
},
"author": "",
"license": "MIT"
}
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 = []; // This empty array will store unique items
const seen = new Set(); // Used to track what we've already seen

for (let item of arr) {
if (!seen.has(item)) {
seen.add(item);
result.push(item);
}
}

return result;
}

module.exports = dedupe;

44 changes: 27 additions & 17 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
const dedupe = require("./dedupe.js");

/*
Dedupe Array
Dedupe = remove duplicates while keeping the first occurrence
*/

📖 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 — no duplicates (should return a copy)
test("given an array with no duplicates, it returns a copy of the array", () => {
const arr = [1, 2, 3, 4];
const result = dedupe(arr);

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]
*/
expect(result).toEqual([1, 2, 3, 4]);
expect(result).not.toBe(arr); // important check
Comment on lines +18 to +19
Copy link
Contributor

Choose a reason for hiding this comment

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

Nicely done.

});

// Acceptance Criteria:
// TEST 3 — removes duplicates
test("given an array with duplicates, it removes duplicates and preserves first occurrence", () => {
expect(dedupe(['a', 'a', 'b', 'c', '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]);
});

// 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 4 — should not mutate original array
test("does not mutate the original array", () => {
const arr = [1, 1, 2, 3];
const copy = [...arr];

// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
dedupe(arr);

// 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
expect(arr).toEqual(copy); // original unchanged
});
22 changes: 21 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
function findMax(elements) {
// If it's not an array, we can't process it
if (!Array.isArray(elements)) {
return null;
}

// Given an empty array, return -Infinity
if (elements.length === 0) {
return -Infinity;
}

// Keep only numeric values
const numericValues = elements.filter((el) => typeof el === "number");

// If there are no numeric values at all, return null
if (numericValues.length === 0) {
return null;
}

// Return the largest number (works for positive, negative, decimals)
return Math.max(...numericValues);
}

module.exports = findMax;
module.exports = findMax;
32 changes: 29 additions & 3 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,54 @@ 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 returns -Infinity", () => {
expect(findMax([])).toBe(-Infinity);
});


// Given an array with one number
// When passed to the max function
// Then it should return that number

// 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("given an array with both positive and negative numbers, returns the largest number overall", () => {
expect(findMax([-10, 0, 10, 20, -5])).toBe(20);
});
// 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, returns the closest one to zero", () => {
expect(findMax([-30, -10, -20, -5])).toBe(-5);
});
// 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, returns the largest decimal number", () => {
expect(findMax([1.5, 2.3, 0.7, 2.9])).toBe(2.9);
});
// 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, returns the max and ignores non-numeric values", () => {
expect(findMax([10, "hello", 20, null, 15, undefined, "30"])).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, returns null", () => {
expect(findMax(["a", "b", "c"])).toBeNull();
});

test("given an array with just negative numbers, returns the closest one to zero", () => {
expect(findMax([-20, -5, -15, -1])).toBe(-1);
});
17 changes: 16 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
function sum(elements) {

function sum(arr) {
// If not an array, return 0 safely
if (!Array.isArray(arr)) return 0;

let total = 0;

for (let item of arr) {
if (typeof item === "number" && !isNaN(item)) {
total += item;
}
}

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 one number, return that number", () => {
expect(sum([10])).toBe(10);
});
// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum

test("given negative numbers, returns correct total", () => {
expect(sum([-5, -10, 5])).toBe(-10);
});

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

test("given float/decimal numbers, returns correct total", () => {
expect(sum([1.5, 2.5, 3])).toBe(7);
});
test("sums decimal numbers with floating point safely", () => {
const result = sum([1.2, 0.6, 0.005]);
expect(result).toBeCloseTo(1.805);
});
// 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("ignore non-number values and sum only numbers", () => {
expect(sum(["hello", 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


test("array with only non-number values returns 0", () => {
expect(sum(["a", "b", {}, [], "hello"])).toBe(0);
});

Loading