Skip to content

Commit 0448c28

Browse files
committed
fixed issues after the feedback
1 parent 3e3845b commit 0448c28

8 files changed

Lines changed: 55 additions & 24 deletions

File tree

Sprint-1/fix/median.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,23 @@
77

88
function calculateMedian(list) {
99
let median = null;
10-
if (Array.isArray(list) && !list.every((item) => typeof item != "number")) {
11-
numericList = list.filter((item) => typeof item === "number");
12-
sortedList = numericList.toSorted((a, b) => a - b);
13-
const middleIndex = Math.floor(sortedList.length / 2);
14-
if (sortedList.length % 2 === 0) {
15-
const medianArray = sortedList.splice(middleIndex - 1, 2);
16-
median = (medianArray[0] + medianArray[1]) / 2;
17-
} else median = sortedList.splice(middleIndex, 1)[0];
18-
}
10+
//return null if the list is not an array
11+
if (!Array.isArray(list)) return median;
12+
13+
// return null if none of the items in the array is a number
14+
if (!list.some((item) => typeof item === "number")) return median;
15+
16+
let numericArray = list.filter((item) => typeof item === "number");
17+
let sortedArray = numericArray.toSorted((a, b) => a - b);
18+
const middleIndex = Math.floor(sortedArray.length / 2);
19+
20+
//check if the number of items in the array are even or odd
21+
// and then calculate the median accordingly
22+
if (sortedArray.length % 2 === 0)
23+
median = (sortedArray[middleIndex - 1] + sortedArray[middleIndex]) / 2;
24+
else median = sortedArray[middleIndex];
25+
26+
if (median === null) throw new Error("Median can't be null");
1927
return median;
2028
}
2129

Sprint-1/implement/dedupe.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
function dedupe(arr) {
2-
if (arr.length === 0) return arr;
3-
else {
4-
dedupeArray = [];
5-
arr.forEach((element) => {
6-
if (!dedupeArray.includes(element)) dedupeArray.push(element);
7-
});
8-
return dedupeArray;
9-
}
2+
if (!Array.isArray(arr)) throw new Error(arr + " is not an array");
3+
else if (arr.length === 0) return arr;
4+
else return Array.from(new Set(arr));
105
}
116

127
module.exports = dedupe;

Sprint-1/implement/dedupe.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,12 @@ describe("dedupe", () => {
4747
it(`returns a copy of array removing the duplicates from [${input}]`, () =>
4848
expect(dedupe(input)).toEqual(expected))
4949
);
50+
51+
// Given an input value that is not array could be null or undefined or just a number or string
52+
// When passed to the dedupe function
53+
// Then it should thrown an error
54+
[null, 930, "just a string", undefined, {}].forEach((val) =>
55+
it("throw an error if the input is not an array", () =>
56+
expect(() => dedupe(val)).toThrow(val + " is not an array"))
57+
);
5058
});

Sprint-1/implement/max.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
function findMax(array) {
2-
numbersArray = array.filter((value) => typeof value === "number");
2+
if (!Array.isArray(array)) throw new Error(array + " is not an array");
3+
let numbersArray = array.filter((value) => typeof value === "number");
34
return Math.max(...numbersArray);
45
}
56

Sprint-1/implement/max.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,12 @@ describe("findMax", () => {
104104
it(`returns the least surprising value for only non-numbers array [${input}]`, () =>
105105
expect(findMax(input)).toEqual(expected))
106106
);
107+
108+
// Given an input that is not array could be null or undefined or just a number or string
109+
// When passed to the findMax function
110+
// Then it should thrown an error
111+
[null, 930, "just a string", undefined, {}].forEach((val) =>
112+
it("throw an error if the input is not an array", () =>
113+
expect(() => findMax(val)).toThrow(val + " is not an array"))
114+
);
107115
});

Sprint-1/implement/sum.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
function sum(array) {
2-
numbersArray = array.filter((value) => typeof value === "number");
3-
let sum = 0;
4-
numbersArray.forEach((element) => {
5-
sum += element;
6-
});
2+
if (!Array.isArray(array)) throw new Error(array + " is not an array");
3+
let numbersArray = array.filter((value) => typeof value === "number");
4+
let initialValue = 0;
5+
let sum = numbersArray.reduce(
6+
(accumulator, currentValue) => accumulator + currentValue,
7+
initialValue
8+
);
79
return sum;
810
}
911

Sprint-1/implement/sum.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,12 @@ describe("sum", () => {
7575
it(`array containing only non-number values [${input}] should should return 0`, () =>
7676
expect(sum(input)).toEqual(expected))
7777
);
78+
79+
// Given an input value that is not array could be null or undefined or just a number or string
80+
// When passed to the sum function
81+
// Then it should thrown an error
82+
[null, 930, "just a string", undefined, {}].forEach((val) =>
83+
it("throw an error if the input is not an array", () =>
84+
expect(() => sum(val)).toThrow(val + " is not an array"))
85+
);
7886
});

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
const fs = require("fs"); //getting file system to read file
2+
13
function getFinalFrequency() {
24
let finalFrequency = 0;
3-
const fs = require("fs"); //getting file system to read file
45

56
const data = fs.readFileSync("input.txt", "utf8");
67
const numbers = data

0 commit comments

Comments
 (0)