Skip to content

Commit 3db21bf

Browse files
Apply changes asked by the reviewer
1 parent 9d6f5a7 commit 3db21bf

File tree

10 files changed

+42
-23
lines changed

10 files changed

+42
-23
lines changed

Sprint-1/fix/median.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ function calculateMedian(list) {
1010
const numericList = list
1111
.filter((l) => typeof l === "number")
1212
.sort((a, b) => a - b);
13-
if (numericList.length <= 1) return null;
13+
if (numericList.length < 1) return null;
14+
if (numericList.length === 1) return numericList[0];
1415

1516
const middleIndex = Math.floor(numericList.length / 2);
1617

1718
if (numericList.length % 2 === 0) {
1819
return (numericList[middleIndex] + numericList[middleIndex - 1]) / 2;
1920
}
20-
const median = numericList.splice(middleIndex, 1)[0];
21+
const median = numericList[middleIndex];
2122
return median;
2223
}
2324

Sprint-1/fix/median.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ const calculateMedian = require("./median.js");
88

99
describe("calculateMedian", () => {
1010
[
11+
{ input: [1.5, 2.5, 3.5], expected: 2.5 },
12+
{ input: [-2, -4, -6, -10], expected: -5 },
13+
{ input: [3, 3, 3, 3, 3], expected: 3 },
14+
{ input: [1, 3], expected: 2 },
15+
{ input: [1], expected: 1 },
1116
{ input: [1, 2, 3], expected: 2 },
1217
{ input: [1, 2, 3, 4, 5], expected: 3 },
1318
{ input: [1, 2, 3, 4], expected: 2.5 },
@@ -18,6 +23,9 @@ describe("calculateMedian", () => {
1823
);
1924

2025
[
26+
{ input: [-5, -1, -3], expected: -3 },
27+
{ input: [2, 2, 2, 2], expected: 2 },
28+
{ input: [8, 4], expected: 6 },
2129
{ input: [3, 1, 2], expected: 2 },
2230
{ input: [5, 1, 3, 4, 2], expected: 3 },
2331
{ input: [4, 2, 1, 3], expected: 2.5 },
@@ -49,6 +57,7 @@ describe("calculateMedian", () => {
4957
);
5058

5159
[
60+
{ input: ["a", null, 5, undefined], expected: 5 },
5261
{ input: [1, 2, "3", null, undefined, 4], expected: 2 },
5362
{ input: ["apple", 1, 2, 3, "banana", 4], expected: 2.5 },
5463
{ input: [1, "2", 3, "4", 5], expected: 3 },

Sprint-1/implement/dedupe.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,3 @@ function dedupe(elements) {
33
}
44

55
module.exports = dedupe;
6-
7-
console.log(dedupe([4542543, undefined, 4542543, 4542543, null, [], null]));

Sprint-1/implement/dedupe.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ describe("dedupe", () => {
2727
expected: [4542543, 65756756, 433254],
2828
},
2929
].forEach(({ input, expected }) =>
30-
it("returns a copy of original array when passed array with no duplicates ", () =>
31-
expect(dedupe(input)).toEqual(expected))
30+
it(
31+
"returns a copy of original array when passed array with no duplicates ",
32+
() => expect(dedupe(input)).toEqual(expected),
33+
expect(dedupe(input)).not.toBe(expected)
34+
)
3235
);
3336

3437
[

Sprint-1/implement/max.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
function findMax(elements) {
2-
const numberElements = elements.filter((el) => typeof el === "number");
2+
const numberElements = elements.filter(
3+
(el) => typeof el === "number" && !Number.isNaN(el)
4+
);
35
if (numberElements.length === 0) return -Infinity;
46
return Math.max(...numberElements);
57
}
68

9+
// console.log(findMax(findMax([0, null, 1])));
10+
711
module.exports = findMax;

Sprint-1/implement/max.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ describe("max()", () => {
5656
);
5757

5858
[
59-
{ input: [1, 2, "3", null, undefined, 4], expected: 4 },
59+
{ input: [0, NaN, 1], expected: 1 },
60+
{ input: [1, 2, "3", null, undefined, NaN, 4], expected: 4 },
6061
{ input: ["apple", 1, 2, 34, "banana", 4], expected: 34 },
6162
{ input: [1, "2", 3, "4", 5], expected: 5 },
6263
{ input: [1, "apple", 2, null, 3, undefined, 4], expected: 4 },

Sprint-1/implement/sum.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
function sum(elements) {
22
if (!Array.isArray(elements)) return 0;
33
return elements.reduce((acc, curr) => {
4-
return typeof curr === "number" ? acc + curr : acc;
4+
return Number.isFinite(curr) ? acc + curr : acc;
55
}, 0);
66
}
77

88
module.exports = sum;
9-
console.log(sum([]));

Sprint-1/implement/sum.test.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ const sum = require("./sum.js");
1111
// Acceptance Criteria:
1212

1313
describe("sum()", () => {
14-
it("returns 0 for empty array", () => expect(sum([])).toBe(0));
14+
it("returns 0 for empty array", () => expect(sum([])).toBeCloseTo(0));
1515

1616
[
1717
{ input: [4], expected: 4 },
1818
{ input: [367], expected: 367 },
1919
{ input: [7958463], expected: 7958463 },
2020
].forEach(({ input, expected }) => {
2121
it(`returns the sum for arrays with one number`, () =>
22-
expect(sum(input)).toBe(expected));
22+
expect(sum(input)).toBeCloseTo(expected));
2323
});
2424

2525
[
@@ -28,7 +28,7 @@ describe("sum()", () => {
2828
{ input: [-7958463, -100, -202, -6453], expected: -7965218 },
2929
].forEach(({ input, expected }) =>
3030
it("returns the correct sum for array with only negative values", () =>
31-
expect(sum(input)).toBe(expected))
31+
expect(sum(input)).toBeCloseTo(expected))
3232
);
3333

3434
[
@@ -37,7 +37,7 @@ describe("sum()", () => {
3737
{ input: [-7958463, -100, -202, -6453, 153, 45621], expected: -7919444 },
3838
].forEach(({ input, expected }) =>
3939
it("returns the correct sum for array containing negative numbers", () =>
40-
expect(sum(input)).toBe(expected))
40+
expect(sum(input)).toBeCloseTo(expected))
4141
);
4242

4343
[
@@ -49,17 +49,19 @@ describe("sum()", () => {
4949
},
5050
].forEach(({ input, expected }) =>
5151
it("returns the correct sum for array containing decimal/float numbers", () =>
52-
expect(sum(input)).toBe(expected))
52+
expect(sum(input)).toBeCloseTo(expected))
5353
);
5454

5555
[
56-
{ input: [-9, 9, 0.1, () => {}], expected: 0.1 },
56+
{ input: [-9, 9, 0.1, () => {}, undefined], expected: 0.1 },
5757
{
58-
input: [-367, -5, "-234", 70, 2, { fruit: "apple" }, -4.567],
58+
input: [-367, -5, "-234", 70, 2, null, { fruit: "apple" }, -4.567],
5959
expected: -304.567,
6060
},
61+
{ input: [Infinity, -Infinity, 1, , NaN, 10, -9], expected: 2 },
6162
{
6263
input: [
64+
NaN,
6365
-7958463,
6466
-100,
6567
"Iran",
@@ -77,15 +79,15 @@ describe("sum()", () => {
7779
expected: -7910469.024,
7880
},
7981
].forEach(({ input, expected }) =>
80-
it("returns the correct sum for array containing decimal/float numbers", () =>
81-
expect(sum(input)).toBe(expected))
82+
it("returns the correct sum for array containing finite and infinite values", () =>
83+
expect(sum(input)).toBeCloseTo(expected))
8284
);
8385

8486
[
8587
["not an array", null, undefined, {}, []],
8688
[("apple", null, undefined)],
8789
].forEach((item) =>
8890
it("returns 0 for arrays with only non-number values", () =>
89-
expect(sum(item)).toBe(0))
91+
expect(sum(item)).toBeCloseTo(0))
9092
);
9193
});

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-
return list.includes(target) ? true : false;
4+
for (const item of list) if (item === target) return true;
5+
return false;
56
}
67

78
module.exports = includes;

Sprint-1/stretch/aoc-2018-day1/solution.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const fs = require("fs");
22

33
function calculateFrequecy() {
44
const text = fs.readFileSync("input.txt", "utf-8");
5-
const ChangesInFrequency = text.trim().split(/\r?\n/).map(Number);
6-
return ChangesInFrequency.reduce((acc, curr) => acc + curr, 0);
5+
const changesInFrequency = text.trim().split(/\r?\n/).map(Number);
6+
return changesInFrequency.reduce((acc, curr) => acc + curr, 0);
77
}
8+
console.log(calculateFrequecy());

0 commit comments

Comments
 (0)