Skip to content

Commit 735f291

Browse files
committed
write and implement function and test case
1 parent 96d077b commit 735f291

8 files changed

Lines changed: 230 additions & 57 deletions

File tree

Sprint-1/fix/median.js

Lines changed: 17 additions & 3 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-
const middleIndex = Math.floor(list.length / 2);
10-
const median = list.splice(middleIndex, 1)[0];
11-
return median;
9+
if (!Array.isArray(list)) return null;
10+
11+
const numbers = [];
12+
for (const x of list) {
13+
if (typeof x === "number" && !isNaN(x)) {
14+
numbers.push(Number(x));
15+
}
16+
}
17+
if (numbers.length === 0) return null;
18+
numbers.sort((a, b) => a - b);
19+
const length = numbers.length;
20+
const middleIndex = Math.floor(length / 2);
21+
if (length % 2 === 0) {
22+
return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2;
23+
} else {
24+
return numbers[middleIndex];
25+
}
1226
}
1327

1428
module.exports = calculateMedian;

Sprint-1/implement/dedupe.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1-
function dedupe() {}
1+
function dedupe(arr) {
2+
3+
if(arr.length===0) return arr;
4+
const dedupeArray=[]
5+
for(let i=0;i<arr.length;i++){
6+
7+
if(!dedupeArray.includes(arr[i])){
8+
dedupeArray.push(arr[i])
9+
}
10+
}
11+
return dedupeArray;
12+
13+
14+
}
15+
module.exports=dedupe;

Sprint-1/implement/dedupe.test.js

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const dedupe = require("./dedupe.js");
1+
const dedupe = require("./dedupe");
22
/*
33
Dedupe Array
44
@@ -16,12 +16,41 @@ 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+
[{ input: [], expected: [] }].forEach(({ input, expected }) =>
21+
it(`given an empty array, it returns an empty array [${input}]`, () => {
22+
expect(dedupe(input)).toEqual(expected);
23+
})
24+
);
25+
// Given an array with no duplicates
26+
// Then it should return a copy of the original array
27+
[
28+
{ input: [1, 2, 3, 4], expected: [1, 2, 3, 4] },
29+
{
30+
input: ["apples", "banana", "orange"],
31+
expected: ["apples", "banana", "orange"],
32+
},
33+
{ input: [-1, 7, 1], expected: [-1, 7, 1] },
34+
].forEach(({ input, expected }) =>
35+
it(`should return same input values [${input}] without duplicate`, () => {
36+
expect(dedupe(input)).toEqual(expected);
37+
}));
2038

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
39+
// When passed to the dedupe function
40+
// Given an array with strings or numbers
41+
// When passed to the dedupe function
42+
// Then it should remove the duplicate values, preserving the first occurence of each element
2443

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
44+
[
45+
{ input: [1, 2, 5, 5, "a", 5, 10, 10, "a"], expected: [1, 2, 5, "a", 10] },
46+
{
47+
input: ["apple", "banana", "orange", "apple", "banana", 1, 3, 4, 1],
48+
expected: ["apple", "banana", "orange", 1, 3,4],
49+
},
50+
].forEach(({ input, expected }) =>
51+
it(`should return deduplicated array for [${input}]`, () => {
52+
expect(dedupe(input)).toEqual(expected);
53+
})
54+
);
55+
56+
});

Sprint-1/implement/max.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
function findMax(elements) {
2+
if (!Array.isArray(elements)) return "invalid elements";
3+
if (elements.length === 0) return Infinity;
4+
const number = [];
5+
for (let i = 0; i < elements.length; i++) {
6+
if (typeof elements[i] === "number" && !Number.isNaN(elements[i])) {
7+
number.push(elements[i]);
8+
}
9+
}
10+
if (number.length === 0) return "invalid elements";
11+
let max = number[0];
12+
13+
for (let i = 1; i < number.length; i++) {
14+
if (max < number[i]) {
15+
max = number[i];
16+
}
17+
}
18+
return max;
219
}
320

421
module.exports = findMax;

Sprint-1/implement/max.test.js

Lines changed: 64 additions & 19 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+
describe("findMax()", () => {
20+
[{ input: [], expected: Infinity }].forEach(({ input, expected }) =>
21+
it(`should return ${expected} for empty [${input}]`, () => {
22+
expect(findMax(input)).toEqual(expected);
23+
})
24+
);
2025

21-
// Given an array with one number
22-
// When passed to the max function
23-
// 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
2429

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
30+
[{ input: [50], expected: 50 }].forEach(({ input, expected }) =>
31+
it(`should return ${expected} for array [${input}]`, () => {
32+
expect(findMax(input)).toEqual(expected);
33+
})
34+
);
2835

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
36+
// Given an array with both positive and negative numbers
37+
// When passed to the max function
38+
// Then it should return the largest number overall
3239

33-
// Given an array with decimal numbers
34-
// When passed to the max function
35-
// Then it should return the largest decimal number
40+
[{ input: [2, 5, 6, -1, 0, 25, -30], expected: 25 }].forEach(
41+
({ input, expected }) => {
42+
it(`should return ${expected} for positive and negative numbers in the array`, () => {
43+
expect(findMax(input)).toEqual(expected);
44+
});
45+
}
46+
);
3647

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
48+
// Given an array with just negative numbers
49+
// When passed to the max function
50+
// Then it should return the closest one to zero
51+
[{ input: [-1, -10, -7, -20], expected: -1 }].forEach(({ input, expected }) =>
52+
it(`should return negative number nearest to zero`, () => {
53+
expect(findMax(input)).toEqual(expected);
54+
})
55+
);
4056

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
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: [0.5, 0.1, 0.56, 0.8], expected: 0.8 }].forEach(
62+
({ input, expected }) =>
63+
it(`should return the largest decimal number from the array`, () => {
64+
expect(findMax(input)).toEqual(expected);
65+
})
66+
);
67+
68+
// Given an array with non-number values
69+
// When passed to the max function
70+
// Then it should return the max and ignore non-numeric values
71+
[
72+
{ input: ["edak", "ofonime", "", "@", -4, 10, 6, 50, -100], expected: 50 },
73+
].forEach(({ input, expected }) =>
74+
it(`should return max numerical value from the array`, () => {
75+
expect(findMax(input)).toEqual(expected);
76+
})
77+
);
78+
79+
// Given an array with only non-number values
80+
// When passed to the max function
81+
// Then it should return the least surprising value given how it behaves for all other inputs
82+
[{ input: ["peter", "", "@", "Hi"], expected: "invalid elements" }].forEach(
83+
({ input, expected }) =>
84+
it(`should return "invalid elements" for non-numeric values`, () => {
85+
expect(findMax(input)).toEqual(expected);
86+
})
87+
);
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 (!Array.isArray(elements)) return "invalid elements";
3+
if(elements.length===0) return 0;
4+
const number = [];
5+
for (const x of elements) {
6+
if (typeof x === "number" && !Number.isNaN(x)) {
7+
number.push(x);
8+
}
9+
}
10+
if(number.length===0) return "invalid elements";
11+
let sumOfNum=0;
12+
for(let i=0; i<number.length; i++){
13+
14+
sumOfNum+=number[i]
15+
}
16+
return sumOfNum
217
}
318

419
module.exports = sum;

Sprint-1/implement/sum.test.js

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,67 @@ 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+
// Given an empty array
15+
// When passed to the sum function
16+
// Then it should return 0
17+
[{ input: [], expected: 0 }].forEach(({ input, expected }) =>
18+
it(`should return ${expected} for [${input}]`, () => {
19+
expect(sum(input)).toEqual(expected);
20+
})
21+
);
22+
23+
// Given an array with just one number
24+
// When passed to the sum function
25+
// Then it should return that number
26+
27+
[{ input: [30], expected: 30 }].forEach(({ input, expected }) =>
28+
it(`should return ${expected} for [${input}]`, () => {
29+
expect(sum(input)).toEqual(expected);
30+
})
31+
);
32+
33+
// Given an array containing negative numbers
34+
// When passed to the sum function
35+
// Then it should still return the correct total sum
36+
37+
[{ input: [-1, -3, -4, -11], expected: -19 }].forEach(({ input, expected }) =>
38+
it(`should return ${expected} for [${input}]`, () => {
39+
expect(sum(input)).toEqual(expected);
40+
})
41+
);
42+
43+
// Given an array with decimal/float numbers
44+
// When passed to the sum function
45+
// Then it should return the correct total sum
46+
47+
[{ input: [0.5, 0.2, 0.11, 0.89, 0.3], expected: 2 }].forEach(
48+
({ input, expected }) =>
49+
it(`should return ${expected} for [${input}]`, () => {
50+
expect(sum(input)).toEqual(expected);
51+
})
52+
);
53+
54+
// Given an array containing non-number values
55+
// When passed to the sum function
56+
// Then it should ignore the non-numerical values and return the sum of the numerical elements
57+
58+
[
59+
{ input: ["evan", 3, "mike", 20, 6, "", "/", , , 20], expected: 49 },
60+
].forEach(({ input, expected }) =>
61+
it(`should return ${expected} for [${input}]`, () => {
62+
expect(sum(input)).toEqual(expected);
63+
})
64+
);
65+
66+
// Given an array with only non-number values
67+
// When passed to the sum function
68+
// Then it should return the least surprising value given how it behaves for all other inputs
69+
[
70+
{ input: ["evan", "mike", "", "/", , ,], expected: "invalid elements" },
71+
].forEach(({ input, expected }) =>
72+
it(`should return ${expected} for [${input}]`, () => {
73+
expect(sum(input)).toEqual(expected);
74+
})
75+
);
76+
});

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 (const element of list) {
65
if (element === target) {
76
return true;
87
}

0 commit comments

Comments
 (0)