Skip to content
20 changes: 17 additions & 3 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 sortedList = list
.filter((element) => typeof element === "number")
.sort((a, b) => a - b);

if (sortedList.length === 0) {
return null;
}
const middleIndex = Math.floor(sortedList.length / 2);

if (sortedList.length % 2 === 0) {
return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2;
} else {
return sortedList[middleIndex];
}
}

module.exports = calculateMedian;
8 changes: 7 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
function dedupe() {}
function dedupe(array) {
if (!Array.isArray(array)) return [];

return [...new Set(array)];
}

module.exports = dedupe;
81 changes: 80 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,83 @@
const dedupe = require("./dedupe.js");

describe("dedupe", () => {
test("Return empty array for given empty array", () => {
expect(dedupe([])).toEqual([]);
});

test("Return copy for no duplicates input", () => {
[
[2, 4, 1, 6.9, 0],
["a", "d", "e", "b", "n", "c"],
["2", "4", "1", "6", "9", "0"],
["cat", "dog", "cow"],
].forEach((input) => {
expect(dedupe(input)).toEqual(input);
});
});

test("removes duplicate strings", () => {
[
{ input: ["a", "a", "a", "b", "b", "c"], expected: ["a", "b", "c"] },
{
input: ["2", "4", "1", "6", "2", "4"],
expected: ["2", "4", "1", "6"],
},
{ input: ["cat", "dog", "cat"], expected: ["cat", "dog"] },
].forEach((obj) => expect(dedupe(obj.input)).toEqual(obj.expected));
});

test("removes duplicate numbers", () => {
[
{ input: [1, 2, 1], expected: [1, 2] },
{
input: [2, 4, 1, 6.9, 2, 0],
expected: [2, 4, 1, 6.9, 0],
},
].forEach((obj) => expect(dedupe(obj.input)).toEqual(obj.expected));
});

test("removes duplicate numbers and strings", () => {
expect(dedupe([1, "a", 2, "b", 1, "a", "2"])).toEqual([
1,
"a",
2,
"b",
"2",
]);
});

test("handles special characters", () => {
expect(dedupe(["@", "@", "#", "#", "!"])).toEqual(["@", "#", "!"]);
});

test("handles strings with spaces", () => {
expect(dedupe(["a a", "a a", "b b"])).toEqual(["a a", "b b"]);
});

test("handles null and undefined", () => {
expect(dedupe([null, null, undefined])).toEqual([null, undefined]);
});

test("handles boolean values", () => {
expect(dedupe([true, false, true])).toEqual([true, false]);
});

[null, undefined, 123, "abc", {}].forEach((input) => {
expect(dedupe(input)).toEqual([]);
});

test("does not mutate original array", () => {
const input = [1, 2, 3];
const original = [...input];

const result = dedupe(input);

expect(result).not.toBe(input);
expect(input).toEqual(original);
});
});

/*
Dedupe Array

Expand All @@ -24,5 +103,5 @@ test.todo("given an empty array, it returns an empty 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
// Then it should return a new array with duplicates removed while preserving the
// first occurrence of each element from the original array.
3 changes: 3 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
function findMax(elements) {
const numberArray = elements.filter((el) => typeof el === "number");
if (numberArray.length === 0) return -Infinity;
return Math.max(...numberArray);
}

module.exports = findMax;
31 changes: 30 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,40 @@ We have set things up already so that this file can see your function from the o

const findMax = require("./max.js");

describe("findMax", () => {
test("return -Infinity for empty array", () => {
expect(findMax([])).toEqual(-Infinity);
});

test("return same number with one value array", () => {
expect(findMax([3])).toEqual(3);
});

test("return largest number with both positive and negative ", () => {
expect(findMax([3, 6, -2, 0, -5, 2])).toEqual(6);
});

test("return largest number with just negative numbers ", () => {
expect(findMax([-3, -6, -2, -1, -5, -2])).toEqual(-1);
});

test("return largest number with just decimal numbers ", () => {
expect(findMax([-3, 6.75, 2, 1, 6.25, 2])).toEqual(6.75);
});

test("ignore non-numeric values with non-number values array", () => {
expect(findMax([-3, 6, "a", 1, "abc", 2])).toEqual(6);
});

test("return -Infinity with all non-number values", () => {
expect(findMax(["abc", "h", "a", "r", "b"])).toEqual(-Infinity);
});
});

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

// Given an array with one number
// When passed to the max function
Expand Down
4 changes: 3 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
function sum(elements) {
return elements
.filter((el) => typeof el === "number")
.reduce((sum, num) => sum + num, 0);
}

module.exports = sum;
28 changes: 27 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,38 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical

const sum = require("./sum.js");

describe("sum", () => {
test("Return 0 for empty array", () => {
expect(sum([])).toEqual(0);
});

test("Return same number with one value array", () => {
expect(sum([5])).toEqual(5);
});

test("Return sum for array containing negative numbers", () => {
expect(sum([2, -5, 0, 9, -10, 15])).toEqual(11);
});

test("Return sum for decimal/float numbers array", () => {
expect(sum([2, 5.5, 0, 9, 10.75])).toEqual(27.25);
});

test("Ignore non-numeric values with non-number values array", () => {
expect(sum([-3, 6, "a", 1, "abc", 2])).toEqual(6);
});

test("return 0 with all non-number values", () => {
expect(sum(["abc", "h", "a", "r", "b"])).toEqual(0);
});
});

// 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")
//test.todo("given an empty array, returns 0");

// Given an array with just one number
// When passed to the sum function
Expand Down
11 changes: 10 additions & 1 deletion Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Refactor the implementation of includes to use a for...of loop

function includes(list, target) {
/*function includes(list, target) {
for (let index = 0; index < list.length; index++) {
const element = list[index];
if (element === target) {
Expand All @@ -9,5 +9,14 @@ function includes(list, target) {
}
return false;
}
*/

function includes(list, target) {
for (const element of list) {
if (element === target) {
return true;
}
}
return false;
}
module.exports = includes;
9 changes: 9 additions & 0 deletions Sprint-1/stretch/aoc-2018-day1/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const fs = require("fs");
const data = fs.readFileSync("input.txt", "utf-8");

function findFinalFrequency(inputString) {
const numberArray = inputString.split("\n").map(Number);
return numberArray.reduce((sum, num) => sum + num, 0);
}

console.log(findFinalFrequency(data));
Loading