Skip to content

Commit bddbceb

Browse files
Complete all Sprint-1 exercises
1 parent 96d077b commit bddbceb

8 files changed

Lines changed: 129 additions & 15 deletions

File tree

Sprint-1/fix/median.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +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];
11-
return median;
9+
if(!Array.isArray(list)){return null;}
10+
const number = list.filter(n => typeof n === "number");
11+
if(number.length === 0) {return null;}
12+
number.sort((a,b) => a-b)
13+
const middleIndex = Math.floor(number.length / 2);
14+
15+
16+
if(number.length%2===0){return (number[middleIndex-1] + number[middleIndex])/2;
17+
}
18+
else return number[middleIndex];
1219
}
1320

14-
module.exports = calculateMedian;
21+
22+
23+
module.exports = calculateMedian

Sprint-1/implement/dedupe.js

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

Sprint-1/implement/dedupe.test.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,28 @@ 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+
test("given an empty array, it returns an empty array", () => {
20+
const array = [];
21+
const currentOutput = dedupe(array);
22+
const targetOutput = [];
23+
expect(currentOutput).toEqual(targetOutput);
24+
});
2025

2126
// Given an array with no duplicates
2227
// When passed to the dedupe function
2328
// Then it should return a copy of the original array
24-
29+
test("given an array with no duplication, it copy of the original array ", () => {
30+
const array = [1, 2, 3];
31+
const currentOutput = dedupe(array);
32+
const targetOutput = [1, 2, 3];
33+
expect(currentOutput).toEqual(targetOutput);
34+
});
2535
// Given an array with strings or numbers
2636
// When passed to the dedupe function
2737
// Then it should remove the duplicate values, preserving the first occurence of each element
38+
test("given an array with string , it removes duplication ", () => {
39+
const array = ["a", "a", 1, 1, "ba", 4];
40+
const currentOutput = dedupe(array);
41+
const targetOutput = ["a", 1, "ba", 4];
42+
expect(currentOutput).toEqual(targetOutput);
43+
});

Sprint-1/implement/max.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
function findMax(elements) {
2+
let maximumNumber = -Infinity;
3+
for (const item of elements) {
4+
if (typeof item === "number" && item > maximumNumber) {
5+
maximumNumber = item;
6+
}
7+
}
8+
return maximumNumber;
29
}
310

411
module.exports = findMax;

Sprint-1/implement/max.test.js

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,65 @@ 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+
test("given an empty array, returns -Infinity", () => {
20+
const maxElement = [];
21+
const currentOutput = findMax(maxElement);
22+
const targetOutput = -Infinity;
23+
expect(currentOutput).toEqual(targetOutput);
24+
});
2025

2126
// Given an array with one number
2227
// When passed to the max function
2328
// Then it should return that number
29+
test("given an array with one number, returns that number", () => {
30+
const maxElement = [1];
31+
const currentOutput = findMax(maxElement);
32+
const targetOutput = 1;
33+
expect(currentOutput).toEqual(targetOutput);
34+
});
2435

2536
// Given an array with both positive and negative numbers
2637
// When passed to the max function
2738
// Then it should return the largest number overall
28-
39+
test("given an array with one both positive and negative numbers , return largest number", () => {
40+
const maxElement = [2, -4, 8, -1];
41+
const currentOutput = findMax(maxElement);
42+
const targetOutput = 8;
43+
expect(currentOutput).toEqual(targetOutput);
44+
});
2945
// Given an array with just negative numbers
3046
// When passed to the max function
3147
// Then it should return the closest one to zero
32-
48+
test("given an array with negative numbers, returns the closest to zero", () => {
49+
const maxElement = [-1, -4, -12];
50+
const currentOutput = findMax(maxElement);
51+
const targetOutput = -1;
52+
expect(currentOutput).toEqual(targetOutput);
53+
});
3354
// Given an array with decimal numbers
3455
// When passed to the max function
3556
// Then it should return the largest decimal number
36-
57+
test("given an array with decimal numbers returns the largest decimal number", () => {
58+
const maxElement = [2.5, 4.5, 9.8];
59+
const currentOutput = findMax(maxElement);
60+
const targetOutput = 9.8;
61+
expect(currentOutput).toEqual(targetOutput);
62+
});
3763
// Given an array with non-number values
3864
// When passed to the max function
3965
// Then it should return the max and ignore non-numeric values
40-
66+
test("given an array with one non-number return the max and ignore non-numeric value, ", () => {
67+
const maxElement = ["hello", 4, 8, 10];
68+
const currentOutput = findMax(maxElement);
69+
const targetOutput = 10;
70+
expect(currentOutput).toEqual(targetOutput);
71+
});
4172
// Given an array with only non-number values
4273
// When passed to the max function
4374
// Then it should return the least surprising value given how it behaves for all other inputs
75+
test("given an array with only non-number values returns -Infinity", () => {
76+
const maxElement = ["history", "nation"];
77+
const currentOutput = findMax(maxElement);
78+
const targetOutput = -Infinity;
79+
expect(currentOutput).toEqual(targetOutput);
80+
});

Sprint-1/implement/sum.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
function sum(elements) {
2+
let total = 0;
3+
for (const element of elements) {
4+
if (typeof element === "number") {
5+
total += element;
6+
}
7+
}
8+
return total;
29
}
310

411
module.exports = sum;

Sprint-1/implement/sum.test.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,59 @@ const sum = require("./sum.js");
1313
// Given an empty array
1414
// When passed to the sum function
1515
// Then it should return 0
16-
test.todo("given an empty array, returns 0")
16+
test("given an empty array, returns 0", () => {
17+
const list = [];
18+
const currentOutput = sum(list);
19+
const targetOutput = 0;
20+
expect(currentOutput).toEqual(targetOutput);
21+
});
1722

1823
// Given an array with just one number
1924
// When passed to the sum function
2025
// Then it should return that number
26+
test("given an array with one number, returns that number", () => {
27+
const list = [20];
28+
const currentOutput = sum(list);
29+
const targetOutput = 20;
30+
expect(currentOutput).toEqual(targetOutput);
31+
});
2132

2233
// Given an array containing negative numbers
2334
// When passed to the sum function
2435
// Then it should still return the correct total sum
36+
test("given an array containing negative numbers[-20, 30, -50] , returns the correct total sum ", () => {
37+
const list = [-20, 30, -50];
38+
const currentOutput = sum(list);
39+
const targetOutput = -40;
40+
expect(currentOutput).toEqual(targetOutput);
41+
});
2542

2643
// Given an array with decimal/float numbers
2744
// When passed to the sum function
2845
// Then it should return the correct total sum
46+
test("given an array with decimal/float numbers [1.5, -2.4, 4.5], returns the correct total sum", () => {
47+
const list = [1.5, -2.4, 4.5];
48+
const currentOutput = sum(list);
49+
const targetOutput = 3.6;
50+
expect(currentOutput).toEqual(targetOutput);
51+
});
2952

3053
// Given an array containing non-number values
3154
// When passed to the sum function
3255
// Then it should ignore the non-numerical values and return the sum of the numerical elements
56+
test("given an array containing non-numerical value, ignore none-numerical value, returns the sum of numerical elements", () => {
57+
const list = [-20, "hello", 30, "a"];
58+
const currentOutput = sum(list);
59+
const targetOutput = 10;
60+
expect(currentOutput).toEqual(targetOutput);
61+
});
3362

3463
// Given an array with only non-number values
3564
// When passed to the sum function
3665
// Then it should return the least surprising value given how it behaves for all other inputs
66+
test("given an array with only non-number values, returns the least surprising value based on the behavior of other inputs", () => {
67+
const list = ["a", "b", null, undefined];
68+
const currentOutput = sum(list);
69+
const targetOutput = 0;
70+
expect(currentOutput).toEqual(targetOutput);
71+
});

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)