Skip to content

Commit 0c3b7bf

Browse files
committed
implemented functions and test cases
1 parent d5a2099 commit 0c3b7bf

6 files changed

Lines changed: 85 additions & 17 deletions

File tree

‎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+
const result = [];
3+
for(let i = 0; i < array.length ; i++){
4+
if(!result.includes(array[i])){
5+
result.push(array[i]);
6+
}
7+
}
8+
return result;
9+
}
10+
module.exports = dedupe;

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@ 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");
20-
19+
//test.todo("given an empty array, it returns an empty array");
20+
test("given an empty array, returns 0", () => {
21+
expect(dedupe([])).toEqual([]);
22+
});
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
24-
26+
test("given an array with no duplicate, returns original array", () => {
27+
expect(dedupe([2,7,9])).toEqual([2,7,9]);
28+
});
2529
// Given an array of strings or numbers
2630
// When passed to the dedupe function
2731
// Then it should return a new array with duplicates removed while preserving the
2832
// first occurrence of each element from the original array.
33+
test("given an array of string or numbers, returns with no duplicates", () => {
34+
expect(dedupe([4, "banana", 4, "banana", 2])).toEqual([4, "banana", 2]);
35+
});

‎Sprint-1/implement/max.js‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
function findMax(elements) {
2+
if (elements.length === 0){
3+
return -Infinity;
4+
}
5+
let max = -Infinity;
6+
for(let item of elements){
7+
if(typeof item !== "number"){
8+
continue;
9+
}
10+
if(item > max){
11+
max = item;
12+
}
13+
}
14+
return max;
215
}
3-
416
module.exports = findMax;

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,43 @@ 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+
expect(findMax([])).toBe(-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 empty array, returns 0", () => {
27+
expect(findMax([78])).toBe(78);
28+
});
2529
// Given an array with both positive and negative numbers
2630
// When passed to the max function
2731
// Then it should return the largest number overall
28-
32+
test("given an array with positive and negative numbers, returns largest number", () => {
33+
expect(findMax([-8, 10, 45, -60])).toBe(45);
34+
});
2935
// Given an array with just negative numbers
3036
// When passed to the max function
3137
// Then it should return the closest one to zero
32-
38+
test("given an array with negative numbers, returns closest to 0", () => {
39+
expect(findMax([-8, -7, -5, -60])).toBe(-5);
40+
});
3341
// Given an array with decimal numbers
3442
// When passed to the max function
3543
// Then it should return the largest decimal number
36-
44+
test("given an array with decimal numbers, returns largest number", () => {
45+
expect(findMax([3.7, 5.8, 4.5, 60.8])).toBe(60.8);
46+
});
3747
// Given an array with non-number values
3848
// When passed to the max function
3949
// Then it should return the max and ignore non-numeric values
40-
50+
test("given an array with non-number values, returns number", () => {
51+
expect(findMax([100, "hello", 94])).toBe(100);
52+
});
4153
// Given an array with only non-number values
4254
// When passed to the max function
4355
// Then it should return the least surprising value given how it behaves for all other inputs
56+
test("given an array with non-numbers, returns -Infinity", () => {
57+
expect(findMax(["hi", "maryam", "here"])).toBe(-Infinity);
58+
});

‎Sprint-1/implement/sum.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
function sum(elements) {
2-
}
2+
let result = 0;
3+
if(elements.length === 0){
4+
return 0;
5+
}
6+
for(let i = 0; i < elements.length; i++){
7+
if(typeof(elements[i]) == "number"){
8+
result = result + elements[i];
9+
}
10+
}
11+
return result;
312

13+
}
414
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.todo("given an empty array, returns 0")
17+
18+
test("given an empty array, returns 0", () => {
19+
expect(sum([])).toBe(0);
20+
});
1721

1822
// Given an array with just one number
1923
// When passed to the sum function
2024
// Then it should return that number
21-
25+
test("given an array with just 1 value, returns that value", () => {
26+
expect(sum([50])).toBe(50);
27+
});
2228
// Given an array containing negative numbers
2329
// When passed to the sum function
2430
// Then it should still return the correct total sum
25-
31+
test("given an array containing negative numbers, returns correct sum", () => {
32+
expect(sum([9, 2, 3, -4])).toBe(10);
33+
});
2634
// Given an array with decimal/float numbers
2735
// When passed to the sum function
2836
// Then it should return the correct total sum
29-
37+
test("given an array with decimal numbers, returns correct sum", () => {
38+
expect(sum([4.5, 9.2, 9, 3.2])).toBe(25.9);
39+
});
3040
// Given an array containing non-number values
3141
// When passed to the sum function
3242
// Then it should ignore the non-numerical values and return the sum of the numerical elements
33-
43+
test("given an array containing non-number values, returns sum of only values", () => {
44+
expect(sum([5, "banana", 2])).toBe(7);
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 with only non-number values, returns 0", () => {
50+
expect(sum(["hi", "here"])).toBe(0);
51+
});

0 commit comments

Comments
 (0)