Skip to content

Commit 3e3845b

Browse files
committed
sprint-1 completed
1 parent 96d077b commit 3e3845b

10 files changed

Lines changed: 261 additions & 80 deletions

File tree

Sprint-1/fix/median.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@
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+
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+
}
1119
return median;
1220
}
1321

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: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1-
function dedupe() {}
1+
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+
}
10+
}
11+
12+
module.exports = dedupe;

Sprint-1/implement/dedupe.test.js

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,41 @@ E.g. dedupe(['a','a','a','b','b','c']) target output: ['a','b','c']
1010
E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) target output: [5, 1, 2, 3, 8]
1111
E.g. dedupe([1, 2, 1]) target output: [1, 2]
1212
*/
13+
describe("dedupe", () => {
14+
// Given an empty array
15+
// When passed to the dedupe function
16+
// Then it should return an empty array
17+
it("given an empty array it should return an empty array", () => {
18+
const array = [];
19+
dedupeArray = dedupe(array);
20+
expect(dedupeArray).toEqual([]);
21+
});
1322

14-
// Acceptance Criteria:
15-
16-
// Given an empty array
17-
// When passed to the dedupe function
18-
// Then it should return an empty array
19-
test.todo("given an empty array, it returns an empty array");
20-
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
24-
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
23+
// Given an array with no duplicates
24+
// When passed to the dedupe function
25+
// Then it should return a copy of the original array
26+
[["a", "b", "c"], ["A", 1, "j", "?"], ["c"]].forEach((val) =>
27+
it(`returns copy of the original array if there are no duplicates in [${val}]`, () =>
28+
expect(dedupe(val)).toEqual(val))
29+
);
30+
// Given an array with strings or numbers
31+
// When passed to the dedupe function
32+
// Then it should remove the duplicate values, preserving the first occurrence of each element
33+
[
34+
{ input: [1, 2, 1, 3, 1, 2, 10, 5, 0, 10], expected: [1, 2, 3, 10, 5, 0] },
35+
{ input: [1, 2, 1, 4], expected: [1, 2, 4] },
36+
{ input: [1, 1, 1, 1, 1], expected: [1] },
37+
{
38+
input: ["banana", "apple", "apple", "banana", "apple", "banana"],
39+
expected: ["banana", "apple"],
40+
},
41+
{
42+
input: [" ", "empty", "", " ", "", "empty"],
43+
expected: [" ", "empty", ""],
44+
},
45+
{ input: ["2", "2", "3", "1"], expected: ["2", "3", "1"] },
46+
].forEach(({ input, expected }) =>
47+
it(`returns a copy of array removing the duplicates from [${input}]`, () =>
48+
expect(dedupe(input)).toEqual(expected))
49+
);
50+
});

Sprint-1/implement/max.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
function findMax(elements) {
1+
function findMax(array) {
2+
numbersArray = array.filter((value) => typeof value === "number");
3+
return Math.max(...numbersArray);
24
}
35

46
module.exports = findMax;

Sprint-1/implement/max.test.js

Lines changed: 93 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,96 @@ 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("findMax", () => {
16+
// Given an empty array
17+
// When passed to the max function
18+
// Then it should return -Infinity
19+
it("if an empty array is passed to the the findMax function, -Infinity should be returned", () => {
20+
emptyArray = [];
21+
expect(findMax(emptyArray)).toEqual(-Infinity);
22+
});
23+
24+
// Given an array with one number
25+
// When passed to the max function
26+
// Then it should return that number
27+
[[1], [70], [0], [-25], [100129]].forEach((val) =>
28+
it(`When an array with only one number is passed i.e. [${val}], it should return that number`, () =>
29+
expect(findMax(val)).toEqual(val[0]))
30+
);
31+
32+
// Given an array with both positive and negative numbers
33+
// When passed to the max function
34+
// Then it should return the largest number overall
35+
[
36+
{ input: [12, -2, 4, 6, 0], expected: 12 },
37+
{ input: [2, 5, 990, -4], expected: 990 },
38+
{ input: [0, -1, -5], expected: 0 },
39+
{ input: [0, -1, 300, 3], expected: 300 },
40+
].forEach(({ input, expected }) =>
41+
it(`returns the max number from the array [${input}]`, () =>
42+
expect(findMax(input)).toEqual(expected))
43+
);
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
48+
[
49+
{ input: [-4, -2, -1902, -2, -1], expected: -1 },
50+
{ input: [-9088, -9087, -990788, -4888777], expected: -9087 },
51+
{ input: [-1, -5], expected: -1 },
52+
].forEach(({ input, expected }) =>
53+
it(`returns the max number from the array of negative numbers only [${input}]`, () =>
54+
expect(findMax(input)).toEqual(expected))
55+
);
56+
57+
// Given an array with decimal numbers
58+
// When passed to the max function
59+
// Then it should return the largest decimal number
60+
[
61+
{ input: [-4.2, 2.8, -19.8009, 2.4, 1.5], expected: 2.8 },
62+
{ input: [-90.88, 0.001, -990.788], expected: 0.001 },
63+
{ input: [-1.11, -5.3], expected: -1.11 },
64+
].forEach(({ input, expected }) =>
65+
it(`returns the max number from the array of decimal numbers [${input}]`, () =>
66+
expect(findMax(input)).toEqual(expected))
67+
);
68+
69+
// Given an array with non-number values
70+
// When passed to the max function
71+
// Then it should return the max and ignore non-numeric values
72+
[
73+
{ input: [-4, -2, "what is this", -1902, -2, "??", -1], expected: -1 },
74+
{
75+
input: [-9088, ".oi9e9", "1000000", -9087, 990788, -4888777],
76+
expected: 990788,
77+
},
78+
{ input: [-1.11, "here", -5.233, "ignore me please"], expected: -1.11 },
79+
].forEach(({ input, expected }) =>
80+
it(`returns the max number from the array of numbers and non-numbers values [${input}]`, () =>
81+
expect(findMax(input)).toEqual(expected))
82+
);
83+
84+
// Given an array with only non-number values
85+
// When passed to the max function
86+
// Then it should return the least surprising value given how it behaves for all other inputs
87+
88+
/* Ans: If there is no number in the array than considering the behavior for the above inputs
89+
the least surprising value in return would be -Infinity as our array has zero elements which are numbers*/
90+
[
91+
{
92+
input: ["Not a number", "what is this", "least surprising", "??", "what"],
93+
expected: -Infinity,
94+
},
95+
{
96+
input: ["kkdkas", "Ahan!", "23"],
97+
expected: -Infinity,
98+
},
99+
{
100+
input: ["here", "Least surprising", "????", "vale is", "-Infinity"],
101+
expected: -Infinity,
102+
},
103+
].forEach(({ input, expected }) =>
104+
it(`returns the least surprising value for only non-numbers array [${input}]`, () =>
105+
expect(findMax(input)).toEqual(expected))
106+
);
107+
});

Sprint-1/implement/sum.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
function sum(elements) {
1+
function sum(array) {
2+
numbersArray = array.filter((value) => typeof value === "number");
3+
let sum = 0;
4+
numbersArray.forEach((element) => {
5+
sum += element;
6+
});
7+
return sum;
28
}
39

410
module.exports = sum;

Sprint-1/implement/sum.test.js

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,71 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical
88

99
const sum = require("./sum.js");
1010

11-
// Acceptance Criteria:
11+
describe("sum", () => {
12+
// Given an empty array
13+
// When passed to the sum function
14+
// Then it should return 0
15+
it("0 should be return when an empty array is passed", () =>
16+
expect(sum([])).toEqual(0));
1217

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")
18+
// Given an array with just one number
19+
// When passed to the sum function
20+
// Then it should return that number
21+
[[1], [22], [-29], [0]].forEach((input) =>
22+
it(`an array with just one number [${input}] should return that number`, () =>
23+
expect(sum(input)).toEqual(input[0]))
24+
);
1725

18-
// Given an array with just one number
19-
// When passed to the sum function
20-
// Then it should return that number
26+
// Given an array containing negative numbers
27+
// When passed to the sum function
28+
// Then it should still return the correct total sum
29+
[
30+
{ input: [-1, -2, -100], expected: -103 },
31+
{ input: [-26, 0, -90], expected: -116 },
32+
{ input: [20, -20, 0, 45], expected: 45 },
33+
].forEach(({ input, expected }) =>
34+
it(`array containing negative numbers [${input}] should return the correct sum`, () =>
35+
expect(sum(input)).toEqual(expected))
36+
);
2137

22-
// Given an array containing negative numbers
23-
// When passed to the sum function
24-
// Then it should still return the correct total sum
38+
// Given an array with decimal/float numbers
39+
// When passed to the sum function
40+
// Then it should return the correct total sum
41+
[
42+
{ input: [1.1, 2.222, 100.001], expected: 103.32300000000001 },
43+
{ input: [26.55, 0.001, -90.34], expected: -63.789 },
44+
{ input: [-0.01, 45.9], expected: 45.89 },
45+
].forEach(({ input, expected }) =>
46+
it(`array containing decimal/float numbers [${input}] should return the correct sum`, () =>
47+
expect(sum(input)).toEqual(expected))
48+
);
49+
// Given an array containing non-number values
50+
// When passed to the sum function
51+
// Then it should ignore the non-numerical values and return the sum of the numerical elements
52+
[
53+
{
54+
input: [1.1, "what", 2.222, "is", "this???", " ", 100.001],
55+
expected: 103.32300000000001,
56+
},
57+
{ input: [26.55, "Okay", "!!", 0.001, -90.34], expected: -63.789 },
58+
{ input: [1, "12", 45], expected: 46 },
59+
].forEach(({ input, expected }) =>
60+
it(`array containing non-number values [${input}] should ignore them and return the correct sum of numbers in it`, () =>
61+
expect(sum(input)).toEqual(expected))
62+
);
2563

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
64+
// Given an array with only non-number values
65+
// When passed to the sum function
66+
// Then it should return the least surprising value given how it behaves for all other inputs
67+
[
68+
{
69+
input: ["what", "is", "this???", " "],
70+
expected: 0,
71+
},
72+
{ input: ["26.55", "Okay", "!!"], expected: 0 },
73+
{ input: ["write", "whatever", "%-959"], expected: 0 },
74+
].forEach(({ input, expected }) =>
75+
it(`array containing only non-number values [${input}] should should return 0`, () =>
76+
expect(sum(input)).toEqual(expected))
77+
);
78+
});

Sprint-1/refactor/includes.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
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-
}
4+
for (const element of list) {
5+
if (element === target) return true;
96
}
107
return false;
118
}

0 commit comments

Comments
 (0)