Skip to content

Commit 074cc7c

Browse files
committed
Merge branch 'coursework/sprint-1-implement' into coursework/sprint-1-data-groups
2 parents 4d87c5a + 4ee9bf8 commit 074cc7c

6 files changed

Lines changed: 102 additions & 6 deletions

File tree

Sprint-1/implement/dedupe.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1-
function dedupe() {}
1+
// Return a new array with duplicate values removed.
2+
// Keep the first occurrence of each value.
3+
4+
function dedupe(elements) {
5+
const uniqueElements = [];
6+
7+
for (const element of elements) {
8+
// Add the element only if it is not already in the result array
9+
if (!uniqueElements.includes(element)) {
10+
uniqueElements.push(element);
11+
}
12+
}
13+
14+
return uniqueElements;
15+
}
16+
17+
module.exports = dedupe;

Sprint-1/implement/dedupe.test.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const dedupe = require("./dedupe.js");
22
/*
33
Dedupe Array
44
5-
📖 Dedupe means **deduplicate**
5+
📖 Dedupe means deduplicate
66
77
In this kata, you will need to deduplicate the elements of an array
88
@@ -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+
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 returns the same values", () => {
27+
expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]);
28+
});
2429

2530
// Given an array with strings or numbers
2631
// When passed to the dedupe function
2732
// Then it should remove the duplicate values, preserving the first occurence of each element
33+
test("given an array with duplicate strings, removes duplicates and preserves first occurrence", () => {
34+
expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]);
35+
});
36+
37+
test("given an array with duplicate numbers, removes duplicates and preserves first occurrence", () => {
38+
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
39+
});
40+
41+
test("given a mixed duplicate order, removes duplicates and preserves first occurrence", () => {
42+
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
43+
});

Sprint-1/implement/max.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1+
// Find the largest numerical value in an array.
2+
// Non-number values should be ignored.
3+
14
function findMax(elements) {
5+
let maxValue = -Infinity;
6+
7+
for (const element of elements) {
8+
// Only compare values that are real numbers
9+
if (typeof element === "number" && !Number.isNaN(element)) {
10+
if (element > maxValue) {
11+
maxValue = element;
12+
}
13+
}
14+
}
15+
16+
return maxValue;
217
}
318

419
module.exports = findMax;

Sprint-1/implement/max.test.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,48 @@ const findMax = require("./max.js");
1515
// Given an empty array
1616
// When passed to the max function
1717
// 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");
18+
test("given an empty array, returns -Infinity", () => {
19+
expect(findMax([])).toBe(-Infinity);
20+
});
2021

2122
// Given an array with one number
2223
// When passed to the max function
2324
// Then it should return that number
25+
test("given an array with one number, returns that number", () => {
26+
expect(findMax([42])).toBe(42);
27+
});
2428

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
32+
test("given an array with positive and negative numbers, returns the largest number", () => {
33+
expect(findMax([-10, 3, 25, -1])).toBe(25);
34+
});
2835

2936
// Given an array with just negative numbers
3037
// When passed to the max function
3138
// Then it should return the closest one to zero
39+
test("given an array with only negative numbers, returns the largest one", () => {
40+
expect(findMax([-9, -2, -15, -4])).toBe(-2);
41+
});
3242

3343
// Given an array with decimal numbers
3444
// When passed to the max function
3545
// Then it should return the largest decimal number
46+
test("given an array with decimal numbers, returns the largest decimal", () => {
47+
expect(findMax([1.2, 3.8, 2.4])).toBe(3.8);
48+
});
3649

3750
// Given an array with non-number values
3851
// When passed to the max function
3952
// Then it should return the max and ignore non-numeric values
53+
test("given an array with non-number values, ignores them and returns the max", () => {
54+
expect(findMax(["hey", 10, "hi", 60, 10])).toBe(60);
55+
});
4056

4157
// Given an array with only non-number values
4258
// When passed to the max function
4359
// Then it should return the least surprising value given how it behaves for all other inputs
60+
test("given an array with only non-number values, returns -Infinity", () => {
61+
expect(findMax(["apple", null, undefined, "banana"])).toBe(-Infinity);
62+
});

Sprint-1/implement/sum.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1+
// Sum the numerical values in an array.
2+
// Non-number values should be ignored.
3+
14
function sum(elements) {
5+
let total = 0;
6+
7+
for (const element of elements) {
8+
// Only add values that are real numbers
9+
if (typeof element === "number" && !Number.isNaN(element)) {
10+
total += element;
11+
}
12+
}
13+
14+
return total;
215
}
316

417
module.exports = sum;

Sprint-1/implement/sum.test.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,41 @@ 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+
expect(sum([])).toBe(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
23+
test("given an array with one number, returns that number", () => {
24+
expect(sum([7])).toBe(7);
25+
});
2126

2227
// Given an array containing negative numbers
2328
// When passed to the sum function
2429
// Then it should still return the correct total sum
30+
test("given an array with negative numbers, returns the correct sum", () => {
31+
expect(sum([-5, 10, -2])).toBe(3);
32+
});
2533

2634
// Given an array with decimal/float numbers
2735
// When passed to the sum function
2836
// Then it should return the correct total sum
37+
test("given an array with decimal numbers, returns the correct sum", () => {
38+
expect(sum([1.5, 2.5, 3])).toBe(7);
39+
});
2940

3041
// Given an array containing non-number values
3142
// When passed to the sum function
3243
// Then it should ignore the non-numerical values and return the sum of the numerical elements
44+
test("given an array with non-number values, ignores them and sums only numbers", () => {
45+
expect(sum(["hey", 10, "hi", 60, 10])).toBe(80);
46+
});
3347

3448
// Given an array with only non-number values
3549
// When passed to the sum function
3650
// Then it should return the least surprising value given how it behaves for all other inputs
51+
test("given an array with only non-number values, returns 0", () => {
52+
expect(sum(["apple", null, undefined, "banana"])).toBe(0);
53+
});

0 commit comments

Comments
 (0)