Skip to content
22 changes: 19 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,25 @@
// 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;
// Check if the input is an array
if (!Array.isArray(list)) {
return null;
}
// Filter out non-numeric values from the array
const numbers = list.filter((x) => typeof x === "number");
// If the filtered array is empty, return null
if (numbers.length === 0) {
return null;
}

// Sort the numbers in ascending order so the median can be calculated correctly
numbers.sort((a, b) => a - b);

const middleIndex = Math.floor(numbers.length / 2);
// Calculate the median based on whether the length of the array is even or odd
return numbers.length % 2 === 0
? (numbers[middleIndex - 1] + numbers[middleIndex]) / 2
: numbers[middleIndex];
}

module.exports = calculateMedian;
13 changes: 12 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
function dedupe() {}
function dedupe(inputArray) {
const uniqueElements = [];
for (const el of inputArray) {
if (uniqueElements.includes(el)) {
continue;
}
uniqueElements.push(el);
}
return uniqueElements;
}

module.exports = dedupe;
40 changes: 28 additions & 12 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,32 @@ E.g. dedupe([1, 2, 1]) returns [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");
describe("dedupe", () => {
// Given an empty array
// When passed to the dedupe function
// Then it should return an empty array
it("returns an empty array when given empty array", () => {
expect(dedupe([])).toStrictEqual([]);
});

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

// Given an array of strings or numbers
// When passed to the dedupe function
// Then it should return a new array with duplicates removed while preserving the
// first occurrence of each element from the original array.
// 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 array is the one given without duplicates", () => {
expect(dedupe([1, 2, 3, 4])).toStrictEqual([1, 2, 3, 4]);
expect(dedupe(["a", "b", "c", "d"])).toStrictEqual(["a", "b", "c", "d"]);
});
// Given an array of strings or numbers
// When passed to the dedupe function
// Then it should return a new array with duplicates removed while preserving the
// first occurrence of each element from the original array.
it("returns an array of unique elements with order preserving the first occurence of each element", () => {
expect(dedupe(["a", "a", "a", "b", "b", "c"])).toStrictEqual([
"a",
"b",
"c",
]);
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toStrictEqual([5, 1, 2, 3, 8]);
expect(dedupe([1, 2, 1])).toStrictEqual([1, 2]);
});
});
13 changes: 13 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
function findMax(elements) {
// filter out only numbers to the new array
const numElements = elements.filter(
(el) => !isNaN(el) && typeof el === "number"
);

return numElements.reduce(
(acc, curr) => (acc > curr ? acc : curr),
-Infinity
);
}

module.exports = findMax;

console.log(findMax([-10, 20, 0, 100, 1]));
console.log(findMax([]));
console.log(findMax(["I", 2, "am", -1000, "Lord", "200", "Voldemort", 10]));
84 changes: 54 additions & 30 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,67 @@
/* Find the maximum element of an array of numbers
/* 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.
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)
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.
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.
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");
describe("findMax function", () => {
// 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.
it("given an empty array, returns -Infinity", () => {
expect(findMax([])).toEqual(-Infinity);
});

// Given an array with one number
// When passed to the max function
// Then it should return that number
// given an array with one number
// when passed to the max function
// then it should return that number
it("given an array with one number, returns that number", () => {
expect(findMax([10])).toEqual(10);
});

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall
// given an array with both positive and negative numbers
// when passed to the max function
// then it should return the largest number overall
it("give an array with both positive and negative numbers, returns larges number overall", () => {
expect(findMax([-10, 20, 0, 100, 1])).toEqual(100);
expect(findMax([-10, -100, 0, 11, 1, -5])).toEqual(11);
});

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
// given an array with just negative numbers
// when passed to the max function
// then it should return the closest one to zero
it("given an array with just negative numbers, returns number closest to zero", () => {
expect(findMax([-1, -200, -35, -4, -100])).toEqual(-1);
});
// given an array with decimal numbers
// when passed to the max function
// then it should return the largest decimal number
it("given an array with decimal numbers, returns largest decimal number", () => {
expect(findMax([-1.2, 3.14, -0.1, -0.007, 5.5, 5.50001])).toEqual(5.50001);
});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
// Given an array with non-number values
// When passed to the max function
// Then it should return the max and ignore non-numeric values
it("given an array with non-number values, returns max, non-numeric values ignored", () => {
expect(
findMax(["I", 2, "am", -1000, "Lord", "200", "Voldemort", 10])
).toEqual(10);
});

// 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
// When passed to the max function
// Then it should return the least surprising value given how it behaves for all other inputs
// 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
it("given an array with only non-numeric values, it returns -Infinity", () => {
expect(findMax(["I", "am", "Lord", "Voldemort"])).toEqual(-Infinity);
});
});
6 changes: 6 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
function sum(elements) {
return elements.reduce((acc, curr) => {
if (typeof curr !== "number") {
return acc;
}
return acc + curr;
}, 0);
}

module.exports = sum;
76 changes: 49 additions & 27 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,52 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical
*/

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")

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

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum

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

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

// 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
describe("sum function", () => {
// Acceptance Criteria:

// Given an empty array
// When passed to the sum function
// Then it should return 0
it("given an empty array, should return 0", () => {
expect(sum([])).toEqual(0);
});

// Given an array with just one number

// When passed to the sum function
// Then it should return that number
it("given an array of just one number, returns that number", () => {
expect(sum([2])).toEqual(2);
});
// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
it("given an array containing negative numbers, returns correct total sum", () => {
expect(sum([-1, -3, -5, 0])).toEqual(-9);
});
// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
it("given an array with decimal numbers, returns correct total sum", () => {
expect(sum([-1.2, 2.3, 0.001, 2.0])).toEqual(3.101);
});
// 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
it("given an array containing non-numerical values, returns the sum of the numerical values", () => {
expect(sum(["I", 2, "am", -1000, "Lord", "200", "Voldemort", 10])).toEqual(
-988
);
});
// 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
it("given an array with only non-numerical values, returns 0", () => {
expect(sum(["I", "am", "Lord", "Voldemort"])).toEqual(0);
});

// Additional test: only positive integers
it("given an array with only positive integers, returns the sum of the elements", () => {
expect(sum([1, 2, 3, 100])).toEqual(106);
});
});
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
17 changes: 17 additions & 0 deletions Sprint-1/stretch/aoc-2018-day1/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const fs = require("fs");

let frequencies;

// read the input file and convert the input elements into numbers
try {
const data = fs.readFileSync("./input.txt", "utf8");
const inputData = data.split("\n").filter((line) => line.trim() !== "");
frequencies = inputData.map(Number);
} catch (err) {
console.error("Error reading file:", err);
}

function getFrequenciesSum(frequencies) {
return frequencies.reduce((acc, curr) => acc + curr, 0);
}
console.log(getFrequenciesSum(frequencies)); // result is 529
Loading