Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
5ce7d15
Add if condition to check list is odd or even.
russom-g Nov 3, 2026
8f51f4c
Add sort method to to sort list.
russom-g Nov 3, 2026
d68c02b
Add if condition to check empty array.
russom-g Nov 3, 2026
f65276c
Write test cases for testing empty and no duplicate arrays
russom-g Nov 3, 2026
6745e82
Implement function to check for empty and duplicated arrays.
russom-g Nov 3, 2026
20d911a
Write a test for testing original occurrence returned
russom-g Nov 3, 2026
6f7f6d4
Fix bug where item pushed instead of list
russom-g Nov 3, 2026
a922943
Format code with prettier.
russom-g Nov 3, 2026
2491759
Implement try catch function to return error messages.
russom-g Nov 4, 2026
697d23f
Write test cases for multiple arrays
russom-g Nov 4, 2026
33ffd60
Write test case for findMax with empty array
russom-g Nov 5, 2026
b471137
Write test case for findMax with one number array
russom-g Nov 5, 2026
434ca4b
Implement function for checking array the largest element
russom-g Nov 5, 2026
d997797
Add for and if condition for checking non-array and non-number elements
russom-g Nov 5, 2026
734381d
Write test cases for finMax with different input.
russom-g Nov 5, 2026
215a119
Write test case for calculateMean with array of numbers.
russom-g Nov 6, 2026
c12fb12
implement function to calculate mean of list.
russom-g Nov 6, 2026
31ba3f1
Write test case for calculateMean with a single number array
russom-g Nov 6, 2026
eb978b1
Write test case for calculateMean with negative or decimal numbers.
russom-g Nov 6, 2026
9a8945a
Add Number method to round mean to 2 decimal place
russom-g Nov 6, 2026
68b854a
Write test for calculateMean with empty array.
russom-g Nov 6, 2026
1f53630
Add if condition for checking empty array.
russom-g Nov 6, 2026
d0b0c30
Write test case for calculateMean with no argument
russom-g Nov 6, 2026
c0cc6b7
Add if condition for checking non array.
russom-g Nov 6, 2026
5da7996
Add if condition for checking non-number array.
russom-g Nov 6, 2026
b772767
Write test case for calculateMean with non-number array
russom-g Nov 6, 2026
1374324
Format code with prettier
russom-g Nov 7, 2026
2b9d397
Implement function for calculating sum.
russom-g Nov 7, 2026
1c95b32
Write test case for sum with an empty array.
russom-g Nov 7, 2026
2f02e30
Write test case for sum with one number array.
russom-g Nov 7, 2026
1735942
Add if condition to check array is number.
russom-g Nov 7, 2026
351f97b
Write test case for sum with empty array
russom-g Nov 7, 2026
7bb3a8f
Add if condition for checking non-array elements.
russom-g Nov 7, 2026
15e8f9f
Write test case for sum with non-array element.
russom-g Nov 7, 2026
6c1939d
Refactor includes with for...of loops
russom-g Nov 7, 2026
db24d03
Implement function read .txt file and sum up the result and returns
russom-g Nov 8, 2026
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: 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 contains values that aren't numbers (the function is expected to throw - see the tests).

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
if (!Array.isArray(list)) {
throw new Error("calculateMedian requires an array of numbers");
}

if (list.length === 0) {
throw new Error("calculateMedian requires a non-empty array");
}

for (const item of list) {
if (typeof item !== "number") {
throw new Error("calculateMedian requires an array of numbers");
}
}
const sorted = [...list].sort((a, b) => a - b);
const middleIndex = Math.floor(sorted.length / 2);
if (sorted.length % 2 === 0) {
return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2;
}
return sorted[middleIndex];
}

module.exports = calculateMedian;
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(list) {
const elements = [];

if (list.length === 0) {
return [];
}

for (const item of list) {
if (!elements.includes(item)) {
elements.push(item);
}
}
return elements;
}

module.exports = dedupe;
18 changes: 16 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,27 @@ E.g. dedupe([1, 2, 1]) returns [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("given an empty array, it returns an empty array", () => {
const list = [];
const result = dedupe(list);
expect(dedupe(list)).toEqual(result);
});

// 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, it returns a copy of the original array", () => {
const list = ["a", "b", "c"];
const result = dedupe(list);
expect(dedupe(list)).toEqual(result);
});

// 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.
test("given an array, it returns an array with duplicates removed and its original occurrence", () => {
const list = [3, 1, 2];
const result = dedupe(list);
expect(dedupe(list)).toEqual(result);
});
8 changes: 7 additions & 1 deletion Sprint-1/implement/describe-median.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ function calculateMedian(list) {
}

// Implement this function. See describe-median.test.js for the acceptance criteria.
function describeMedian(list) {}
function describeMedian(list) {
try {
return `The median is ${calculateMedian(list)}`;
} catch (error) {
return `Could not calculate a median: calculateMedian requires a non-empty array: ${error.message}`;
}
}

module.exports = describeMedian;
17 changes: 16 additions & 1 deletion Sprint-1/implement/describe-median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,33 @@ message, so the caller can see what went wrong.

const describeMedian = require("./describe-median.js");


// Acceptance criteria:

// Given an array of numbers
// When passed to describeMedian
// Then it should return "The median is " followed by the median
// Delete this test.todo and replace it with a test.
test.todo('given [1, 2, 3], returns "The median is 2"');
test("given [1, 2, 3], returns The median is 2", () => {
const list = [1, 2, 3];
const result = describeMedian(list);
expect(describeMedian(list)).toEqual(result);
});

// Given an empty array
// When passed to describeMedian
// Then it should return "Could not calculate a median: calculateMedian requires a non-empty array"
test("given an empty array [], returns Could not calculate a median", () => {
const list = [];
const result = describeMedian(list);
expect(describeMedian(list)).toEqual(result);
});

// Given something that isn't an array of numbers, e.g. "banana"
// When passed to describeMedian
// Then it should return "Could not calculate a median: calculateMedian requires an array of numbers"
test("given a non-array , returns Could not calculate a median", () => {
const list = [];
const result = describeMedian(list);
expect(describeMedian(list)).toEqual(result);
});
14 changes: 14 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
function findMax(elements) {
if (!Array.isArray(elements)) {
throw new Error("findMax requires an array of numbers");
}

for (const element of elements) {
if (typeof element !== "number") {
throw new Error("findMax requires an array of numbers");
}

if (!elements.length === 1) {
return element;
}
}
return Math.max(...elements);
}

module.exports = findMax;
34 changes: 33 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,60 @@ 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([])).toEqual(-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", () => {
const list = [1];
const result = findMax(list);
expect(result).toEqual(1);
});

// 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 positive and negative numbers, returns the largest number", () => {
const list = [1, -9, 7, -5];
const result = findMax(list);
expect(result).toEqual(7);
});

// 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 negative numbers, returns closest one to zero", () => {
const list = [-1, -9, -7, -5];
const result = findMax(list);
expect(result).toEqual(-1);
});

// 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", () => {
const list = [1.1, 0.9, 4.7, 1.5];
const result = findMax(list);
expect(result).toEqual(4.7);
});

// Given an array containing a value that isn't a number
// When passed to the max function
// Then it should throw Error("findMax requires an array of numbers")
test("given an array with non-numbers, returns, findMax requires an array of numbers", () => {
expect(() => findMax(["a", "l", "k"])).toThrow(
new Error("findMax requires an array of numbers")
);
});

// Given something that isn't an array at all, such as "hey", 42 or no argument
// When passed to the max function
// Then it should throw Error("findMax requires an array of numbers")
test("given a non-array, returns, findMax requires an array of numbers", () => {
expect(() => findMax("a", "", 7, "kilo")).toThrow(
new Error("findMax requires an array of numbers")
);
});
17 changes: 16 additions & 1 deletion Sprint-1/implement/mean.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
function calculateMean(list) {}
function calculateMean(list) {
if (!Array.isArray(list)) {
throw new Error("calculateMean requires an array of numbers");
}
if (list.length === 0) {
throw new Error("calculateMean requires a non-empty array");
}

let total = 0;
for (const item of list) {
if (typeof item !== "number") {
throw new Error("calculateMean requires an array of numbers");
}
total = total + item;
}
return Number(total.toFixed(2)) / list.length;
}
module.exports = calculateMean;
40 changes: 39 additions & 1 deletion Sprint-1/implement/mean.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,66 @@ const calculateMean = require("./mean.js");
// When passed to calculateMean
// Then it should return their mean
// Delete this test.todo and replace it with a test.
test.todo("given [1, 2, 6], returns 3");
test("given [1, 2, 6], returns 3", () => {
const list = [1, 2, 6];
const result = calculateMean(list);
expect(result).toEqual(3);
});

// Given an array with a single number
// When passed to calculateMean
// Then it should return that number
test("given [6], returns 6", () => {
expect(6).toEqual(6);
});

// Given an array containing negative or decimal numbers
// When passed to calculateMean
// Then it should return the correct mean
test("given negative numbers, return mean", () => {
const list = [-7, -3, -5];
const result = calculateMean(list);
expect(result).toEqual(-5);
});

test("given decimal numbers, return mean", () => {
const list = [7.2, 2.4, 6.8];
const result = calculateMean(list);
expect(result).toBeCloseTo(5.47);
});

// Given an empty array
// When passed to calculateMean
// Then it should throw Error("calculateMean requires a non-empty array")
test("given an empty array, returns, calculateMean requires a non-empty array", () => {
expect(() => calculateMean([])).toThrow(
new Error("calculateMean requires a non-empty array")
);
});

// Given a value that isn't an array, e.g. "banana", 42, null or {}
// When passed to calculateMean
// Then it should throw Error("calculateMean requires an array of numbers")
test("given a non-array, returns, calculateMean requires an array of numbers", () => {
expect(() => calculateMean("banana", 42, null, {})).toThrow(
new Error("calculateMean requires an array of numbers")
);
});

// Given no argument at all
// When passed to calculateMean
// Then it should throw Error("calculateMean requires an array of numbers")
test("given no argument, returns, calculateMean requires an array of numbers", () => {
expect(() => calculateMean()).toThrow(
new Error("calculateMean requires an array of numbers")
);
});

// Given an array containing a non-number value, e.g. [1, "2", 3]
// When passed to calculateMean
// Then it should throw Error("calculateMean requires an array of numbers")
test("given non-number containing array, returns, calculateMean requires an array of numbers", () => {
expect(() => calculateMean([1, "2", 3])).toThrow(
new Error("calculateMean requires an array of numbers")
);
});
11 changes: 11 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
function sum(elements) {
if (!Array.isArray(elements)) {
throw new Error("sum requires an array of numbers");
}
let total = 0;
for (const element of elements) {
if (typeof element !== "number") {
throw new Error("sum requires an array of numbers");
}
total += element;
}
return Number(total.toFixed(2));
}

module.exports = sum;
23 changes: 22 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,45 @@ 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([])).toEqual(0);
});

// Given an array with just one number
// When passed to the sum function
// Then it should return that number
test("given a one number array, returns the same number", () => {
expect(sum([9])).toEqual(9);
});

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
test("given a negative number array, returns the sum", () => {
expect(sum([-9, -3, -6])).toEqual(-18);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test("given a decimal numbers array, returns the sum", () => {
expect(sum([1.9, 7.5, 4.91])).toBeCloseTo(14.31);
});

// Given an array containing a value that isn't a number
// When passed to the sum function
// Then it should throw Error("sum requires an array of numbers")
test("given a non-numbers array, returns sum requires an array of numbers", () => {
expect(() => sum(["apple", null ,3])).toThrow(
new Error("sum requires an array of numbers")
);
});

// Given something that isn't an array at all, such as "hey", 42 or no argument
// When passed to the sum function
// Then it should throw Error("sum requires an array of numbers")
test("given a non-array, returns sum requires an array of numbers", () => {
expect(() => sum("apple", null, 3)).toThrow(
new Error("sum requires an array of numbers")
);
});
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
14 changes: 14 additions & 0 deletions Sprint-1/stretch/aoc-2018-day1/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fs = require("fs");
const inputText = fs.readFileSync("input.txt", "utf8");

function calculateResultingFrequency(frequencyChangeLines) {
const lines = frequencyChangeLines.split("\n");
const frequencyChangeAsNumbers = lines.map((change) => Number(change));
let resultingFrequency = 0;

for (const frequencyChange of frequencyChangeAsNumbers) {
resultingFrequency += frequencyChange;
}
return resultingFrequency;
}
console.log(calculateResultingFrequency(inputText));
Loading