Skip to content

Commit 350ad2e

Browse files
Alex JamshidiAlex Jamshidi
authored andcommitted
code formatter with prettier
1 parent 4e0a556 commit 350ad2e

8 files changed

Lines changed: 102 additions & 75 deletions

File tree

Sprint-1/fix/median.js

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

88
function calculateMedian(list) {
9-
if (!Array.isArray(list)) {return null;}
10-
let numberArray = list.filter(n => Number.isFinite(n))
11-
if (numberArray.length == 0) {return null};
12-
numberArray.sort((a, b) => a - b)
9+
if (!Array.isArray(list)) {
10+
return null;
11+
}
12+
let numberArray = list.filter((n) => Number.isFinite(n));
13+
if (numberArray.length == 0) {
14+
return null;
15+
}
16+
numberArray.sort((a, b) => a - b);
1317

14-
if (numberArray.length % 2 == 1) {return numberArray[((numberArray.length - 1) / 2)]}
15-
else {return (numberArray[((numberArray.length / 2))] + numberArray[((numberArray.length / 2) - 1)]) / 2}
18+
if (numberArray.length % 2 == 1) {
19+
return numberArray[(numberArray.length - 1) / 2];
20+
} else {
21+
return (
22+
(numberArray[numberArray.length / 2] +
23+
numberArray[numberArray.length / 2 - 1]) /
24+
2
25+
);
26+
}
1627
}
1728

1829
module.exports = calculateMedian;

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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
function dedupe(array) {
2-
const unique = [];
3-
array.forEach((item) => {
4-
if (!unique.includes(item)) {
5-
unique.push(item);
6-
}
7-
});
8-
return unique
2+
const unique = [];
3+
array.forEach((item) => {
4+
if (!unique.includes(item)) {
5+
unique.push(item);
6+
}
7+
});
8+
return unique;
99
}
1010

11-
module.exports = dedupe;
11+
module.exports = dedupe;

Sprint-1/implement/max.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
function findMax(list) {
2-
const numbers = list.filter(n => Number.isFinite(n));
3-
if (numbers.length == 0) { return "-Infinity"}
4-
else {return Math.max(...numbers)};
2+
const numbers = list.filter((n) => Number.isFinite(n));
3+
if (numbers.length == 0) {
4+
return "-Infinity";
5+
} else {
6+
return Math.max(...numbers);
7+
}
58
}
69

710
module.exports = findMax;

Sprint-1/implement/max.test.js

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,71 +13,67 @@ We have set things up already so that this file can see your function from the o
1313
const findMax = require("./max.js");
1414

1515
describe("calculateMedian", () => {
16-
// Given an empty array
17-
// When passed to the max function
18-
// Then it should return -Infinity
19-
// Delete this test.todo and replace it with a test.
16+
// Given an empty array
17+
// When passed to the max function
18+
// Then it should return -Infinity
19+
// Delete this test.todo and replace it with a test.
2020

2121
it("empty array returns -Infinity", () => {
2222
const list = [];
2323
expect(findMax(list)).toEqual("-Infinity");
2424
});
2525

26-
// Given an array with one number
27-
// When passed to the max function
28-
// Then it should return that number
26+
// Given an array with one number
27+
// When passed to the max function
28+
// Then it should return that number
2929

3030
it("array with one number returns that number", () => {
3131
const list = [2];
3232
expect(findMax(list)).toEqual(2);
3333
});
3434

35-
// Given an array with both positive and negative numbers
36-
// When passed to the max function
37-
// Then it should return the largest number overall
35+
// Given an array with both positive and negative numbers
36+
// When passed to the max function
37+
// Then it should return the largest number overall
3838

3939
it("array with +ve and -ve numbers, returns largest number overall", () => {
4040
const list = [10, -15];
4141
expect(findMax(list)).toEqual(10);
4242
});
4343

44-
45-
// Given an array with just negative numbers
46-
// When passed to the max function
47-
// Then it should return the closest one to zero
44+
// Given an array with just negative numbers
45+
// When passed to the max function
46+
// Then it should return the closest one to zero
4847

4948
it("array -ve numbers, returns closest to zero", () => {
5049
const list = [-1, -2, -3, -15];
5150
expect(findMax(list)).toEqual(-1);
5251
});
5352

54-
55-
// Given an array with decimal numbers
56-
// When passed to the max function
57-
// Then it should return the largest decimal number
53+
// Given an array with decimal numbers
54+
// When passed to the max function
55+
// Then it should return the largest decimal number
5856

5957
it("array with decimal numbers should return largest decimal number", () => {
6058
const list = [1.9, 2.8999, 3.1];
6159
expect(findMax(list)).toEqual(3.1);
6260
});
6361

64-
65-
// Given an array with non-number values
66-
// When passed to the max function
67-
// Then it should return the max and ignore non-numeric values
62+
// Given an array with non-number values
63+
// When passed to the max function
64+
// Then it should return the max and ignore non-numeric values
6865

6966
it("array with non-number values, returns the max and ignore non-numeric values", () => {
7067
const list = [1, "a", 2, "b", 3, "c"];
7168
expect(findMax(list)).toEqual(3);
7269
});
7370

74-
75-
// Given an array with only non-number values
76-
// When passed to the max function
77-
// Then it should return the least surprising value given how it behaves for all other inputs
71+
// Given an array with only non-number values
72+
// When passed to the max function
73+
// Then it should return the least surprising value given how it behaves for all other inputs
7874

7975
it("array with only non-number values, return the least surprising value given how it behaves for all other inputs", () => {
8076
const list = ["a", "b", "c"];
8177
expect(findMax(list)).toEqual("-Infinity");
8278
});
83-
});
79+
});

Sprint-1/implement/sum.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
function sum(list) {
2-
const numbers = list.filter(n => Number.isFinite(n));
3-
let total = 0;
4-
for (n of numbers) {total += n};
5-
return total;
2+
const numbers = list.filter((n) => Number.isFinite(n));
3+
let total = 0;
4+
for (n of numbers) {
5+
total += n;
6+
}
7+
return total;
68
}
79

810
module.exports = sum;

Sprint-1/implement/sum.test.js

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

1313
describe("sum", () => {
14-
// Given an empty array
15-
// When passed to the sum function
16-
// Then it should return 0
14+
// Given an empty array
15+
// When passed to the sum function
16+
// Then it should return 0
1717
it("empty array returns 0", () => {
1818
const list = [];
1919
expect(sum(list)).toEqual(0);
2020
});
2121

22-
// Given an array with just one number
23-
// When passed to the sum function
24-
// Then it should return that number
22+
// Given an array with just one number
23+
// When passed to the sum function
24+
// Then it should return that number
2525
it("array with one number returns number", () => {
2626
const list = [1];
2727
expect(sum(list)).toEqual(1);
2828
});
2929

30-
// Given an array containing negative numbers
31-
// When passed to the sum function
32-
// Then it should still return the correct total sum
30+
// Given an array containing negative numbers
31+
// When passed to the sum function
32+
// Then it should still return the correct total sum
3333

3434
it("array with negative numbers returns correct sum", () => {
3535
const list = [-1, -2, -3];
3636
expect(sum(list)).toEqual(-6);
3737
});
3838

39-
// Given an array with decimal/float numbers
40-
// When passed to the sum function
41-
// Then it should return the correct total sum
39+
// Given an array with decimal/float numbers
40+
// When passed to the sum function
41+
// Then it should return the correct total sum
4242

4343
it("array with decimal numbers returns correct sum", () => {
4444
const list = [1.1, 2.2, 3.3];
4545
expect(sum(list)).toEqual(6.6);
4646
});
4747

48-
// Given an array containing non-number values
49-
// When passed to the sum function
50-
// Then it should ignore the non-numerical values and return the sum of the numerical elements
48+
// Given an array containing non-number values
49+
// When passed to the sum function
50+
// Then it should ignore the non-numerical values and return the sum of the numerical elements
5151

5252
it("array with non-number values returns sum of numerical values only", () => {
5353
const list = [1, 2, "apple", 4, "banana", 3];
5454
expect(sum(list)).toEqual(10);
5555
});
5656

57-
// Given an array with only non-number values
58-
// When passed to the sum function
59-
// Then it should return the least surprising value given how it behaves for all other inputs
57+
// Given an array with only non-number values
58+
// When passed to the sum function
59+
// Then it should return the least surprising value given how it behaves for all other inputs
6060

6161
it("array with non-number values only returns least surprising output", () => {
6262
const list = ["a", "b", "c"];
6363
expect(sum(list)).toEqual(0);
6464
});
65-
});
65+
});

Sprint-1/refactor/includes.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
// Refactor the implementation of includes to use a for...of loop
22

33
function includes(list, target) {
4-
for (item of list) {if (item === target) {return true;}}
5-
return false;
4+
for (item of list) {
5+
if (item === target) {
6+
return true;
7+
}
8+
}
9+
return false;
610
}
711

812
module.exports = includes;
913

10-
1114
/* Old code:
1215
1316
for (let index = 0; index < list.length; index++) {

0 commit comments

Comments
 (0)