Skip to content

Commit 2b7ed5b

Browse files
committed
Fix Sprint-2 implementations and revert Sprint-1 files
1 parent 08af2c8 commit 2b7ed5b

16 files changed

Lines changed: 49 additions & 230 deletions

File tree

Sprint-1/fix/median.js

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,9 @@
66
// or 'list' has mixed values (the function is expected to sort only numbers).
77

88
function calculateMedian(list) {
9-
if (!Array.isArray(list)) {
10-
return null;
11-
}
12-
13-
const numbers = [];
14-
for (let i = 0; i < list.length; i++) {
15-
if (typeof list[i] === 'number') {
16-
numbers.push(list[i]);
17-
}
18-
}
19-
20-
if (numbers.length === 0) {
21-
return null;
22-
}
23-
24-
for (let i = 0; i < numbers.length; i++) {
25-
for (let j = i + 1; j < numbers.length; j++) {
26-
if (numbers[i] > numbers[j]) {
27-
const temp = numbers[i];
28-
numbers[i] = numbers[j];
29-
numbers[j] = temp;
30-
}
31-
}
32-
}
33-
34-
const middleIndex = Math.floor(numbers.length / 2);
35-
36-
37-
if (numbers.length % 2 === 0) {
38-
39-
return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2;
40-
} else {
41-
42-
return numbers[middleIndex];
43-
}
9+
const middleIndex = Math.floor(list.length / 2);
10+
const median = list.splice(middleIndex, 1)[0];
11+
return median;
4412
}
4513

46-
4714
module.exports = calculateMedian;

Sprint-1/implement/dedupe.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1 @@
1-
function dedupe(elements) {
2-
const unique = [];
3-
4-
for (let i = 0; i < elements.length; i++) {
5-
const current = elements[i];
6-
7-
if (!unique.includes(current)) {
8-
unique.push(current);
9-
}
10-
}
11-
12-
return unique;
13-
}
14-
15-
module.exports = dedupe;
1+
function dedupe() {}

Sprint-1/implement/dedupe.test.js

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,12 @@ 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");
1920

20-
test("given an empty array, it returns an empty array", () => {
21-
const currentOutput = dedupe([]);
22-
const targetOutput = [];
23-
24-
expect(currentOutput).toEqual(targetOutput);
25-
});
2621
// Given an array with no duplicates
2722
// When passed to the dedupe function
2823
// Then it should return a copy of the original array
2924

30-
test("Given an array with no duplicates, it returns an the same array", () => {
31-
const currentOutput = dedupe([1,4,6,"d","a","x","e",0,7,8]);
32-
const targetOutput = [1,4,6,"d","a","x","e",0,7,8];
33-
34-
expect(currentOutput).toEqual(targetOutput);
35-
});
3625
// Given an array with strings or numbers
3726
// When passed to the dedupe function
38-
// Then it should remove the duplicate values, preserving the first occurrence of each element
39-
40-
test("Given an array with strings or numbers, it returns an the array whit no duplicated elements", () => {
41-
const currentOutput = dedupe(['a','a','a','b','b','c']);
42-
const targetOutput = ['a','b','c'];
43-
44-
expect(currentOutput).toEqual(targetOutput);
45-
});
27+
// Then it should remove the duplicate values, preserving the first occurence of each element

Sprint-1/implement/max.js

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

214
module.exports = findMax;

Sprint-1/implement/max.test.js

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,75 +16,28 @@ 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-
20-
test("given an empty array, returns -Infinity", () => {
21-
const currentOutput = findMax([]);
22-
const targetOutput = -Infinity;
23-
24-
expect(currentOutput).toEqual(targetOutput);
25-
});
19+
test.todo("given an empty array, returns -Infinity");
2620

2721
// Given an array with one number
2822
// When passed to the max function
2923
// Then it should return that number
3024

31-
test("given an array with one number, returns that number", () => {
32-
const currentOutput = findMax([42]);
33-
const targetOutput = 42;
34-
35-
expect(currentOutput).toEqual(targetOutput);
36-
});
37-
38-
3925
// Given an array with both positive and negative numbers
4026
// When passed to the max function
4127
// Then it should return the largest number overall
4228

43-
test("given an array with positive and negative numbers, returns the largest", () => {
44-
const currentOutput = findMax([-25, 5, 20, -3, 15]);
45-
const targetOutput = 20;
46-
47-
expect(currentOutput).toEqual(targetOutput);
48-
});
4929
// Given an array with just negative numbers
5030
// When passed to the max function
5131
// Then it should return the closest one to zero
5232

53-
test("given an array of only negative numbers, returns the closest to zero", () => {
54-
const currentOutput = findMax([-50, -3, -20, -10]);
55-
const targetOutput = -3;
56-
57-
expect(currentOutput).toEqual(targetOutput);
58-
});
59-
6033
// Given an array with decimal numbers
6134
// When passed to the max function
6235
// Then it should return the largest decimal number
6336

64-
test("given an array of decimal numbers, returns the largest decimal", () => {
65-
const currentOutput = findMax([1.1, 3.5, 2.9, 3.4]);
66-
const targetOutput = 3.5;
67-
68-
expect(currentOutput).toEqual(targetOutput);
69-
});
70-
7137
// Given an array with non-number values
7238
// When passed to the max function
7339
// Then it should return the max and ignore non-numeric values
7440

75-
test("given an array with non-number values, returns correct max (ignoring non-numbers)", () => {
76-
const currentOutput = findMax([10, "hi", 50, true, 3]);
77-
const targetOutput = 50;
78-
79-
expect(currentOutput).toEqual(targetOutput);
80-
});
81-
8241
// Given an array with only non-number values
8342
// When passed to the max function
8443
// Then it should return the least surprising value given how it behaves for all other inputs
85-
test("given an array with only non-number values, returns -Infinity", () => {
86-
const currentOutput = findMax(["a", null, undefined, {}, [], true]);
87-
const targetOutput = -Infinity;
88-
89-
expect(currentOutput).toEqual(targetOutput);
90-
});

Sprint-1/implement/sum.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
11
function sum(elements) {
2-
// This variable will store the total sum
3-
let total = 0;
4-
5-
// Go through each element in the array
6-
for (let i = 0; i < elements.length; i++) {
7-
let current = elements[i]; // get the current element
8-
9-
// Only add it if it's a number
10-
if (typeof current === "number" && !Number.isNaN(current)) {
11-
total = total + current; // add to total
12-
}
13-
}
14-
15-
// Return the sum of all numbers
16-
return total;
172
}
183

194
module.exports = sum;

Sprint-1/implement/sum.test.js

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,64 +13,24 @@ 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("given an empty array, returns 0",()=>{
17-
const currentOutput = sum([]);
18-
const targetPitPut= 0;
19-
20-
expect(currentOutput).toEqual(targetPitPut);
21-
})
22-
16+
test.todo("given an empty array, returns 0")
2317

2418
// Given an array with just one number
2519
// When passed to the sum function
2620
// Then it should return that number
27-
test("given an array whit only 1 number, returns same input",()=>{
28-
const currentOutput = sum([4]);
29-
const targetPitPut= 4;
30-
31-
expect(currentOutput).toEqual(targetPitPut);
32-
})
33-
3421

3522
// Given an array containing negative numbers
3623
// When passed to the sum function
3724
// Then it should still return the correct total sum
38-
test("given an array whit a negative number, returns correct total (subtract the negative number)",()=>{
39-
const currentOutput = sum([1,2,3,4,-5,]);
40-
const targetPitPut= 5;
41-
42-
expect(currentOutput).toEqual(targetPitPut);
43-
})
44-
4525

4626
// Given an array with decimal/float numbers
4727
// When passed to the sum function
4828
// Then it should return the correct total sum
4929

50-
test("given an array whit a decimal/float number, returns correct total (decimal/float number)",()=>{
51-
const currentOutput = sum([1,2,3,4,3.5,]);
52-
const targetPitPut= 13.5;
53-
54-
expect(currentOutput).toEqual(targetPitPut);
55-
})
56-
57-
5830
// Given an array containing non-number values
5931
// When passed to the sum function
6032
// Then it should ignore the non-numerical values and return the sum of the numerical elements
61-
test("given an array whit a non-number values, returns correct total (ignore the NaN and sum the others)",()=>{
62-
const currentOutput = sum([1,2,3,4,"hi",5]);
63-
const targetPitPut= 15;
64-
65-
expect(currentOutput).toEqual(targetPitPut);
66-
})
6733

6834
// Given an array with only non-number values
6935
// When passed to the sum function
7036
// Then it should return the least surprising value given how it behaves for all other inputs
71-
test("given an array with only non-number values, returns the least surprising value (0)", () => {
72-
const currentOutput = sum(["hello", null, undefined, {}, [], true, NaN]);
73-
const targetOutput = 0;
74-
75-
expect(currentOutput).toEqual(targetOutput);
76-
});

Sprint-1/refactor/includes.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
// Refactor the implementation of includes to use a for...of loop
22

33
function includes(list, target) {
4-
for (const element of list) {
4+
for (let index = 0; index < list.length; index++) {
5+
const element = list[index];
56
if (element === target) {
6-
return true;
7+
return true;
78
}
89
}
9-
return false;
10+
return false;
1011
}
1112

1213
module.exports = includes;

Sprint-2/implement/contains.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
function contains() {function contains(obj, key) {
2-
// First, check if obj is a real object
1+
function contains(obj, key) {
32
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
43
return false;
54
}
65

7-
// Use hasOwnProperty to check if the key exists
8-
return obj.hasOwnProperty(key);
9-
}
10-
11-
module.exports = contains;
6+
return Object.prototype.hasOwnProperty.call(obj, key);
127
}
138

149
module.exports = contains;

Sprint-2/implement/contains.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ test("contains returns false for non-existent property", () => {
4646
// When passed to contains
4747
// Then it should return false or throw an error
4848
test("contains returns false for invalid input", () => {
49-
expect(contains([], "a")).toBe(false);
49+
expect(contains([], "length")).toBe(false);
5050
expect(contains(null, "a")).toBe(false);
5151
expect(contains("string", "a")).toBe(false);
5252
});

0 commit comments

Comments
 (0)