Skip to content

Commit 8e5fb57

Browse files
committed
Fix revert files from Sprint 1
1 parent 9cf496b commit 8e5fb57

7 files changed

Lines changed: 8 additions & 82 deletions

File tree

‎Sprint-1/fix/median.js‎

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,9 @@
66
// or 'list' has mixed values (the function is expected to sort only numbers).
77

88
function calculateMedian(list) {
9-
// Validate input: must be an array
10-
if (!Array.isArray(list)) {
11-
return null;
12-
}
13-
14-
// Filter out non-numeric values
15-
const numbers = list.filter((item) => typeof item === "number");
16-
17-
// If no numeric values, return null
18-
if (numbers.length === 0) {
19-
return null;
20-
}
21-
22-
// Sort numbers without mutating original list
23-
const sorted = [...numbers].sort((a, b) => a - b);
24-
25-
const middleIndex = Math.floor(sorted.length / 2);
26-
27-
// Odd length → return middle number
28-
if (sorted.length % 2 !== 0) {
29-
return sorted[middleIndex];
30-
}
31-
32-
// Even length → average of two middle numbers
33-
return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2;
9+
const middleIndex = Math.floor(list.length / 2);
10+
const median = list.splice(middleIndex, 1)[0];
11+
return median;
3412
}
3513

3614
module.exports = calculateMedian;

‎Sprint-1/implement/dedupe.js‎

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

‎Sprint-1/implement/max.js‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
function findMax(elements) {
2-
const numbers = elements.filter((item) => typeof item === "number");
3-
if (numbers.length === 0) {
4-
return -Infinity;
5-
}
6-
return Math.max(...numbers);
72
}
83

94
module.exports = findMax;

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

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,28 @@ const findMax = require("./max.js");
1616
// When passed to the max function
1717
// Then it should return -Infinity
1818
// Delete this test.todo and replace it with a test.
19-
//test.todo("given an empty array, returns -Infinity");
20-
test("given an empty array, returns -Infinity", () => {
21-
expect(findMax([])).toBe(-Infinity);
22-
});
19+
test.todo("given an empty array, returns -Infinity");
2320

2421
// Given an array with one number
2522
// When passed to the max function
2623
// Then it should return that number
27-
test("given an array with one number, returns that number", () => {
28-
expect(findMax([42])).toBe(42);
29-
});
3024

3125
// Given an array with both positive and negative numbers
3226
// When passed to the max function
3327
// Then it should return the largest number overall
34-
test("given an array with positive and negative numbers, returns the largest", () => {
35-
expect(findMax([-10, 5, 3, -2])).toBe(5);
36-
});
3728

3829
// Given an array with just negative numbers
3930
// When passed to the max function
4031
// Then it should return the closest one to zero
41-
test("given an array with only negative numbers, returns the closest to zero", () => {
42-
expect(findMax([-10, -3, -20])).toBe(-3);
43-
});
4432

4533
// Given an array with decimal numbers
4634
// When passed to the max function
4735
// Then it should return the largest decimal number
48-
test("given an array with decimal numbers, returns the largest decimal", () => {
49-
expect(findMax([1.2, 3.5, 2.8])).toBe(3.5);
50-
});
5136

5237
// Given an array with non-number values
5338
// When passed to the max function
5439
// Then it should return the max and ignore non-numeric values
55-
test("given an array with non-number values, ignores them and returns the max", () => {
56-
expect(findMax(['hey', 10, 'hi', 60, 10])).toBe(60);
57-
});
5840

5941
// Given an array with only non-number values
6042
// When passed to the max function
6143
// Then it should return the least surprising value given how it behaves for all other inputs
62-
test("given an array with only non-number values, returns -Infinity", () => {
63-
expect(findMax(['a', 'b', null, undefined])).toBe(-Infinity);
64-
});

‎Sprint-1/implement/sum.js‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
function sum(elements) {
2-
const numbers = elements.filter((item) => typeof item === "number");
3-
if (numbers.length === 0) {
4-
return 0;
5-
}
6-
return numbers.reduce((total, num) => total + num, 0);
72
}
83

94
module.exports = sum;

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,24 @@ const sum = require("./sum.js");
1313
// Given an empty array
1414
// When passed to the sum function
1515
// Then it should return 0
16-
// test.todo("given an empty array, returns 0")
17-
test("given an empty array, returns 0", () => {
18-
expect(sum([])).toBe(0);
19-
});
16+
test.todo("given an empty array, returns 0")
2017

2118
// Given an array with just one number
2219
// When passed to the sum function
2320
// Then it should return that number
24-
test("given an array with one number, returns that number", () => {
25-
expect(sum([42])).toBe(42);
26-
});
2721

2822
// Given an array containing negative numbers
2923
// When passed to the sum function
3024
// Then it should still return the correct total sum
31-
test("given an array with negative numbers, returns the correct total", () => {
32-
expect(sum([-5, 10, -3])).toBe(2);
33-
});
3425

3526
// Given an array with decimal/float numbers
3627
// When passed to the sum function
3728
// Then it should return the correct total sum
38-
test("given an array with decimal numbers, returns the correct total", () => {
39-
expect(sum([1.5, 2.5, 3.1])).toBe(7.1);
40-
});
4129

4230
// Given an array containing non-number values
4331
// When passed to the sum function
4432
// Then it should ignore the non-numerical values and return the sum of the numerical elements
45-
test("given an array with non-number values, ignores them and returns the sum of numbers", () => {
46-
expect(sum(["hey", 10, "hi", 60, 10])).toBe(80);
47-
});
4833

4934
// Given an array with only non-number values
5035
// When passed to the sum function
5136
// Then it should return the least surprising value given how it behaves for all other inputs
52-
test("given an array with only non-number values, returns 0", () => {
53-
expect(sum(["a", null, undefined, "b"])).toBe(0);
54-
});

‎Sprint-1/refactor/includes.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Refactor the implementation of includes to use a for...of loop
22

33
function includes(list, target) {
4-
for (const element of list) {
4+
for (let index = 0; index < list.length; index++) {
5+
const element = list[index];
56
if (element === target) {
67
return true;
78
}

0 commit comments

Comments
 (0)