Skip to content

Commit 74647da

Browse files
committed
fix codes,write test cases and refactor codes
1 parent 4222cda commit 74647da

8 files changed

Lines changed: 198 additions & 17 deletions

File tree

‎Sprint-1/fix/median.js‎

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

88
function calculateMedian(list) {
9+
if (!Array.isArray(list)) {
10+
return null;
11+
}
12+
list = list.filter((item) => typeof item === "number");
13+
if (list.length === 0) {
14+
return null;
15+
}
16+
17+
list.sort((a, b) => a - b);
918
const middleIndex = Math.floor(list.length / 2);
10-
const median = list.splice(middleIndex, 1)[0];
11-
return median;
19+
const sumOfIndex = list[middleIndex] + list[middleIndex - 1];
20+
const middleValue = sumOfIndex / 2;
21+
if (list.length % 2 > 0) {
22+
return list[middleIndex];
23+
} else {
24+
return middleValue;
25+
}
1226
}
1327

1428
module.exports = calculateMedian;

‎Sprint-1/implement/dedupe.js‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
function dedupe() {}
1+
function dedupe(arr) {
2+
if (arr.length === 0) {
3+
return [];
4+
}
5+
const uniqueSet = new Set(arr);
6+
if (arr.length !== uniqueSet.size) {
7+
return [...uniqueSet];
8+
} else {
9+
return [...arr];
10+
}
11+
}
12+
13+
module.exports = dedupe;

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,57 @@ 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+
test("given an empty array, it returns an empty array", () => {
20+
const arr = [];
21+
const emptyArray = dedupe(arr);
22+
expect(emptyArray).toEqual([]);
23+
});
2024

2125
// Given an array with no duplicates
2226
// When passed to the dedupe function
2327
// Then it should return a copy of the original array
28+
test("the function should return the cope of original array when no duplicates of array pass to it", () => {
29+
const arr = [2, 3, 4, 5];
2430

31+
const result = dedupe(arr);
32+
expect(result).toEqual(arr);
33+
expect(result).not.toBe(arr);
34+
});
35+
test("the function should return the cope of original array when no duplicates of array pass to it ", () => {
36+
const arr = [13, 9, 8, 7];
37+
38+
const result = dedupe(arr);
39+
expect(result).toEqual(arr);
40+
expect(result).not.toBe(arr);
41+
});
2542
// Given an array of strings or numbers
2643
// When passed to the dedupe function
27-
// Then it should return a new array with duplicates removed while preserving the
44+
// Then it should return a new array with duplicates removed while preserving the
2845
// first occurrence of each element from the original array.
46+
test("the function should return the first occurrence of each element when duplicated arrays of element pass to it", () => {
47+
const arr = [3, 3, 3, 7, 8, 9, 9, 6, 6];
48+
49+
const newResult = dedupe(arr);
50+
expect(newResult).toEqual([3, 7, 8, 9, 6]);
51+
});
52+
test("the function should return the first occurrence of each element when duplicated arrays of element pass to it", () => {
53+
const arr = [3, 3, "hi", 8, 8, "hi", "you", 8, "you", "hi"];
54+
55+
const newResult = dedupe(arr);
56+
expect(newResult).toEqual([3, "hi", 8, "you"]);
57+
});
58+
test("the function should return the first occurrence of each element when duplicated arrays of element pass to it", () => {
59+
const arr = [
60+
"orange",
61+
"egg",
62+
"egg",
63+
"banana",
64+
"orange",
65+
"apple",
66+
"banana",
67+
"apple",
68+
];
69+
70+
const newResult = dedupe(arr);
71+
expect(newResult).toEqual(["orange", "egg", "banana", "apple"]);
72+
});

‎Sprint-1/implement/max.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
function findMax(elements) {
2+
if (elements.length === 0) {
3+
return Infinity;
4+
} else if (elements.length === 1) {
5+
return elements[0];
6+
}
7+
const number = elements.filter((value) => typeof value === "number");
8+
if (number.length === 0) {
9+
return undefined;
10+
}
11+
let max = elements[0];
12+
for (let i = 0; i < elements.length; i++) {
13+
if (max < elements[i]) {
14+
max = elements[i];
15+
}
16+
}
17+
return max;
218
}
319

420
module.exports = findMax;

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

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,73 @@ 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");
19+
test("when given an empty array to the function it returns Infinity", () => {
20+
const elements = [];
21+
22+
const result = findMax(elements);
23+
expect(result).toEqual(Infinity);
24+
});
2025

2126
// Given an array with one number
2227
// When passed to the max function
2328
// Then it should return that number
24-
29+
test("when the given is array of one number it should return that number", () => {
30+
const elements = [30];
31+
const result = findMax(elements);
32+
expect(result).toEqual(30);
33+
});
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
28-
37+
test("should return the largest number when the array contains both negative and positive numbers", () => {
38+
const elements = [-10, -12, -7, 0, 2, 4];
39+
const result = findMax(elements);
40+
expect(result).toEqual(4);
41+
});
42+
test("should return the largest number when the array contains both negative and positive numbers", () => {
43+
const elements = [-20, -6, -2, 0, 7, 9, 5];
44+
const result = findMax(elements);
45+
expect(result).toEqual(9);
46+
});
2947
// Given an array with just negative numbers
3048
// When passed to the max function
3149
// Then it should return the closest one to zero
32-
50+
test("should return number that is closest to zero when the array contains all negative numbers", () => {
51+
const elements = [-20, -6, -2, -4, -7, -9, -5];
52+
const result = findMax(elements);
53+
expect(result).toEqual(-2);
54+
});
55+
test("should return number that is closest to zero when the array contains all negative numbers", () => {
56+
const elements = [-20, -11, -1, 0, -7, -9, -5];
57+
const result = findMax(elements);
58+
expect(result).toEqual(0);
59+
});
3360
// Given an array with decimal numbers
3461
// When passed to the max function
3562
// Then it should return the largest decimal number
36-
63+
test("should return the largest decimal number when the array contains decimal numbers", () => {
64+
const elements = [2.7, 6.3, 7.3, 4.9, 7.9, 4.5];
65+
const result = findMax(elements);
66+
expect(result).toEqual(7.9);
67+
});
68+
test("should return the largest decimal number when the array contains decimal numbers", () => {
69+
const elements = [11.8, 16.3, 27.3, 41.9, 0.9, 14.5];
70+
const result = findMax(elements);
71+
expect(result).toEqual(41.9);
72+
});
3773
// Given an array with non-number values
3874
// When passed to the max function
3975
// Then it should return the max and ignore non-numeric values
40-
76+
test("should return the largest number ignoring non-numerical values when the array contains non-numerical value", () => {
77+
const elements = [11, "apple", 27, "hi", 9, "zero"];
78+
const result = findMax(elements);
79+
expect(result).toEqual(27);
80+
});
4181
// Given an array with only non-number values
4282
// When passed to the max function
4383
// Then it should return the least surprising value given how it behaves for all other inputs
84+
test("should return only number value, when the array contains non-numerical value", () => {
85+
const elements = ["apple", "hi", "book", "zero"];
86+
const result = findMax(elements);
87+
expect(result).toEqual(undefined);
88+
});

‎Sprint-1/implement/sum.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
function sum(elements) {
2+
if (elements.length === 0) {
3+
return 0;
4+
}
5+
const number = elements.filter((value) => typeof value === "number");
6+
if (number.length === 0) {
7+
return undefined;
8+
}
9+
if (elements.length === 1) {
10+
return elements[0];
11+
}
12+
let arraySum = 0;
13+
for (let i = 0; i < number.length; i++) {
14+
arraySum += number[i];
15+
}
16+
return arraySum;
217
}
318

419
module.exports = sum;

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

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,60 @@ 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")
16+
test("when empty array is given it should returns 0", () => {
17+
const elements = [];
18+
const result = sum(elements);
19+
expect(result).toEqual(0);
20+
});
1721

1822
// Given an array with just one number
1923
// When passed to the sum function
2024
// Then it should return that number
21-
25+
test("when array with just one number given it should returns that number", () => {
26+
const elements = [13];
27+
const result = sum(elements);
28+
expect(result).toEqual(13);
29+
});
30+
test("when array with just one number given it should returns that number", () => {
31+
const elements = [9];
32+
const result = sum(elements);
33+
expect(result).toEqual(9);
34+
});
2235
// Given an array containing negative numbers
2336
// When passed to the sum function
2437
// Then it should still return the correct total sum
25-
38+
test("when array with negative numbers given it should return the correct total sum", () => {
39+
const elements = [-9, -2, -4, -7];
40+
const result = sum(elements);
41+
expect(result).toEqual(-22);
42+
});
43+
test("when array with negative numbers given it should return the correct total sum", () => {
44+
const elements = [-11, -9, -2, -5];
45+
const result = sum(elements);
46+
expect(result).toEqual(-27);
47+
});
2648
// Given an array with decimal/float numbers
2749
// When passed to the sum function
2850
// Then it should return the correct total sum
29-
51+
test("when array that contain decimal or float number pass to the function it should return the correct total sum", () => {
52+
const elements = [0.9, 1 / 2, 4.3, 3.7, 1 / 4];
53+
const result = sum(elements);
54+
expect(result).toEqual(9.649999999999999);
55+
});
3056
// Given an array containing non-number values
3157
// When passed to the sum function
3258
// Then it should ignore the non-numerical values and return the sum of the numerical elements
59+
test("when array that contain number and non-number pass to the function it should return the sum of the numerical elements", () => {
60+
const elements = [9, 1, 4, "one", 3, "two", 4];
61+
const result = sum(elements);
62+
expect(result).toEqual(21);
63+
});
3364

3465
// Given an array with only non-number values
3566
// When passed to the sum function
3667
// Then it should return the least surprising value given how it behaves for all other inputs
68+
test("when array that contain non-number pass to the function it should return undefined", () => {
69+
const elements = ["window", "one", "name", "two"];
70+
const result = sum(elements);
71+
expect(result).toEqual(undefined);
72+
});

‎Sprint-1/refactor/includes.js‎

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

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

0 commit comments

Comments
 (0)