Skip to content

Commit 65bf6f1

Browse files
committed
Complete Sprint 1 exercises (median fix, max, sum, dedupe)
1 parent 96d077b commit 65bf6f1

8 files changed

Lines changed: 128 additions & 55 deletions

File tree

Sprint-1/fix/median.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
1-
// Fix this implementation
2-
// Start by running the tests for this function
3-
// If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory
1+
function calculateMedian(list) {
42

5-
// Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null)
6-
// or 'list' has mixed values (the function is expected to sort only numbers).
3+
// must be an array
4+
if (!Array.isArray(list)) {
5+
return null;
6+
}
77

8-
function calculateMedian(list) {
9-
const middleIndex = Math.floor(list.length / 2);
10-
const median = list.splice(middleIndex, 1)[0];
11-
return median;
8+
// keep only numeric values
9+
const numbers = list.filter(value => typeof value === "number");
10+
11+
// if no numbers exist return null
12+
if (numbers.length === 0) {
13+
return null;
14+
}
15+
16+
// sort numbers without modifying original list
17+
const sorted = [...numbers].sort((a, b) => a - b);
18+
19+
const middle = Math.floor(sorted.length / 2);
20+
21+
// even length
22+
if (sorted.length % 2 === 0) {
23+
return (sorted[middle - 1] + sorted[middle]) / 2;
24+
}
25+
26+
// odd length
27+
return sorted[middle];
1228
}
1329

14-
module.exports = calculateMedian;
30+
module.exports = calculateMedian;

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+
function dedupe(arr) {
2+
if (!Array.isArray(arr)) {
3+
return [];
4+
}
5+
6+
const result = [];
7+
8+
for (let i = 0; i < arr.length; i++) {
9+
if (!result.includes(arr[i])) {
10+
result.push(arr[i]);
11+
}
12+
}
13+
14+
return result;
15+
}
16+
17+
module.exports = dedupe;

Sprint-1/implement/dedupe.test.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
const dedupe = require("./dedupe.js");
2+
23
/*
34
Dedupe Array
4-
5-
📖 Dedupe means **deduplicate**
6-
7-
In this kata, you will need to deduplicate the elements of an array
8-
9-
E.g. dedupe(['a','a','a','b','b','c']) target output: ['a','b','c']
10-
E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) target output: [5, 1, 2, 3, 8]
11-
E.g. dedupe([1, 2, 1]) target output: [1, 2]
125
*/
136

14-
// Acceptance Criteria:
15-
167
// Given an empty array
178
// When passed to the dedupe function
189
// Then it should return an empty array
19-
test.todo("given an empty array, it returns an empty array");
10+
test("given an empty array, it returns an empty array", () => {
11+
expect(dedupe([])).toEqual([]);
12+
});
2013

2114
// Given an array with no duplicates
2215
// When passed to the dedupe function
2316
// Then it should return a copy of the original array
17+
test("given an array with no duplicates, it returns the same array", () => {
18+
expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]);
19+
});
2420

2521
// Given an array with strings or numbers
2622
// When passed to the dedupe function
27-
// Then it should remove the duplicate values, preserving the first occurence of each element
23+
// Then it should remove duplicate values
24+
test("removes duplicate numbers", () => {
25+
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
26+
});
27+
28+
test("removes duplicate strings", () => {
29+
expect(dedupe(['a','a','a','b','b','c'])).toEqual(['a','b','c']);
30+
});
31+
32+
test("preserves first occurrence order", () => {
33+
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
34+
});

Sprint-1/implement/max.js

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

4-
module.exports = findMax;
13+
module.exports = findMax;

Sprint-1/implement/max.test.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,50 @@
1-
/* Find the maximum element of an array of numbers
2-
3-
In this kata, you will need to implement a function that find the largest numerical element of an array.
4-
5-
E.g. max([30, 50, 10, 40]), target output: 50
6-
E.g. max(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-numerical elements)
7-
8-
You should implement this function in max.js, and add tests for it in this file.
9-
10-
We have set things up already so that this file can see your function from the other file.
11-
*/
12-
131
const findMax = require("./max.js");
142

153
// Given an empty array
164
// When passed to the max function
175
// 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");
6+
test("given an empty array, returns -Infinity", () => {
7+
expect(findMax([])).toBe(-Infinity);
8+
});
209

2110
// Given an array with one number
2211
// When passed to the max function
2312
// Then it should return that number
13+
test("given an array with one number", () => {
14+
expect(findMax([5])).toBe(5);
15+
});
2416

2517
// Given an array with both positive and negative numbers
2618
// When passed to the max function
2719
// Then it should return the largest number overall
20+
test("given positive and negative numbers", () => {
21+
expect(findMax([-10, 20, 5, -2])).toBe(20);
22+
});
2823

2924
// Given an array with just negative numbers
3025
// When passed to the max function
3126
// Then it should return the closest one to zero
27+
test("given only negative numbers", () => {
28+
expect(findMax([-10, -3, -50, -1])).toBe(-1);
29+
});
3230

3331
// Given an array with decimal numbers
3432
// When passed to the max function
3533
// Then it should return the largest decimal number
34+
test("given decimal numbers", () => {
35+
expect(findMax([1.2, 5.7, 3.3])).toBe(5.7);
36+
});
3637

3738
// Given an array with non-number values
3839
// When passed to the max function
3940
// Then it should return the max and ignore non-numeric values
41+
test("ignores non-number values", () => {
42+
expect(findMax(["hey", 10, "hi", 60, 10])).toBe(60);
43+
});
4044

4145
// Given an array with only non-number values
4246
// When passed to the max function
43-
// Then it should return the least surprising value given how it behaves for all other inputs
47+
// Then it should return the least surprising value
48+
test("only non-number values returns -Infinity", () => {
49+
expect(findMax(["a", "b", "c"])).toBe(-Infinity);
50+
});

Sprint-1/implement/sum.js

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

4-
module.exports = sum;
13+
module.exports = sum;

Sprint-1/implement/sum.test.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
1-
/* Sum the numbers in an array
2-
3-
In this kata, you will need to implement a function that sums the numerical elements of an array
4-
5-
E.g. sum([10, 20, 30]), target output: 60
6-
E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical elements)
7-
*/
8-
91
const sum = require("./sum.js");
102

11-
// Acceptance Criteria:
12-
133
// Given an empty array
144
// When passed to the sum function
155
// Then it should return 0
16-
test.todo("given an empty array, returns 0")
6+
test("given an empty array, returns 0", () => {
7+
expect(sum([])).toBe(0);
8+
});
179

1810
// Given an array with just one number
1911
// When passed to the sum function
2012
// Then it should return that number
13+
test("given an array with one number", () => {
14+
expect(sum([5])).toBe(5);
15+
});
2116

2217
// Given an array containing negative numbers
2318
// When passed to the sum function
2419
// Then it should still return the correct total sum
20+
test("given negative numbers", () => {
21+
expect(sum([10, -5, -5])).toBe(0);
22+
});
2523

2624
// Given an array with decimal/float numbers
2725
// When passed to the sum function
2826
// Then it should return the correct total sum
27+
test("given decimal numbers", () => {
28+
expect(sum([1.5, 2.5, 3])).toBe(7);
29+
});
2930

3031
// Given an array containing non-number values
3132
// When passed to the sum function
32-
// Then it should ignore the non-numerical values and return the sum of the numerical elements
33+
// Then it should ignore the non-numerical values
34+
test("ignores non-number values", () => {
35+
expect(sum(["hey", 10, "hi", 60, 10])).toBe(80);
36+
});
3337

3438
// Given an array with only non-number values
3539
// When passed to the sum function
36-
// Then it should return the least surprising value given how it behaves for all other inputs
40+
// Then it should return the least surprising value
41+
test("only non-number values returns 0", () => {
42+
expect(sum(["a", "b", "c"])).toBe(0);
43+
});

Sprint-1/package-lock.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)