Skip to content

Commit d6361e9

Browse files
committed
All tasks under Sprint -1 is completed and i put all together as Cjyuan suggests
1 parent de6889c commit d6361e9

8 files changed

Lines changed: 91 additions & 94 deletions

File tree

‎Sprint-1/fix/median.js‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,28 @@
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+
// Filter out non-numeric values
10+
// const numbers = list.filter((x) => typeof x === "number");
11+
const numbers = [];
12+
for (const x of list) {
13+
if (Number.isFinite(x)) {
14+
numbers.push(x);
15+
}
16+
}
17+
if (numbers.length === 0) {
18+
return null;
19+
}
20+
// Sort the numbers
21+
numbers.sort((a, b) => a - b);
22+
const middleIndex = Math.floor(numbers.length / 2);
23+
// for even length arrays
24+
if (numbers.length % 2 === 0) {
25+
const left = numbers[middleIndex - 1];
26+
const right = numbers[middleIndex];
27+
return (left + right) / 2;
28+
}
29+
//console.log(`middleIndex: ${middleIndex}`);
30+
const median = numbers[middleIndex];
1131
return median;
1232
}
13-
1433
module.exports = calculateMedian;

‎Sprint-1/implement/dedupe.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
function dedupe() {}
1+
function dedupe(value) {
2+
const newDedupe = [...new Set(value)];
3+
return newDedupe;
4+
}
5+
6+
module.exports = dedupe;

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

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
11
const dedupe = require("./dedupe.js");
2-
/*
3-
Dedupe Array
42

5-
📖 Dedupe means **deduplicate**
3+
test("given an empty array, it returns an empty array", () => {
4+
expect(dedupe([])).toEqual([]);
5+
});
66

7-
In this kata, you will need to deduplicate the elements of an array
7+
test("given an array with unique elements, it returns the same array", () => {
8+
expect(dedupe([2, 1, 4, "2", "d", "g"])).toEqual([2, 1, 4, "2", "d", "g"]);
9+
});
810

9-
E.g. dedupe(['a','a','a','b','b','c']) returns ['a','b','c']
10-
E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) returns [5, 1, 2, 3, 8]
11-
E.g. dedupe([1, 2, 1]) returns [1, 2]
12-
*/
13-
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 of strings or numbers
26-
// When passed to the dedupe function
27-
// Then it should return a new array with duplicates removed while preserving the
28-
// first occurrence of each element from the original array.
11+
test("given an array of strings or numbers then it should return new array with duplicates removed", () => {
12+
expect(dedupe([2, 2, 4, 2, 3, 4, 8, 5, 4, 1, 9])).toEqual([
13+
2, 4, 3, 8, 5, 1, 9,
14+
]);
15+
});

‎Sprint-1/implement/max.js‎

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

46
module.exports = findMax;

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

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,33 @@
1-
/* Find the maximum element of an array of numbers
2-
3-
In this kata, you will need to implement a function that find the largest numerical element of an array.
4-
5-
E.g. max([30, 50, 10, 40]), target output: 50
6-
E.g. max(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-numerical elements)
7-
8-
You should implement this function in max.js, and add tests for it in this file.
9-
10-
We have set things up already so that this file can see your function from the other file.
11-
*/
12-
131
const findMax = require("./max.js");
142

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");
3+
test("Given an array with only +ve numbers and should return the largest of them", () => {
4+
expect(findMax([2, 4, 2, 1, 5])).toEqual(5);
5+
});
6+
7+
test("Given an empty array should return -Infinity", () => {
8+
expect(findMax([])).toEqual(-Infinity);
9+
});
2010

21-
// Given an array with one number
22-
// When passed to the max function
23-
// Then it should return that number
11+
test("Given an array with one number only and it should return that number", () => {
12+
expect(findMax([7])).toEqual(7);
13+
});
2414

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
15+
test("Given an array with both positive and negative numbers and it should return the largest number", () => {
16+
expect(findMax([1, -3, -2, 3, 4])).toEqual(4);
17+
});
2818

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
19+
test("Given an array with negative numbers and it should return the largest number", () => {
20+
expect(findMax([-2, -5, -5, -3, -10])).toEqual(-2);
21+
});
3222

33-
// Given an array with decimal numbers
34-
// When passed to the max function
35-
// Then it should return the largest decimal number
23+
test("Given an array with decimal numbers and it should return the largest decimal numbers", () => {
24+
expect(findMax([2.7, 2.01, 4.4, 5.2])).toEqual(5.2);
25+
});
3626

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
27+
test("Given an array a mix of non-numeric values and numeric values and it should return the largest from only the numeric values and ignore the non-numeric ones", () => {
28+
expect(findMax([2, "a", 8, "12", 8.1])).toEqual(8.1);
29+
});
4030

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
31+
test("Given an array with only non-numeric values and it should return -Infinity", () => {
32+
expect(findMax(["a", "e", "I", "o", "U"])).toEqual(-Infinity);
33+
});

‎Sprint-1/implement/sum.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
function sum(elements) {
2+
let total = 0;
3+
elements = elements.filter((x) => typeof x === "number");
4+
for (let i = 0; i < elements.length; i++) {
5+
total += elements[i];
6+
}
7+
return total;
28
}
39

410
module.exports = sum;

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

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,25 @@
1-
/* Sum the numbers in an array
2-
3-
In this kata, you will need to implement a function that sums the numerical elements of an array
4-
5-
E.g. sum([10, 20, 30]), target output: 60
6-
E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical elements)
7-
*/
8-
91
const sum = require("./sum.js");
102

11-
// Acceptance Criteria:
12-
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")
3+
test("given an empty array, it returns an empty array", () => {
4+
expect(sum([])).toEqual(0);
5+
});
176

18-
// Given an array with just one number
19-
// When passed to the sum function
20-
// Then it should return that number
7+
test("Given an array with just one number to the sum function Then it should return that number", () => {
8+
expect(sum([3])).toEqual(3);
9+
});
2110

22-
// Given an array containing negative numbers
23-
// When passed to the sum function
24-
// Then it should still return the correct total sum
11+
test("given an array containing negative numbers to the sum function then it should return the correct total", () => {
12+
expect(sum([1, -1, -2, -3])).toEqual(-5);
13+
});
2514

26-
// Given an array with decimal/float numbers
27-
// When passed to the sum function
28-
// Then it should return the correct total sum
15+
test("given an array with decimal/float numbers should return the correct total sum", () => {
16+
expect(sum([2, 2.5])).toEqual(4.5);
17+
});
2918

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
19+
test("given an array non-number value should return the correct total of only the numeric ones", () => {
20+
expect(sum([2, "2", "a", "b", 3])).toEqual(5);
21+
});
3322

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
23+
test("given an array non-number value should return 0", () => {
24+
expect(sum(["a", "c", "f", "3"])).toEqual(0);
25+
});

‎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)