Skip to content

Commit 39c0872

Browse files
committed
Updated Sprint-1
1 parent 86b8965 commit 39c0872

6 files changed

Lines changed: 149 additions & 81 deletions

File tree

Sprint-1/implement/dedupe.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
function dedupe(arr) {
2-
// The spread operator [...] converts back to an array.
3-
return [...new Set(arr)]; //New Set(arr) removes duplicates automatically
2+
// (Though requirements suggest it will be an array)
3+
if (!Array.isArray(arr)) return [];
4+
// The spread operator [...] preserves the first-occurrence order
5+
return [...new Set(arr)];
46
}
7+
8+
module.exports = dedupe;

Sprint-1/implement/dedupe.test.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,41 @@
1+
2+
13
const dedupe = require("./dedupe.js");
4+
5+
describe("dedupe()", () => {
6+
// Acceptance Criteria 1
7+
test("given an empty array, it returns an empty array", () => {
8+
expect(dedupe([])).toEqual([]);
9+
});
10+
11+
// Acceptance Criteria 2
12+
test("given an array with no duplicates, it returns a copy of the original array", () => {
13+
const input = [1, 2, 3, "a", "b"];
14+
const result = dedupe(input);
15+
expect(result).toEqual(input);
16+
// Ensure it's a new reference (a copy)
17+
expect(result).not.toBe(input);
18+
});
19+
20+
// Acceptance Criteria 3 & 4: Numbers
21+
test("it should remove duplicate numbers and preserve first occurrence order", () => {
22+
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
23+
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
24+
});
25+
26+
// Acceptance Criteria 3 & 4: Strings
27+
test("it should remove duplicate strings and preserve first occurrence order", () => {
28+
expect(dedupe(['a', 'a', 'a', 'b', 'b', 'c'])).toEqual(['a', 'b', 'c']);
29+
});
30+
31+
// Mixed types edge case
32+
test("it should handle mixed strings and numbers correctly", () => {
33+
expect(dedupe([1, "1", 1, "a", "a"])).toEqual([1, "1", "a"]);
34+
});
35+
});
36+
37+
38+
239
/*
340
Dedupe Array
441
@@ -16,7 +53,7 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2]
1653
// Given an empty array
1754
// When passed to the dedupe function
1855
// Then it should return an empty array
19-
test.todo("given an empty array, it returns an empty array");
56+
//test.todo("given an empty array, it returns an empty array");
2057

2158
// Given an array with no duplicates
2259
// When passed to the dedupe function

Sprint-1/implement/max.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11

22
function findMax(arr) {
3-
// filter out non-numbers, non-numeric strings, and NaN. ALso convert string numbers to number
3+
// Requirement 7: Filter out non-numbers and non-numeric strings
4+
// Requirement 8: If only non-numbers exist, numericValues will be empty
45
const numericValues = arr
5-
.filter((item) => typeof item === "number" || (typeof item === "string" && item.trim() !== "" && !isNaN(item)))
6+
.filter((item) => {
7+
// Check if it's a number and not NaN
8+
if (typeof item === "number" && !isNaN(item)) return true;
9+
// Check if it's a numeric string that isn't just whitespace
10+
if (typeof item === "string" && item.trim() !== "" && !isNaN(item)) return true;
11+
return false;
12+
})
613
.map((item) => Number(item));
714

8-
// returns -Infinity i no numerical elements are found
15+
// Requirement 2 & 8: Returns -Infinity if no numerical elements are found
916
if (numericValues.length === 0) return -Infinity;
1017

18+
// Requirement 1, 3, 4, 5, 6: Use Math.max with the spread operator
1119
return Math.max(...numericValues);
1220
}
1321

1422
module.exports = findMax;
23+
24+

Sprint-1/implement/max.test.js

Lines changed: 45 additions & 38 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-
*/
121

132
const findMax = require("./max.js");
143

15-
// Given an empty array
16-
// When passed to the max function
17-
// 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");
20-
21-
// Given an array with one number
22-
// When passed to the max function
23-
// Then it should return that number
24-
25-
// Given an array with both positive and negative numbers
26-
// When passed to the max function
27-
// Then it should return the largest number overall
28-
29-
// Given an array with just negative numbers
30-
// When passed to the max function
31-
// Then it should return the closest one to zero
32-
33-
// Given an array with decimal numbers
34-
// When passed to the max function
35-
// Then it should return the largest decimal number
4+
describe("findMax()", () => {
5+
// Requirement 1
6+
test("finds the largest number in an array", () => {
7+
expect(findMax([30, 50, 10, 40])).toBe(50);
8+
});
9+
10+
// Requirement 2
11+
test("given an empty array, returns -Infinity", () => {
12+
expect(findMax([])).toBe(-Infinity);
13+
});
14+
15+
// Requirement 3
16+
test("given an array with one number, returns that number", () => {
17+
expect(findMax([42])).toBe(42);
18+
});
19+
20+
// Requirement 4
21+
test("finds the largest number in a mix of positive and negative numbers", () => {
22+
expect(findMax([-10, 5, 0, -2, 20])).toBe(20);
23+
});
24+
25+
// Requirement 5
26+
test("given only negative numbers, returns the one closest to zero", () => {
27+
expect(findMax([-50, -10, -40])).toBe(-10);
28+
});
29+
30+
// Requirement 6
31+
test("finds the largest among decimal numbers", () => {
32+
expect(findMax([1.5, 1.55, 1.05])).toBe(1.55);
33+
});
34+
35+
// Requirement 7
36+
test("ignores non-numeric values and finds the max", () => {
37+
expect(findMax(['hey', 10, 'hi', 60, '10'])).toBe(60);
38+
});
39+
40+
// Requirement 8
41+
test("given only non-number values, returns -Infinity", () => {
42+
expect(findMax(['apple', 'orange', ' '])).toBe(-Infinity);
43+
});
44+
45+
test("treats numeric strings as numbers", () => {
46+
expect(findMax(["10", "20", "5"])).toBe(20);
47+
});
48+
});
3649

37-
// Given an array with non-number values
38-
// When passed to the max function
39-
// Then it should return the max and ignore non-numeric values
4050

41-
// Given an array with only non-number values
42-
// When passed to the max function
43-
// Then it should return the least surprising value given how it behaves for all other inputs

Sprint-1/implement/sum.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
21
function sum(arr) {
32
if (!Array.isArray(arr)) return 0;
43

54
return arr.reduce((accumulator, currentValue) => {
6-
// 1. Convert the value to a Number (handles strings like "10")
5+
// We want to skip booleans, null, and empty/whitespace strings
6+
// as Number() would coerce them into 0 or 1.
7+
if (typeof currentValue === 'boolean' || currentValue === null ||
8+
(typeof currentValue === 'string' && currentValue.trim() === "")) {
9+
return accumulator;
10+
}
11+
712
const numericValue = Number(currentValue);
813

9-
// 2. Check if the result is a valid number and not NaN
10-
// Note: Number.isNaN is stricter/safer than the global isNaN
11-
if (!Number.isNaN(numericValue) && typeof currentValue !== 'boolean' && currentValue !== null) {
14+
// Check if the result is a valid number and not NaN
15+
if (!Number.isNaN(numericValue)) {
1216
return accumulator + numericValue;
1317
}
1418

Sprint-1/implement/sum.test.js

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,42 @@
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-
*/
81

92
const sum = require("./sum.js");
103

11-
// Acceptance Criteria:
12-
13-
// Given an empty array
14-
// When passed to the sum function
15-
// Then it should return 0
16-
test.todo("given an empty array, returns 0")
17-
18-
// Given an array with just one number
19-
// When passed to the sum function
20-
// Then it should return that number
21-
22-
// Given an array containing negative numbers
23-
// When passed to the sum function
24-
// Then it should still return the correct total sum
25-
26-
// Given an array with decimal/float numbers
27-
// When passed to the sum function
28-
// Then it should return the correct total sum
29-
30-
// Given an array containing non-number values
31-
// When passed to the sum function
32-
// Then it should ignore the non-numerical values and return the sum of the numerical elements
4+
describe("sum()", () => {
5+
// Requirement 1 & 6
6+
test("should sum numerical elements and ignore non-numerical strings", () => {
7+
expect(sum([10, 20, 30])).toBe(60);
8+
expect(sum(['hey', 10, 'hi', 60, 10])).toBe(80);
9+
});
10+
11+
// Requirement 2
12+
test("given an empty array, it should return 0", () => {
13+
expect(sum([])).toBe(0);
14+
});
15+
16+
// Requirement 3
17+
test("given an array with one number, it should return that number", () => {
18+
expect(sum([42])).toBe(42);
19+
});
20+
21+
// Requirement 4
22+
test("given an array with negative numbers, it should return the correct total", () => {
23+
expect(sum([10, -5, 20])).toBe(25);
24+
expect(sum([-10, -20, 5])).toBe(-25);
25+
});
26+
27+
// Requirement 5
28+
test("given an array with decimals, it should return the correct total", () => {
29+
// Using toBeCloseTo for float precision safety
30+
expect(sum([1.5, 2.5, 1.1])).toBeCloseTo(5.1);
31+
});
32+
33+
// Requirement 7
34+
test("given an array with only non-number values, it should return 0", () => {
35+
expect(sum(['apple', 'banana', null, undefined])).toBe(0);
36+
});
37+
38+
test("should handle numeric strings correctly", () => {
39+
expect(sum(["10", "20", 5])).toBe(35);
40+
});
41+
});
3342

34-
// Given an array with only non-number values
35-
// When passed to the sum function
36-
// Then it should return the least surprising value given how it behaves for all other inputs

0 commit comments

Comments
 (0)