Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
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
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 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");
}
if (!list.every((item) => typeof item === "number")) {
throw new Error("calculateMedian requires an array of numbers");
}

const sortedList = [...list].sort((a, b) => a - b);
const middleIndex = Math.floor(sortedList.length / 2);

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

module.exports = calculateMedian;
13 changes: 12 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
function dedupe() {}
function dedupe(elements) {
const dedupedArray = [];

for (value of elements) {
if (!dedupedArray.includes(value)) {
dedupedArray.push(value);
}
}
return dedupedArray;
}

module.exports = dedupe;
26 changes: 15 additions & 11 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ E.g. dedupe([1, 2, 1]) returns [1, 2]

// Acceptance Criteria:

// 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("given an array with no duplicates, it returns the copy of same array", () => {
expect(dedupe([3, 19, 53, 467])).toEqual([3, 19, 53, 467]);
});

// 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
// first occurrence of each element from the original array.
test("given an array of strings or numbers, returns the array with duplicates removed and first occurrence preserved", () => {
expect(dedupe([7, "hi", 567, "a", 7, "hi", "b"])).toEqual([
7,
"hi",
567,
"a",
"b",
]);
});
22 changes: 18 additions & 4 deletions Sprint-1/implement/describe-median.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Don't change this function.
function calculateMedian(list) {
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");
}

if (!Array.isArray(list)) {
throw new Error("calculateMedian requires an array of numbers");
}

for (const item of list) {
if (typeof item !== "number") {
throw new Error("calculateMedian requires an array of numbers");
Expand All @@ -20,6 +22,18 @@ 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 {
if (list.length === 0) {
return "Could not calculate a median: calculateMedian requires a non-empty array";
}

if (!Array.isArray(list)) {
return "Could not calculate a median: calculateMedian requires an array of numbers";
}
}
}

module.exports = describeMedian;
29 changes: 14 additions & 15 deletions Sprint-1/implement/describe-median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ 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"');

// Given an empty array
// When passed to describeMedian
// Then it should return "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 [1, 2, 3], returns "The median is 2"', () => {
expect(describeMedian([1, 2, 3])).toEqual("The median is 2");
});

test('given empty array, returns "Could not calculate a median: calculateMedian requires a non-empty array"', () => {
expect(describeMedian([])).toEqual(
"Could not calculate a median: calculateMedian requires a non-empty array"
);
});
test('given something that isnt an array of numbers, returns "Could not calculate a median: calculateMedian requires an array of numbers"', () => {
expect(describeMedian("Non-array")).toEqual(
"Could not calculate a median: calculateMedian requires an array of numbers"
);
});
20 changes: 20 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
function findMax(elements) {
if (elements.length === 0) {
return -Infinity;
}

if (
!elements.every(
(currentValue) =>
typeof currentValue === "number" && !Number.isNaN(currentValue)
)
) {
throw new Error("findMax requires an array of numbers");
}

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

sortedElements = elements.toSorted((a, b) => a - b);

return sortedElements[sortedElements.length - 1];
}

module.exports = findMax;
28 changes: 25 additions & 3 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,50 @@ 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", () => {
expect(findMax([100])).toEqual(100);
});

// 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, returns largest number overall", () => {
expect(findMax([45, -89, 167, -374, 1758, -7432])).toEqual(1758);
});

// 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, returns the closest one to zero", () => {
expect(findMax([-7, -2, -13, -121, -1345])).toEqual(-2);
});
// 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", () => {
expect(findMax([0.1, 1.2, 23.67, 0.0012, 467.89])).toEqual(467.89);
});
// 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 containing a value that isn't a number, throw Error", () => {
expect(findMax([48, 288, "hi", 576])).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 something that isn't an array, throw Error", () => {
expect(findMax("This is a string")).toThrow(
new Error("findMax requires an array of numbers")
);
});
28 changes: 27 additions & 1 deletion Sprint-1/implement/mean.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
function calculateMean(list) {}
function calculateMean(list) {
if (list.length === 0) {
throw new Error("calculateMean requires a non-empty array");
}

if (!Array.isArray(list)) {
throw new Error("calculateMean requires an array of numbers");
}

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

let total = 0;

for (number of list) {
total += number;
}
const mean = total / list.length;

return mean;
}

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

// Given an array with a single number
// When passed to calculateMean
// Then it should return that number

test("given array with single number, return that number", () => {
expect(calculateMean([81])).toEqual(81);
});
// Given an array containing negative or decimal numbers
// When passed to calculateMean
// Then it should return the correct mean

test("given an array with positive and negative numbers, return the correct mean", () => {
expect(calculateMean([3, -16, 463, -4563])).toBeCloseTo(-1028.25);
});
// Given an empty array
// When passed to calculateMean
// Then it should throw Error("calculateMean requires a non-empty array")

test("given an empty array, throw an error", () => {
expect(calculateMean([])).toThrow("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 something that isn't an array, throw an error", () => {
expect(calculateMean("This is not a string")).toThrow(
"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, throw an error", () => {
expect(calculateMean()).toThrow("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 an array with non-number values, throw an error", () => {
expect(calculateMean([3, 5, 9, "greetings", 41])).toThrow(
"calculateMean requires an array of numbers"
);
});
17 changes: 17 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
function sum(elements) {
if (
!elements.every(
(currentValue) =>
typeof currentValue === "number" && !Number.isNaN(currentValue)
)
) {
throw new Error("sum requires an array of numbers");
}
if (!Array.isArray(elements)) {
throw new Error("sum requires an array of numbers");
}

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

module.exports = sum;
51 changes: 25 additions & 26 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,28 @@ E.g. sum(['hey', 10, 'hi', 60, 10]) throws Error("sum requires an array of numbe

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

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

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

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum

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

// 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 an empty array, returns 0", () => {
expect(sum([])).toEqual(0);
});

test("given an array with one number, returns that number", () => {
expect(sum([100])).toEqual(100);
});
test("given an array with just negative numbers, returns the correct total sum", () => {
expect(sum([-7, -2, -13, -121, -1345])).toEqual(-1488);
});

test("given an array with decimal numbers, returns the correct total sum", () => {
expect(sum([0.1, 1.2, 23.67, 0.0012, 467.89])).toEqual(492.8612);
});

test("given an array containing a value that isn't a number, throw Error", () => {
expect(sum([48, 288, "hi", 576])).toThrow(
new Error("sum requires an array of numbers")
);
});
test("given something that isn't an array, throw Error", () => {
expect(sum("This is a string")).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 (element of list) {
if (element === target) {
return true;
}
Expand Down
Loading
Loading