Skip to content
25 changes: 22 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,28 @@
// 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.slice().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;
6 changes: 5 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
function dedupe() {}
function dedupe(arr) {
return arr.filter((item, index) => arr.indexOf(item) === index);
}

module.exports = dedupe;
28 changes: 26 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,37 @@ 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, should return an empty array", () => {
expect(dedupe([])).toEqual([]);
});

// 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, should return a copy of the original array", () => {
const original = [5, 1, 2, 3, 8];
const result = dedupe(original);

expect(result).toEqual([5, 1, 2, 3, 8]);
expect(result).not.toBe(original);
});
Comment thread
cjyuan marked this conversation as resolved.
Resolved

// 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 of strings, removes duplicates and keeps first occurrence", () => {
expect(dedupe(["car", "plane", "car", "train", "plane"])).toEqual([
"car",
"plane",
"train",
]);
});

test("Given an array of numbers, removes duplicates and keeps first occurrence", () => {
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
});

test("Given [1, 2, 1], returns [1, 2]", () => {
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
});
9 changes: 8 additions & 1 deletion Sprint-1/implement/describe-median.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ function calculateMedian(list) {
}

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

module.exports = describeMedian;
22 changes: 20 additions & 2 deletions Sprint-1/implement/describe-median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,31 @@ const describeMedian = require("./describe-median.js");
// 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 an array of numbers, returns "The median is " followed by the median', () => {
expect(describeMedian([5, 6, 7])).toEqual("The median is 6");
});

// 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 message", () => {
expect(describeMedian([])).toEqual(
"Could not calculate a median: calculateMedian requires a non-empty array"
);
});

// 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 non-numbers, returns could-not-calculate message", () => {
expect(describeMedian(["banana"])).toEqual(
"Could not calculate a median: calculateMedian requires an array of numbers"
);
});

test("Given a string, returns could-not-calculate message", () => {
expect(describeMedian("banana")).toEqual(
"Could not calculate a median: calculateMedian requires an array of numbers"
);
});

22 changes: 22 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@

function findMax(elements) {
if (!Array.isArray(elements)) {
throw new Error("findMax requires an array of numbers");
}

for (const item of elements) {
if (typeof item !== "number" || Number.isNaN(item)) {
throw new Error("findMax requires an array of numbers");
}
}

if (elements.length === 0) {
return -Infinity;
}

let max = elements[0];
for (const item of elements) {
if (item > max) {
max = item;
}
}
return max;
}

module.exports = findMax;
46 changes: 43 additions & 3 deletions Sprint-1/implement/max.test.js
Comment thread
cjyuan marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,69 @@ 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");
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, should return thant 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
test("Given an array with both positive and negative numbers, should return the largest number overall", () => {
expect(findMax([30, -7, 1, 45, -70, 10, -10])).toEqual(45);
});

// 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, should return the closest one to zero", () => {
expect(findMax([-15, -10, -7, -20, -70])).toEqual(-7);
});

// 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, should return the largest decimal number", () => {
expect(findMax([3.5, 2.7, 1.9, 5.1])).toEqual(5.1);
});

// 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("Throws an error when array contains a value that isn't a number", () => {
expect(() => findMax(["plane", 10, "car", 60, 10])).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("Throws an error when the input is not an array at all", () => {
expect(() => findMax("hey")).toThrow(
new Error("findMax requires an array of numbers")
);
});

test("Throws an error when given a number", () => {
expect(() => findMax(36)).toThrow(
new Error("findMax requires an array of numbers")
);
});

test("Throws an error when called with no argument", () => {
expect(() => findMax()).toThrow(
new Error("findMax requires an array of numbers")
);
});

test("Throws an error when array contains NaN", () => {
expect(() => findMax([1, NaN, 3])).toThrow(
new Error("findMax requires an array of numbers")
);
});
23 changes: 22 additions & 1 deletion Sprint-1/implement/mean.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
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");
}

for (const item of list) {
if (typeof item !== "number" || !Number.isFinite(item)) {
throw new Error("calculateMean requires an array of numbers");
}
}

let sum = 0;
for (const num of list) {
sum += num;
}
return sum / list.length;
}

module.exports = calculateMean;

52 changes: 50 additions & 2 deletions Sprint-1/implement/mean.test.js
Comment thread
cjyuan marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,77 @@ const calculateMean = require("./mean.js");
// Given an array of numbers
// 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 an array of numbers, should return their mean", () => {
expect(calculateMean([3, 7, 50,40])).toEqual(25);
});

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

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

test("Given an array containing decimal numbers, should return the correct mean", () => {
expect(calculateMean([2.2, 1.9, 3.5, 4.1])).toBeCloseTo(2.925);
});

test("Given an array containing both negative and decimal numbers, should return the correct mean", () => {
expect(calculateMean([2.2, -1.9, 3.5, -4.1])).toBeCloseTo(-0.075);
});
Comment thread
cjyuan marked this conversation as resolved.
Resolved

// Given an empty array
// When passed to calculateMean
// Then it should throw Error("calculateMean requires a non-empty array")
test("Throws an error when given an 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("Throws an error when given a value that isn't an array", () => {
expect(() => calculateMean(["banana"])).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("Throws an error when given no argument at all", () => {
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("Throws an error when given an array containing a non-number value", () => {
expect(() => calculateMean([1, "2", 3])).toThrow(
new Error("calculateMean requires an array of numbers")
);
});

test("Throws an error when array contains Infinity or -Infinity", () => {
expect(() => calculateMean([Infinity, 0, -Infinity])).toThrow(
new Error("calculateMean requires an array of numbers")
);
});

test("Throws an error when array contains NaN", () => {
expect(() => calculateMean([0, NaN])).toThrow(
new Error("calculateMean requires an array of numbers")
);
});
15 changes: 15 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
function sum(elements) {
if (!Array.isArray(elements)) {
throw new Error("sum requires an array of numbers");
}

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

let total = 0;
for (const item of elements) {
total += item;
}
return total;
}

module.exports = sum;
Loading
Loading