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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ node_modules
**/.DS_Store
.idea
package-lock.json
mean.js
mean.test.js
18 changes: 16 additions & 2 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,22 @@
// 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];
if (!Array.isArray(list) || !list.every((item) => typeof item === "number")) {
throw new Error("calculateMedian requires an array of numbers");
}

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

const sortedList = list.slice().sort((a, b) => a - b);

const mid = Math.floor(sortedList.length / 2);
const median =
sortedList.length % 2 !== 0
? sortedList[mid]
: (sortedList[mid - 1] + sortedList[mid]) / 2;

return median;
}

Expand Down
15 changes: 14 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
function dedupe() {}
function dedupe(items) {
if (items.length === 0) {
return [];
}

let singleOccur = [];
items.forEach((i) => {
if (!singleOccur.includes(i)) {
singleOccur.push(i);
}
});
return singleOccur;
}
module.exports = dedupe;
17 changes: 15 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,26 @@ 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", () => {
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("should return a copy of the original when the given array has no duplicates", () => {
expect(dedupe([1, 3, 8])).toEqual([1, 3, 8]);
});

// 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("should return a new array with duplicates removed and 1st occurence of each element preserved", () => {
expect(dedupe(["Mon", "Wed", "Wed", "Sun", "Tue", "Sun"])).toEqual([
"Mon",
"Wed",
"Sun",
"Tue",
]);
});
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 (err) {
return `Could not calculate a median: ${err.message}`;
}
}

module.exports = describeMedian;
18 changes: 17 additions & 1 deletion Sprint-1/implement/describe-median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,28 @@ const describeMedian = require("./describe-median.js");
// 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"');
describe("describeMedian", () => {
test('returns "The median is " followed by the median', () => {
expect(describeMedian([3, 1, 4, 1, 5])).toBe("The median is 3");
});
});

// Given an empty array
// When passed to describeMedian
// Then it should return "Could not calculate a median: calculateMedian requires a non-empty array"
test("returns error message when given an empty array", () => {
const result = describeMedian([]);
expect(result).toBe(
"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("returns error message when given a non-array", () => {
const result = describeMedian("five seven nine");
expect(result).toBe(
"Could not calculate a median: calculateMedian requires an array of numbers"
);
});
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) {
if (!Array.isArray(elements)) {
throw new Error("findMax requires an array of numbers");
}

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

if (!elements.every((element) => typeof element === "number")) {
throw new Error("findMax requires for all items to be numbers");
}

return Math.max(...elements);
}

module.exports = findMax;
34 changes: 32 additions & 2 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,59 @@ 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("should return -Infinity if given an empty array", () => {
expect(findMax([])).toEqual(-Infinity);
});

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

test("should return that number if given an array with one number", () => {
expect(findMax([2])).toEqual(2);
});

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall

test("should return the largest number overall if given positive & negative numbers", () => {
expect(findMax([-2, 4, -7, 9])).toEqual(9);
});

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero

test("should return the one closest to zero if given only negative numbers", () => {
expect(findMax([-2, -4, -7, -9])).toEqual(-2);
});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number

test("should return the largest decimal number when only decimals were given", () => {
expect(findMax([-2.5, 4.1, 3.2, 9.3])).toEqual(9.3);
});

// 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("should throw an error when at least one value is not a number", () => {
expect(() => findMax([-2, "9", "sweet", 9])).toThrow(
new Error("findMax requires for all items to be 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("should throw an error when the data type isn't an array", () => {
expect(() => findMax(["ten", 1, "thirty"])).toThrow(
new Error("findMax requires for all items to be numbers")
);
});
19 changes: 18 additions & 1 deletion Sprint-1/implement/mean.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
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");
}

if (!list.every((item) => typeof item === "number")) {
throw new Error("calculateMean requires an array of numbers");
}

let sum = list.reduce((a, b) => a + b, 0);

let meanResult = sum / list.length;
return meanResult;
}

module.exports = calculateMean;
32 changes: 31 additions & 1 deletion Sprint-1/implement/mean.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,58 @@ 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");
describe("calculatemean", () => {
it("returns the mean for [1, 2, 6]", () => {
expect(calculateMean([1, 2, 6])).toEqual(3);
});
});

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

// Given an array containing negative or decimal numbers
// When passed to calculateMean
// Then it should return the correct mean
it("returns the mean for [-1, 2.5, 6]", () => {
expect(calculateMean([-1, 2.5, 6])).toEqual(2.5);
});

// Given an empty array
// When passed to calculateMean
// Then it should throw Error("calculateMean requires a non-empty array")
it("throws 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")
it("throws when given a non-array", () => {
expect(() => calculateMean("luck")).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")
it("throws when not given any argument", () => {
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")
it("throws when given an array containing a non-number value", () => {
expect(() => calculateMean(["two", 5, "8"])).toThrow(
new Error("calculateMean requires an array of numbers")
);
});
16 changes: 16 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
function sum(elements) {
if (
!Array.isArray(elements) ||
!elements.every((item) => typeof item === "number")
) {
throw new Error("sum requires an array of numbers");
}

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

let total = 0;
for (let i = 0; i < elements.length; i++) {
total += elements[i];
}
return total;
}

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("should return the same number when given one number", () => {
expect(sum([3])).toEqual(3);
});

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
test("should return correct sum when given negative numbers", () => {
expect(sum([5, -4, 1, 9])).toEqual(11);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test("should return a correct total sum when given decimal/float numbers", () => {
expect(sum([2.2, 1, 4.1])).toEqual(7.3);
});

// 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("should throw an error when passing non-number values", () => {
expect(() => sum([8, -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("should throw an error when passing non-array data types", () => {
expect(() => sum("three", {})).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
Loading