Skip to content
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
28 changes: 25 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,31 @@
// 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;
// 1 check if input is valid non-empty array
if (!Array.isArray(list) || list.length === 0) {
return null;
}

// 2 filter to keep only numeric values
const numbersOnly = list.filter((item) => typeof item === "number");

// 3 return null if no numbers remain after filtering
if (numbersOnly.length === 0) {
return null;
}

// 4 create a copy and sort it numerically
const sorted = [...numbersOnly].sort((a, b) => a - b);

const len = sorted.length;
const mid = Math.floor(len / 2);

// 5 calculate median based on parity
if (len % 2 !== 0) {
return sorted[mid];
} else {
return (sorted[mid - 1] + sorted[mid]) / 2;
}
}

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

// 2. Set to remove duplicates
// spread Set back into a new array [...]
return [...new Set(list)];
}
module.exports = dedupe;

11 changes: 11 additions & 0 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2]
// Then it should return an empty array
test.todo("given an empty array, it returns an empty array");

test("returns an empty array for empty input", () => {
expect(dedupe([])).toEqual([]);
});

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

test("preserves the first occurrence", () => {
expect(dedupe(["first", "second", "first"])).toEqual(["first", "second" ]);
});
// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
Expand Down
14 changes: 13 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
function findMax(elements) {
function findMax(list) {
// 1. array check
if (!Array.isArray(list)) return null;

// 2. cleaning
const numbersOnly = list.filter(item => typeof item === `number`);

// 3. the -Infinity rule (for empty or non-numeric arrays)
if (numbersOnly.length === 0) return -Infinity;

// 4. math magic
return Math.max(...numbersOnly);

}

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

test("given an array with one number, returns that number", () => {
expect(findMax([42])).toBe(42);
});

test("returns the largest number (closest to zero) for negative numbers", () => {
expect(findMax([-21, -5, -121])).toBe(-5);
});

test("ignores non-number values", () => {
expect(findMax(["apple", 10, null, 50, "orange"])).toBe(50);
});

// Given an array with one number
// When passed to the max function
Expand Down
12 changes: 11 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
function sum(elements) {
function sum(list) {
// 1. array valid
if (!Array.isArray(list)) return 0;

// 2. filter and add them up
// acc = accumulator (sum so far), curr = current value
return list
.filter((item) => typeof item === "number")
.reduce((acc, curr) => acc + curr, 0);
// 0 is the starting point.
// If array empty after filter, returns 0.
}

module.exports = sum;
15 changes: 14 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,21 @@ 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);
});

test("sums up positive, negative and decimal numbers", () => {
expect(sum([10, -5, 2.5])).toBe(7.5);
});

test("ignores non-numeric values", () => {
expect(sum(["apple", 11, null, 21,])).toBe(32)
});

test("returns 0 for an array with only non-numbers", () => {
expect(sum(["a", "b"])).toBe(0);
});
// Given an array with just one number
// When passed to the sum function
// Then it should return that number
Expand Down
10 changes: 6 additions & 4 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// 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];
// don't need 'index' or 'list.length' anymore
// just say: "for every item of the list..."
for (const element of list) {
if (element === target) {
return true;
return true; // Found it! Stop and return
}
}
return false;

return false; // Checked everything, found nothing
}

module.exports = includes;
15 changes: 15 additions & 0 deletions Sprint-1/stretch/aoc-2018-day1/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const fs = require("fs");

// read file from current direct using UTF-8 encoding
const input = fs.readFileSync("./input.txt", "utf8");

const changes = input
.split("\n") // split string into array by new lines
.filter((line) => line.trim() !== "") // remove empty string or space only lines
.map(Number); // convert string to values into numbers

// calculate total frequency
const result = changes.reduce((total, change) => total + change, 0);

console.log("Resulting frequency:", result);