Skip to content

Commit dc8fa61

Browse files
all tasks completed for data groups sprint 1
1 parent 4222cda commit dc8fa61

8 files changed

Lines changed: 117 additions & 19 deletions

File tree

‎Sprint-1/fix/median.js‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,25 @@
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+
if (!Array.isArray(list)) {
10+
return null;
11+
}
12+
const list2 = list.filter(item => typeof item === "number").sort( (a,b) => (a - b));
13+
if (list2.length === 0) {
14+
return null;
15+
}
16+
if(list2.length % 2 === 1){
17+
const middleIndex = Math.floor(list2.length / 2);
18+
const median = list2[middleIndex];
19+
return median;
20+
}
21+
if(list2.length % 2 === 0){
22+
const middleIndex = Math.floor(list2.length / 2);
23+
const median = (list2[middleIndex] + list2[middleIndex - 1]) / 2;
1124
return median;
25+
}
26+
1227
}
1328

29+
1430
module.exports = calculateMedian;

‎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(input) {
2+
const newArray = []
3+
if(input.length === 0){
4+
return []
5+
}
6+
for(const i in input)
7+
for(const j in input)
8+
if (i !== j && !newArray.includes(input[i]))
9+
newArray.push(input[i])
10+
return newArray;
11+
}
12+
module.exports = dedupe;

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,25 @@ E.g. dedupe([1, 2, 1]) returns [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+
expect(dedupe([])).toEqual([]);
21+
});
2022

2123
// Given an array with no duplicates
2224
// When passed to the dedupe function
2325
// Then it should return a copy of the original array
26+
test(`given an array with no duplicates, it should return the same array`, () => {
27+
expect(dedupe(['a','b','c'])).toEqual(['a','b','c']);
28+
expect(dedupe(['1','2','3'])).toEqual(['1','2','3']);
29+
30+
});
2431

2532
// Given an array of strings or numbers
2633
// When passed to the dedupe function
2734
// Then it should return a new array with duplicates removed while preserving the
2835
// first occurrence of each element from the original array.
36+
test(`given an array with duplicates, it should return the array with duplicates removed preserving the first occurrence`, () => {
37+
expect(dedupe(['b','c','a','b','c', 'b','c'])).toEqual(['b','c','a']);
38+
expect(dedupe(['1','2','2','3','1','2','3'])).toEqual(['1','2','3']);
39+
40+
});

‎Sprint-1/implement/max.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
function findMax(elements) {
2+
if (elements.length === 0){
3+
return -Infinity;
4+
}
5+
const cleanElements = elements.filter(item => typeof item === "number").sort( (a,b) => (a - b));
6+
if (cleanElements.length === 0){
7+
return -Infinity;
8+
}
9+
else{
10+
return cleanElements[cleanElements.length-1]
11+
}
212
}
3-
413
module.exports = findMax;

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,46 @@ 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, it returns -Infinity`, () => {
20+
expect(findMax([])).toEqual(-Infinity);
21+
});
2022

2123
// Given an array with one number
2224
// When passed to the max function
2325
// Then it should return that number
24-
26+
test(`given an array with one number, it should return that number`, () => {
27+
expect(findMax([80])).toEqual(80);
28+
expect(findMax([1])).toEqual(1);
29+
});
2530
// Given an array with both positive and negative numbers
2631
// When passed to the max function
2732
// Then it should return the largest number overall
28-
33+
test(`given an array with both positive numbers and negative numbers, it should return the largest number overall`, () => {
34+
expect(findMax([80, -30, 50, -10, 20, 100, 40])).toEqual(100);
35+
expect(findMax([-90, 30, 250, 1, 120, -100, -40])).toEqual(250);
36+
});
2937
// Given an array with just negative numbers
3038
// When passed to the max function
3139
// Then it should return the closest one to zero
32-
40+
test(`given an array with negative numbers, it should return the addition of them all`, () => {
41+
expect(findMax([-80, -30, -50, -10, -20, -100, -40])).toEqual(-10);
42+
});
3343
// Given an array with decimal numbers
3444
// When passed to the max function
3545
// Then it should return the largest decimal number
36-
46+
test(`given an array with decimal numbers, it should return the largest decimal number`, () => {
47+
expect(findMax([-80.5, -30.1, 50.7, -10, 20.4, 10.3, 60.1])).toEqual(60.1);
48+
});
3749
// Given an array with non-number values
3850
// When passed to the max function
3951
// Then it should return the max and ignore non-numeric values
40-
52+
test(`given an array of numeric and non-numeric values, it should return the largest numeric`, () => {
53+
expect(findMax(['hey', 10, 'hi', 60, 10])).toEqual(60);
54+
expect(findMax(['i', 'love', 10, 'code', 'hi', 80, 60, 'your', 'future', 10])).toEqual(80);
55+
});
4156
// Given an array with only non-number values
4257
// When passed to the max function
4358
// Then it should return the least surprising value given how it behaves for all other inputs
59+
test(`given an array of non-numeric values, it should return -Infinity`, () => {
60+
expect(findMax(['i', 'love', 'code', 'your', 'future'])).toEqual(-Infinity);
61+
});

‎Sprint-1/implement/sum.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
function sum(elements) {
2+
if (elements.length === 0){
3+
return 0;
24
}
5+
const cleanElements = elements.filter(item => typeof item === "number").sort( (a,b) => (a - b));
6+
let sum = 0
7+
if (cleanElements.length === 0){
8+
return 0;
9+
}
10+
else{
11+
for(let i = 0; i < cleanElements.length; i++)
12+
sum += cleanElements[i]
13+
14+
return sum;
315

16+
}
17+
}
418
module.exports = sum;

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,39 @@ 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, it returns zero`, () => {
17+
expect(sum([])).toEqual(0);
18+
});
1719

1820
// Given an array with just one number
1921
// When passed to the sum function
2022
// Then it should return that number
21-
23+
test(`given an array with one number, it should return that number`, () => {
24+
expect(sum([80])).toEqual(80);
25+
expect(sum([1])).toEqual(1);
26+
});
2227
// Given an array containing negative numbers
2328
// When passed to the sum function
2429
// Then it should still return the correct total sum
25-
30+
test(`given an array with negative numbers, it should return the addition of them all`, () => {
31+
expect(sum([-80, -30, -50, -10, -20, -100, -40])).toEqual(-330);
32+
});
2633
// Given an array with decimal/float numbers
2734
// When passed to the sum function
2835
// Then it should return the correct total sum
29-
36+
test(`given an array with decimal numbers, it should return the total`, () => {
37+
expect(sum([-80.5, -30.1, 50.7, -10, 20.4, 10.3, 60.1])).toEqual(20.9);
38+
});
3039
// Given an array containing non-number values
3140
// When passed to the sum function
3241
// Then it should ignore the non-numerical values and return the sum of the numerical elements
33-
42+
test(`given an array of numeric and non-numeric values, it should return the total of the numeric values`, () => {
43+
expect(sum(['hey', 10, 'hi', 60, 10])).toEqual(80);
44+
expect(sum(['i', 'love', 10, 'code', 'hi', 80, 60, 'your', 'future', 10])).toEqual(160);
45+
});
3446
// Given an array with only non-number values
3547
// When passed to the sum function
3648
// Then it should return the least surprising value given how it behaves for all other inputs
49+
test(`given an array of non-numeric values, it should return zero`, () => {
50+
expect(sum(['i', 'love', 'code', 'your', 'future'])).toEqual(0);
51+
})

‎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 (let index = 0; index < list.length; index++) {
5-
const element = list[index];
4+
for (const item of list) {
5+
const element = item;
66
if (element === target) {
77
return true;
88
}
99
}
1010
return false;
1111
}
12+
console.log(includes(["a", "b", "c", "d"], "c"))
1213

13-
module.exports = includes;
14+
15+
16+
module.exports = includes;

0 commit comments

Comments
 (0)