Skip to content

Commit e969dec

Browse files
committed
Sprint-1
1 parent aa7da66 commit e969dec

File tree

8 files changed

+147
-12
lines changed

8 files changed

+147
-12
lines changed

Sprint-1/fix/median.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,22 @@
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)){ //check if variable is array
10+
return null;
11+
}
12+
const numbersOnly = list.filter(item => typeof item === "number" && !isNaN(item));
13+
if (numbersOnly.length == 0){
14+
return null;
15+
}
16+
const sorted =[...numbersOnly].sort((a,b) => a-b ); //we created a new arrey where we saved sorted element (as string) and turned them to numbers.
17+
18+
const middleIndex = Math.floor(sorted.length / 2);
19+
if (sorted.length % 2 !== 0){ //for odds length of arrey
20+
return sorted[middleIndex]
21+
}
22+
else{
23+
return (sorted[middleIndex -1] + sorted[middleIndex]) / 2; //for even length of arrey
24+
}
1225
}
1326

1427
module.exports = calculateMedian;

Sprint-1/implement/dedupe.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
function dedupe() {}
1+
function dedupe(array) {
2+
if (array.length == 0){ // check if array is empty
3+
return [];
4+
}
5+
let mySet = new Set(array); // create set mySet
6+
let dedupeArray = Array.from (mySet); // create new array from the set mySet
7+
return dedupeArray;
8+
}
9+
10+
module.exports = dedupe;

Sprint-1/implement/dedupe.test.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,29 @@ 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 currentOutput = dedupe([]);
21+
const targetOutput = [];
22+
expect(currentOutput).toEqual(targetOutput);
23+
});
2024

2125
// Given an array with no duplicates
2226
// When passed to the dedupe function
2327
// Then it should return a copy of the original array
24-
28+
test("given an array with no duplicates, it should return a copy of the original array", () => {
29+
const currentOutput = dedupe([5, 1, 2, 3, 8]);
30+
const targetOutput = [5, 1, 2, 3, 8];
31+
expect(currentOutput).toEqual(targetOutput);
32+
});
2533
// Given an array with strings or numbers
2634
// When passed to the dedupe function
2735
// Then it should remove the duplicate values, preserving the first occurence of each element
36+
test("given an array with strings or number, it should remove the duplicate values, preserving the first occurence of each element", () => {
37+
const currentOutput = dedupe([5, "1", "1", 5, 8]);
38+
const targetOutput = [5, "1", 8];
39+
40+
console.log(currentOutput);
41+
console.log(targetOutput);
42+
43+
expect(currentOutput).toEqual(targetOutput);
44+
});

Sprint-1/implement/max.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
function findMax(elements) {
2+
//filter array for numbersOnly
3+
const numbersOnly = elements.filter(item => !isNaN(item));
4+
if (numbersOnly.length == 0){ // check if array is empty
5+
return -Infinity;
6+
}
7+
if (numbersOnly.length === 1){ //check if they has one element
8+
return numbersOnly[0];
9+
}
10+
let max = numbersOnly[0];// find a max number
11+
for (let i=1; i<numbersOnly.length; i++){
12+
if (numbersOnly[i]> max){
13+
max = numbersOnly[i];
14+
}
15+
}
16+
return max;
217
}
318

419
module.exports = findMax;

Sprint-1/implement/max.test.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,63 @@ 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+
20+
test("given an empty array, it should return -Infinity", () => {
21+
const currentOutput = findMax([]);
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, it should return that number", () => {
30+
const currentOutput = findMax([1]);
31+
const targetOutput = 1;
32+
expect(currentOutput).toEqual(targetOutput);
33+
});
2434

2535
// Given an array with both positive and negative numbers
2636
// When passed to the max function
2737
// Then it should return the largest number overall
38+
test("Given an array with both positive and negative numbers it should return the largest number overall", () => {
39+
const currentOutput = findMax([-2, 2, 3, -3]);
40+
const targetOutput = 3;
41+
expect(currentOutput).toEqual(targetOutput);
42+
});
2843

2944
// Given an array with just negative numbers
3045
// When passed to the max function
3146
// Then it should return the closest one to zero
47+
test("Given an array with just negative numbers, it should returnthe closest one to zero", () => {
48+
const currentOutput = findMax([-1, -2, -3, -4]);
49+
const targetOutput = -1;
50+
expect(currentOutput).toEqual(targetOutput);
51+
});
3252

3353
// Given an array with decimal numbers
3454
// When passed to the max function
3555
// Then it should return the largest decimal number
56+
test("given an array with decimal numbers, it should return the largest decimal number", () => {
57+
const currentOutput = findMax([0.2, 0.45, 5.3, 1.5]);
58+
const targetOutput = 5.3;
59+
expect(currentOutput).toEqual(targetOutput);
60+
});
3661

3762
// Given an array with non-number values
3863
// When passed to the max function
3964
// Then it should return the max and ignore non-numeric values
65+
test("given an array with non-number values, it should return the max and ignore non-numeric values", () => {
66+
const currentOutput = findMax(["a", "!", 3, 5, "0"]);
67+
const targetOutput = 5;
68+
expect(currentOutput).toEqual(targetOutput);
69+
});
4070

4171
// Given an array with only non-number values
4272
// When passed to the max function
4373
// Then it should return the least surprising value given how it behaves for all other inputs
74+
test("given an array with only non-number values, it should return the least surprising value given how it behaves for all other inputs", () => {
75+
const currentOutput = findMax(["2", "3", "$", "%"]);
76+
const targetOutput = "3";
77+
expect(currentOutput).toEqual(targetOutput);
78+
});

Sprint-1/implement/sum.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
function sum(elements) {
2+
if (elements.length == 0){ // check if array is empty
3+
return 0;
4+
}
5+
if (elements.length === 1){ //check if array has one element
6+
return elements[0];
7+
}
8+
let sum = 0;
9+
let isNumbersPresent = elements.some(item => typeof item === 'number' && !isNaN(item));
10+
for (let i =0; i<elements.length; i++){ //sum of elements
11+
if (typeof elements[i] === 'number'){ //check if element is number
12+
sum += elements[i];
13+
}
14+
if (isNumbersPresent == false && !isNaN(elements[i])){ //1) check if array doesnt have numbers; 2)check if element can be conversted to number
15+
sum += parseFloat(elements[i]); // add converted to num element
16+
}
17+
}
18+
return sum
219
}
320

421
module.exports = sum;

Sprint-1/implement/sum.test.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,61 @@ E.g. sum([10, 20, 30]), target output: 60
66
E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical elements)
77
*/
88

9-
const sum = require("./sum.js");
9+
const sum = require("./sum.js");
1010

1111
// Acceptance Criteria:
1212

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+
17+
test("given an empty array, it should return 0", () => {
18+
const currentOutput = sum ([]);
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 just one number, it should return that number", () => {
27+
const currentOutput = sum ([1]);
28+
const targetOutput = 1;
29+
expect(currentOutput).toEqual(targetOutput);
30+
});
2131

2232
// Given an array containing negative numbers
2333
// When passed to the sum function
2434
// Then it should still return the correct total sum
35+
test("given an array containing negative numbers, it should still return the correct total sum", () => {
36+
const currentOutput = sum ([-2, -3, -4, -5]);
37+
const targetOutput = -14;
38+
expect(currentOutput).toEqual(targetOutput);
39+
});
2540

2641
// Given an array with decimal/float numbers
2742
// When passed to the sum function
2843
// Then it should return the correct total sum
44+
test("given an array with decimal/float numbers, it should return the correct total sum", () => {
45+
const currentOutput = sum ([2.5, 3.1, 4.9]);
46+
const targetOutput = 10.5;
47+
expect(currentOutput).toEqual(targetOutput);
48+
});
2949

3050
// Given an array containing non-number values
3151
// When passed to the sum function
3252
// Then it should ignore the non-numerical values and return the sum of the numerical elements
53+
test("given an array containing non-number values, it should return the sum of the numerical elements", () => {
54+
const currentOutput = sum (["a", "!", 3, 5, ","]);
55+
const targetOutput = 8;
56+
expect(currentOutput).toEqual(targetOutput);
57+
});
3358

3459
// Given an array with only non-number values
3560
// When passed to the sum function
3661
// Then it should return the least surprising value given how it behaves for all other inputs
62+
test("given an array with only non-number values, it should return the least surprising value given how it behaves for all other inputs", () => {
63+
const currentOutput = sum (["a", "!", "3", "5", ","]);
64+
const targetOutput = 8;
65+
expect(currentOutput).toEqual(targetOutput);
66+
});

Sprint-1/refactor/includes.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +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) {
4+
for(const element of list){
5+
if (element === target) {
76
return true;
87
}
98
}

0 commit comments

Comments
 (0)