Skip to content

Commit 9d6f5a7

Browse files
Complete sprint-1
1 parent cfd674c commit 9d6f5a7

File tree

10 files changed

+220
-75
lines changed

10 files changed

+220
-75
lines changed

Sprint-1/fix/median.js

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

88
function calculateMedian(list) {
9-
const middleIndex = Math.floor(list.length / 2);
10-
const median = list.splice(middleIndex, 1)[0];
9+
if (!Array.isArray(list)) return null;
10+
const numericList = list
11+
.filter((l) => typeof l === "number")
12+
.sort((a, b) => a - b);
13+
if (numericList.length <= 1) return null;
14+
15+
const middleIndex = Math.floor(numericList.length / 2);
16+
17+
if (numericList.length % 2 === 0) {
18+
return (numericList[middleIndex] + numericList[middleIndex - 1]) / 2;
19+
}
20+
const median = numericList.splice(middleIndex, 1)[0];
1121
return median;
1222
}
1323

Sprint-1/fix/median.test.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ describe("calculateMedian", () => {
1313
{ input: [1, 2, 3, 4], expected: 2.5 },
1414
{ input: [1, 2, 3, 4, 5, 6], expected: 3.5 },
1515
].forEach(({ input, expected }) =>
16-
it(`returns the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
16+
it(`returns the median for [${input}]`, () =>
17+
expect(calculateMedian(input)).toEqual(expected))
1718
);
1819

1920
[
@@ -24,7 +25,8 @@ describe("calculateMedian", () => {
2425
{ input: [110, 20, 0], expected: 20 },
2526
{ input: [6, -2, 2, 12, 14], expected: 6 },
2627
].forEach(({ input, expected }) =>
27-
it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
28+
it(`returns the correct median for unsorted array [${input}]`, () =>
29+
expect(calculateMedian(input)).toEqual(expected))
2830
);
2931

3032
it("doesn't modify the input array [3, 1, 2]", () => {
@@ -33,8 +35,17 @@ describe("calculateMedian", () => {
3335
expect(list).toEqual([3, 1, 2]);
3436
});
3537

36-
[ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val =>
37-
it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null))
38+
[
39+
"not an array",
40+
123,
41+
null,
42+
undefined,
43+
{},
44+
[],
45+
["apple", null, undefined],
46+
].forEach((val) =>
47+
it(`returns null for non-numeric array (${val})`, () =>
48+
expect(calculateMedian(val)).toBe(null))
3849
);
3950

4051
[
@@ -45,6 +56,7 @@ describe("calculateMedian", () => {
4556
{ input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 },
4657
{ input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 },
4758
].forEach(({ input, expected }) =>
48-
it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
59+
it(`filters out non-numeric values and calculates the median for [${input}]`, () =>
60+
expect(calculateMedian(input)).toEqual(expected))
4961
);
5062
});

Sprint-1/implement/dedupe.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
function dedupe() {}
1+
function dedupe(elements) {
2+
return [...new Set(elements)];
3+
}
4+
5+
module.exports = dedupe;
6+
7+
console.log(dedupe([4542543, undefined, 4542543, 4542543, null, [], null]));

Sprint-1/implement/dedupe.test.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,29 @@ E.g. dedupe([1, 2, 1]) target output: [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+
describe("dedupe", () => {
20+
it("returns an empty array if passed an empty array", () =>
21+
expect(dedupe([])).toEqual([]));
2022

21-
// Given an array with no duplicates
22-
// When passed to the dedupe function
23-
// Then it should return a copy of the original array
23+
[
24+
{ input: [1, 2, 3], expected: [1, 2, 3] },
25+
{
26+
input: [4542543, 65756756, 433254],
27+
expected: [4542543, 65756756, 433254],
28+
},
29+
].forEach(({ input, expected }) =>
30+
it("returns a copy of original array when passed array with no duplicates ", () =>
31+
expect(dedupe(input)).toEqual(expected))
32+
);
2433

25-
// Given an array with strings or numbers
26-
// When passed to the dedupe function
27-
// Then it should remove the duplicate values, preserving the first occurence of each element
34+
[
35+
{ input: [1, 2, 3, "apple", 3, 2, {}], expected: [1, 2, 3, "apple", {}] },
36+
{
37+
input: [4542543, undefined, 4542543, 4542543, null, [], null],
38+
expected: [4542543, undefined, null, []],
39+
},
40+
].forEach(({ input, expected }) =>
41+
it("returns array wit unique values in their first occurrence index when passed an array with numbers and non-number values ", () =>
42+
expect(dedupe(input)).toEqual(expected))
43+
);
44+
});

Sprint-1/implement/max.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
function findMax(elements) {
2+
const numberElements = elements.filter((el) => typeof el === "number");
3+
if (numberElements.length === 0) return -Infinity;
4+
return Math.max(...numberElements);
25
}
36

47
module.exports = findMax;

Sprint-1/implement/max.test.js

Lines changed: 65 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,68 @@ We have set things up already so that this file can see your function from the o
1212

1313
const findMax = require("./max.js");
1414

15-
// Given an empty array
16-
// When passed to the max function
17-
// Then it should return -Infinity
18-
// Delete this test.todo and replace it with a test.
19-
test.todo("given an empty array, returns -Infinity");
20-
21-
// Given an array with one number
22-
// When passed to the max function
23-
// Then it should return that number
24-
25-
// Given an array with both positive and negative numbers
26-
// When passed to the max function
27-
// Then it should return the largest number overall
28-
29-
// Given an array with just negative numbers
30-
// When passed to the max function
31-
// Then it should return the closest one to zero
32-
33-
// Given an array with decimal numbers
34-
// When passed to the max function
35-
// Then it should return the largest decimal number
36-
37-
// Given an array with non-number values
38-
// When passed to the max function
39-
// Then it should return the max and ignore non-numeric values
40-
41-
// Given an array with only non-number values
42-
// When passed to the max function
43-
// Then it should return the least surprising value given how it behaves for all other inputs
15+
describe("max()", () => {
16+
it("returns -Infinity for empty array", () =>
17+
expect(findMax([])).toBe(-Infinity));
18+
19+
[
20+
{ input: [43], expected: 43 },
21+
{ input: [342], expected: 342 },
22+
{ input: [65455453], expected: 65455453 },
23+
].forEach(({ input, expected }) =>
24+
it("returns the only number in array with one number", () =>
25+
expect(findMax(input)).toBe(expected))
26+
);
27+
28+
[
29+
{ input: [43, -4], expected: 43 },
30+
{ input: [342, -45, -768, 23], expected: 342 },
31+
{ input: [65455453, -54666, -4566, 6565], expected: 65455453 },
32+
].forEach(({ input, expected }) =>
33+
it("returns the largest number in array containing negative numbers", () =>
34+
expect(findMax(input)).toBe(expected))
35+
);
36+
37+
[
38+
{ input: [-43, -4], expected: -4 },
39+
{ input: [-342, -45, -768, -23], expected: -23 },
40+
{ input: [-65455453, -54666, -4566, -6565], expected: -4566 },
41+
].forEach(({ input, expected }) =>
42+
it("returns closes number to zero in an array with only negative numbers", () =>
43+
expect(findMax(input)).toBe(expected))
44+
);
45+
46+
[
47+
{ input: [43.32, -4.1], expected: 43.32 },
48+
{ input: [342.54, -45.12, -768.76, 23.99], expected: 342.54 },
49+
{
50+
input: [65455453.4533, -54666.222, -4566.322, 6565.43],
51+
expected: 65455453.4533,
52+
},
53+
].forEach(({ input, expected }) =>
54+
it("returns the largest decimal number in an array with numbers", () =>
55+
expect(findMax(input)).toBe(expected))
56+
);
57+
58+
[
59+
{ input: [1, 2, "3", null, undefined, 4], expected: 4 },
60+
{ input: ["apple", 1, 2, 34, "banana", 4], expected: 34 },
61+
{ input: [1, "2", 3, "4", 5], expected: 5 },
62+
{ input: [1, "apple", 2, null, 3, undefined, 4], expected: 4 },
63+
{ input: [3, "apple", 1, null, 2, undefined, 4, 54], expected: 54 },
64+
{ input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 5 },
65+
].forEach(({ input, expected }) =>
66+
it("returns max of numbers in an array containing non-numeric values", () =>
67+
expect(findMax(input)).toEqual(expected))
68+
);
69+
70+
[
71+
["not an array", "3", null, undefined],
72+
["apple", "banana"],
73+
["apple", null, undefined],
74+
["banana", {}],
75+
].forEach((input) =>
76+
it("returns -Infinity in array with only non-numeric values", () =>
77+
expect(findMax(input)).toBe(-Infinity))
78+
);
79+
});

Sprint-1/implement/sum.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
function sum(elements) {
2+
if (!Array.isArray(elements)) return 0;
3+
return elements.reduce((acc, curr) => {
4+
return typeof curr === "number" ? acc + curr : acc;
5+
}, 0);
26
}
37

48
module.exports = sum;
9+
console.log(sum([]));

Sprint-1/implement/sum.test.js

Lines changed: 79 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,82 @@ const sum = require("./sum.js");
1010

1111
// Acceptance Criteria:
1212

13-
// Given an empty array
14-
// When passed to the sum function
15-
// Then it should return 0
16-
test.todo("given an empty array, returns 0")
17-
18-
// Given an array with just one number
19-
// When passed to the sum function
20-
// Then it should return that number
21-
22-
// Given an array containing negative numbers
23-
// When passed to the sum function
24-
// Then it should still return the correct total sum
25-
26-
// Given an array with decimal/float numbers
27-
// When passed to the sum function
28-
// Then it should return the correct total sum
29-
30-
// Given an array containing non-number values
31-
// When passed to the sum function
32-
// Then it should ignore the non-numerical values and return the sum of the numerical elements
33-
34-
// Given an array with only non-number values
35-
// When passed to the sum function
36-
// Then it should return the least surprising value given how it behaves for all other inputs
13+
describe("sum()", () => {
14+
it("returns 0 for empty array", () => expect(sum([])).toBe(0));
15+
16+
[
17+
{ input: [4], expected: 4 },
18+
{ input: [367], expected: 367 },
19+
{ input: [7958463], expected: 7958463 },
20+
].forEach(({ input, expected }) => {
21+
it(`returns the sum for arrays with one number`, () =>
22+
expect(sum(input)).toBe(expected));
23+
});
24+
25+
[
26+
{ input: [-9], expected: -9 },
27+
{ input: [-367, -5], expected: -372 },
28+
{ input: [-7958463, -100, -202, -6453], expected: -7965218 },
29+
].forEach(({ input, expected }) =>
30+
it("returns the correct sum for array with only negative values", () =>
31+
expect(sum(input)).toBe(expected))
32+
);
33+
34+
[
35+
{ input: [-9, 9], expected: 0 },
36+
{ input: [-367, -5, 70, 2], expected: -300 },
37+
{ input: [-7958463, -100, -202, -6453, 153, 45621], expected: -7919444 },
38+
].forEach(({ input, expected }) =>
39+
it("returns the correct sum for array containing negative numbers", () =>
40+
expect(sum(input)).toBe(expected))
41+
);
42+
43+
[
44+
{ input: [-9, 9, 0.1], expected: 0.1 },
45+
{ input: [-367, -5, 70, 2, -4.567], expected: -304.567 },
46+
{
47+
input: [-7958463, -100, -202, -6453, 153, 45621, -1.48, 8976.456],
48+
expected: -7910469.024,
49+
},
50+
].forEach(({ input, expected }) =>
51+
it("returns the correct sum for array containing decimal/float numbers", () =>
52+
expect(sum(input)).toBe(expected))
53+
);
54+
55+
[
56+
{ input: [-9, 9, 0.1, () => {}], expected: 0.1 },
57+
{
58+
input: [-367, -5, "-234", 70, 2, { fruit: "apple" }, -4.567],
59+
expected: -304.567,
60+
},
61+
{
62+
input: [
63+
-7958463,
64+
-100,
65+
"Iran",
66+
-202,
67+
-6453,
68+
"UK",
69+
153,
70+
[],
71+
45621,
72+
"Egypt",
73+
-1.48,
74+
8976.456,
75+
{},
76+
],
77+
expected: -7910469.024,
78+
},
79+
].forEach(({ input, expected }) =>
80+
it("returns the correct sum for array containing decimal/float numbers", () =>
81+
expect(sum(input)).toBe(expected))
82+
);
83+
84+
[
85+
["not an array", null, undefined, {}, []],
86+
[("apple", null, undefined)],
87+
].forEach((item) =>
88+
it("returns 0 for arrays with only non-number values", () =>
89+
expect(sum(item)).toBe(0))
90+
);
91+
});

Sprint-1/refactor/includes.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +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];
6-
if (element === target) {
7-
return true;
8-
}
9-
}
10-
return false;
4+
return list.includes(target) ? true : false;
115
}
126

137
module.exports = includes;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const fs = require("fs");
2+
3+
function calculateFrequecy() {
4+
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);
7+
}

0 commit comments

Comments
 (0)