Skip to content

Commit 2993b07

Browse files
committed
Address mentor feedback for sprint 1 data groups
1 parent 1f24b43 commit 2993b07

6 files changed

Lines changed: 49 additions & 0 deletions

File tree

Sprint-1/fix/median.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,46 @@ function calculateMedian(list) {
1111
return null;
1212
}
1313

14+
<<<<<<< HEAD
1415
// Keep only real numeric values
1516
const numbersOnly = list.filter(
1617
(item) => typeof item === "number" && !Number.isNaN(item)
1718
);
1819

1920
// Return null if the array contains no numbers
21+
=======
22+
// filter() returns a new array, so this does not modify the original input
23+
const numbersOnly = list.filter((item) => Number.isFinite(item));
24+
25+
// Return null if there are no numeric values
26+
>>>>>>> a22ed15 (Address mentor feedback for sprint 1 data groups)
2027
if (numbersOnly.length === 0) {
2128
return null;
2229
}
2330

31+
<<<<<<< HEAD
2432
// Create a sorted copy so the original input is not changed
2533
const sortedNumbers = [...numbersOnly].sort((a, b) => a - b);
2634

2735
const middleIndex = Math.floor(sortedNumbers.length / 2);
2836

2937
// For an even-length array, median is the average of the two middle values
38+
=======
39+
// Safe to sort directly because numbersOnly is already a new array
40+
const sortedNumbers = numbersOnly.sort((a, b) => a - b);
41+
const middleIndex = Math.floor(sortedNumbers.length / 2);
42+
43+
// Even number of values: return the average of the two middle values
44+
>>>>>>> a22ed15 (Address mentor feedback for sprint 1 data groups)
3045
if (sortedNumbers.length % 2 === 0) {
3146
return (sortedNumbers[middleIndex - 1] + sortedNumbers[middleIndex]) / 2;
3247
}
3348

49+
<<<<<<< HEAD
3450
// For an odd-length array, median is the middle value
51+
=======
52+
// Odd number of values: return the middle value
53+
>>>>>>> a22ed15 (Address mentor feedback for sprint 1 data groups)
3554
return sortedNumbers[middleIndex];
3655
}
3756

Sprint-1/implement/dedupe.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ const dedupe = require("./dedupe.js");
22
/*
33
Dedupe Array
44
5+
<<<<<<< HEAD
56
📖 Dedupe means deduplicate
7+
=======
8+
Dedupe means deduplicate
9+
>>>>>>> a22ed15 (Address mentor feedback for sprint 1 data groups)
610
711
In this kata, you will need to deduplicate the elements of an array
812
@@ -23,8 +27,17 @@ test("given an empty array, it returns an empty array", () => {
2327
// Given an array with no duplicates
2428
// When passed to the dedupe function
2529
// Then it should return a copy of the original array
30+
<<<<<<< HEAD
2631
test("given an array with no duplicates, it returns the same values", () => {
2732
expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]);
33+
=======
34+
test("given an array with no duplicates, it returns a copy of the original array", () => {
35+
const input = [1, 2, 3];
36+
const result = dedupe(input);
37+
38+
expect(result).toEqual(input);
39+
expect(result).not.toBe(input);
40+
>>>>>>> a22ed15 (Address mentor feedback for sprint 1 data groups)
2841
});
2942

3043
// Given an array with strings or numbers

Sprint-1/implement/max.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ function findMax(elements) {
55
let maxValue = -Infinity;
66

77
for (const element of elements) {
8+
<<<<<<< HEAD
89
// Only compare values that are real numbers
910
if (typeof element === "number" && !Number.isNaN(element)) {
1011
if (element > maxValue) {
1112
maxValue = element;
1213
}
14+
=======
15+
if (Number.isFinite(element) && element > maxValue) {
16+
maxValue = element;
17+
>>>>>>> a22ed15 (Address mentor feedback for sprint 1 data groups)
1318
}
1419
}
1520

Sprint-1/implement/max.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ test("given an array with decimal numbers, returns the largest decimal", () => {
5151
// When passed to the max function
5252
// Then it should return the max and ignore non-numeric values
5353
test("given an array with non-number values, ignores them and returns the max", () => {
54+
<<<<<<< HEAD
5455
expect(findMax(["hey", 10, "hi", 60, 10])).toBe(60);
56+
=======
57+
expect(findMax(["hey", 10, "300", "hi", 60, 10])).toBe(60);
58+
>>>>>>> a22ed15 (Address mentor feedback for sprint 1 data groups)
5559
});
5660

5761
// Given an array with only non-number values

Sprint-1/implement/sum.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ function sum(elements) {
55
let total = 0;
66

77
for (const element of elements) {
8+
<<<<<<< HEAD
89
// Only add values that are real numbers
910
if (typeof element === "number" && !Number.isNaN(element)) {
11+
=======
12+
if (Number.isFinite(element)) {
13+
>>>>>>> a22ed15 (Address mentor feedback for sprint 1 data groups)
1014
total += element;
1115
}
1216
}

Sprint-1/implement/sum.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ test("given an array with negative numbers, returns the correct sum", () => {
3535
// When passed to the sum function
3636
// Then it should return the correct total sum
3737
test("given an array with decimal numbers, returns the correct sum", () => {
38+
<<<<<<< HEAD
3839
expect(sum([1.5, 2.5, 3])).toBe(7);
40+
=======
41+
expect(sum([1.2, 0.6, 0.005])).toBeCloseTo(1.805, 10);
42+
>>>>>>> a22ed15 (Address mentor feedback for sprint 1 data groups)
3943
});
4044

4145
// Given an array containing non-number values

0 commit comments

Comments
 (0)