Skip to content

Commit ff207d3

Browse files
committed
fixed code in the respective files
1 parent d4189cd commit ff207d3

12 files changed

Lines changed: 212 additions & 17 deletions

File tree

‎Sprint-1/fix/median.js‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,28 @@
66
// or contains values that aren't numbers (the function is expected to throw - see the tests).
77

88
function calculateMedian(list) {
9-
const middleIndex = Math.floor(list.length / 2);
10-
const median = list.splice(middleIndex, 1)[0];
11-
return median;
9+
if (!Array.isArray(list)) {
10+
throw new Error("calculateMedian requires an array of numbers");
11+
}
12+
13+
if (list.length === 0) {
14+
throw new Error("calculateMedian requires a non-empty array");
15+
}
16+
17+
for (const item of list) {
18+
if (typeof item !== "number") {
19+
throw new Error("calculateMedian requires an array of numbers");
20+
}
21+
}
22+
23+
const sortedList = [...list].sort((a, b) => a - b);
24+
const middleIndex = Math.floor(sortedList.length / 2);
25+
26+
if (sortedList.length % 2 === 1) {
27+
return sortedList[middleIndex];
28+
}
29+
30+
return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2;
1231
}
1332

1433
module.exports = calculateMedian;

‎Sprint-1/implement/dedupe.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
function dedupe() {}
1+
function dedupe(values) {
2+
return [...new Set(values)];
3+
}
4+
5+
module.exports = dedupe;

‎Sprint-1/implement/dedupe.test.js‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,37 @@ E.g. dedupe([1, 2, 1]) returns [1, 2]
1616
// Given an empty array
1717
// When passed to the dedupe function
1818
// Then it should return an empty array
19-
test.todo("given an empty array, it returns an empty array");
19+
it("returns an empty array when given an empty array", () => {
20+
expect(dedupe([])).toEqual([]);
21+
});
2022

2123
// Given an array with no duplicates
2224
// When passed to the dedupe function
2325
// Then it should return a copy of the original array
26+
it("returns a copy of an array with no duplicates", () => {
27+
const values = [1, "a", 2, "b"];
28+
const result = dedupe(values);
29+
30+
expect(result).toEqual(values);
31+
expect(result).not.toBe(values);
32+
});
2433

2534
// Given an array of strings or numbers
2635
// When passed to the dedupe function
27-
// Then it should return a new array with duplicates removed while preserving the
36+
// Then it should return a new array with duplicates removed while preserving the
2837
// first occurrence of each element from the original array.
38+
it("removes duplicate numbers while preserving their first occurrences", () => {
39+
const values = [5, 1, 1, 2, 3, 2, 5, 8];
40+
const result = dedupe(values);
41+
42+
expect(result).toEqual([5, 1, 2, 3, 8]);
43+
expect(result).not.toBe(values);
44+
});
45+
46+
it("removes duplicate strings while preserving their first occurrences", () => {
47+
const values = ["a", "b", "a", "c", "b"];
48+
const result = dedupe(values);
49+
50+
expect(result).toEqual(["a", "b", "c"]);
51+
expect(result).not.toBe(values);
52+
});

‎Sprint-1/implement/describe-median.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ function calculateMedian(list) {
2020
}
2121

2222
// Implement this function. See describe-median.test.js for the acceptance criteria.
23-
function describeMedian(list) {}
23+
function describeMedian(list) {
24+
try {
25+
const median = calculateMedian(list);
26+
return `The median is ${median}`;
27+
} catch (error) {
28+
return `could not calculate a median: ${error.message}`;
29+
}
30+
}
2431

2532
module.exports = describeMedian;

‎Sprint-1/implement/describe-median.test.js‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,32 @@ const describeMedian = require("./describe-median.js");
2020
// Given an array of numbers
2121
// When passed to describeMedian
2222
// Then it should return "The median is " followed by the median
23-
// Delete this test.todo and replace it with a test.
24-
test.todo('given [1, 2, 3], returns "The median is 2"');
23+
it("returns the median for [1, 2, 3]", () => {
24+
expect(describeMedian([1, 2, 3])).toEqual("The median is 2");
25+
});
2526

2627
// Given an empty array
2728
// When passed to describeMedian
2829
// Then it should return "Could not calculate a median: calculateMedian requires a non-empty array"
30+
it("returns an error description when given an empty array", () => {
31+
try {
32+
expect(describeMedian([])).toBe(
33+
"Could not calculate a median: calculateMedian requires a non-empty array"
34+
);
35+
} catch (error) {
36+
throw new Error(`Empty-array case failed: ${error.message}`);
37+
}
38+
});
2939

3040
// Given something that isn't an array of numbers, e.g. "banana"
3141
// When passed to describeMedian
3242
// Then it should return "Could not calculate a median: calculateMedian requires an array of numbers"
43+
it("returns an error description for an array of strings", () => {
44+
try {
45+
expect(describeMedian(["banana", "apple", "orange"])).toBe(
46+
"Could not calculate a median: calculateMedian requires an array of numbers"
47+
);
48+
} catch (error) {
49+
throw new Error(`String-array case failed: ${error.message}`);
50+
}
51+
});

‎Sprint-1/implement/max.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
function findMax(elements) {
2+
if (
3+
!Array.isArray(elements) ||
4+
elements.some((element) => typeof element !== "number")
5+
) {
6+
throw new Error("findMax requires an array of numbers");
7+
}
8+
return elements.reduce(
9+
(max, element) => (element > max ? element : max),
10+
-Infinity
11+
);
212
}
313

414
module.exports = findMax;

‎Sprint-1/implement/max.test.js‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,71 @@ const findMax = require("./max.js");
1515
// Given an empty array
1616
// When passed to the max function
1717
// Then it should return -Infinity
18-
// Delete this test.todo and replace it with a test.
19-
test.todo("given an empty array, returns -Infinity");
18+
// test.todo("given an empty array, returns -Infinity");
19+
it("returns -Infinity for an empty array", () => {
20+
expect(findMax([])).toBe(-Infinity);
21+
});
2022

2123
// Given an array with one number
2224
// When passed to the max function
2325
// Then it should return that number
26+
it("returns 10 for a single positive number", () => {
27+
expect(findMax([10])).toBe(10);
28+
});
29+
30+
it("returns the largest value in a positive-number array", () => {
31+
expect(findMax([10, 20, 5])).toBe(20);
32+
});
2433

2534
// Given an array with both positive and negative numbers
2635
// When passed to the max function
2736
// Then it should return the largest number overall
37+
it("returns 200 for an array of positive and negative numbers", () => {
38+
expect(findMax([10, -10, 200, -15])).toEqual(200);
39+
});
40+
41+
it("returns Infinity when Infinity is the largest value", () => {
42+
expect(findMax([10, Infinity, -15])).toBe(Infinity);
43+
});
2844

2945
// Given an array with just negative numbers
3046
// When passed to the max function
3147
// Then it should return the closest one to zero
48+
it("returns -1 for an array of negative numbers", () => {
49+
expect(findMax([-1, -10, -200, -15, -30])).toEqual(-1);
50+
});
51+
52+
it("returns the number for a single negative value", () => {
53+
expect(findMax([-10])).toBe(-10);
54+
});
3255

3356
// Given an array with decimal numbers
3457
// When passed to the max function
3558
// Then it should return the largest decimal number
59+
it("returns 200.99 for an array of decimal numbers", () => {
60+
expect(findMax([1.5, 2.5, 200.99, 15.01, 30.1])).toBe(200.99);
61+
});
3662

3763
// Given an array containing a value that isn't a number
3864
// When passed to the max function
3965
// Then it should throw Error("findMax requires an array of numbers")
66+
it("throws for an array of strings", () => {
67+
expect(() => findMax(["Hello", "There", "Weekend"])).toThrow(
68+
"findMax requires an array of numbers"
69+
);
70+
});
71+
72+
it("throws for an array mixing strings and numbers", () => {
73+
expect(() => findMax(["Hello", 10, 20])).toThrow(
74+
"findMax requires an array of numbers"
75+
);
76+
});
4077

4178
// Given something that isn't an array at all, such as "hey", 42 or no argument
4279
// When passed to the max function
4380
// Then it should throw Error("findMax requires an array of numbers")
81+
it("throws when the input is not an array", () => {
82+
expect(() => findMax("Hello")).toThrow(
83+
"findMax requires an array of numbers"
84+
);
85+
});

‎Sprint-1/implement/mean.js‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1-
function calculateMean(list) {}
1+
function calculateMean(list) {
2+
if (!Array.isArray(list)) {
3+
throw new Error("calculateMean requires an array of numbers");
4+
}
5+
6+
if (list.length === 0) {
7+
throw new Error("calculateMean requires a non-empty array");
8+
}
9+
10+
for (const item of list) {
11+
if (typeof item !== "number") {
12+
throw new Error("calculateMean requires an array of numbers");
13+
}
14+
}
15+
16+
const sum = list.reduce((a, b) => a + b, 0);
17+
18+
return sum / list.length;
19+
}
220

321
module.exports = calculateMean;

‎Sprint-1/implement/mean.test.js‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,56 @@ const calculateMean = require("./mean.js");
2222
// Given an array of numbers
2323
// When passed to calculateMean
2424
// Then it should return their mean
25-
// Delete this test.todo and replace it with a test.
26-
test.todo("given [1, 2, 6], returns 3");
25+
it("returns the mean in a positive-number array", () => {
26+
expect(calculateMean([1, 2, 6])).toBe(3);
27+
});
2728

2829
// Given an array with a single number
2930
// When passed to calculateMean
3031
// Then it should return that number
32+
it("returns 10 for a single positive number", () => {
33+
expect(calculateMean([10])).toBe(10);
34+
});
3135

3236
// Given an array containing negative or decimal numbers
3337
// When passed to calculateMean
3438
// Then it should return the correct mean
39+
it("returns -3 for an array of negative numbers", () => {
40+
expect(calculateMean([-1, -2, -6])).toEqual(-3);
41+
});
3542

3643
// Given an empty array
3744
// When passed to calculateMean
3845
// Then it should throw Error("calculateMean requires a non-empty array")
46+
it("throws for an empty array", () => {
47+
expect(() => calculateMean([])).toThrow(
48+
"calculateMean requires a non-empty array"
49+
);
50+
});
3951

4052
// Given a value that isn't an array, e.g. "banana", 42, null or {}
4153
// When passed to calculateMean
4254
// Then it should throw Error("calculateMean requires an array of numbers")
55+
it("throws when the input is not an array", () => {
56+
expect(() => calculateMean("Hello")).toThrow(
57+
"calculateMean requires an array of numbers"
58+
);
59+
});
4360

4461
// Given no argument at all
4562
// When passed to calculateMean
4663
// Then it should throw Error("calculateMean requires an array of numbers")
64+
it("throws when the input has no argument", () => {
65+
expect(() => calculateMean("")).toThrow(
66+
"calculateMean requires an array of numbers"
67+
);
68+
});
4769

4870
// Given an array containing a non-number value, e.g. [1, "2", 3]
4971
// When passed to calculateMean
5072
// Then it should throw Error("calculateMean requires an array of numbers")
73+
it("throws for an array containing a non-number value", () => {
74+
expect(() => calculateMean(["2", 10, 20])).toThrow(
75+
"calculateMean requires an array of numbers"
76+
);
77+
});

‎Sprint-1/implement/sum.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
function sum(elements) {
2+
if (!Array.isArray(elements)) {
3+
throw new Error("sum requires an array of numbers");
4+
}
5+
6+
if (elements.some((element) => typeof element !== "number")) {
7+
throw new Error("sum requires an array of numbers");
8+
}
9+
10+
return elements.reduce((total, element) => total + element, 0);
211
}
312

413
module.exports = sum;

0 commit comments

Comments
 (0)