Skip to content

Commit 466c1c5

Browse files
committed
GLASGOW | Jan-26-ITP | Prakash Dcosta | Sprint 1 | Data Groups
Self checklist - [X] I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title - [X] My changes meet the requirements of the task - [X] I have tested my changes - [X] My changes follow the [style guide] ## Changelist In this PR, i have implemented functions according to requrements and built test cases
1 parent 96d077b commit 466c1c5

9 files changed

Lines changed: 127 additions & 18 deletions

File tree

Sprint-1/fix/median.js

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

88
function calculateMedian(list) {
9-
const middleIndex = Math.floor(list.length / 2);
10-
const median = list.splice(middleIndex, 1)[0];
11-
return median;
9+
// It must be an array
10+
if (!Array.isArray(list)) {
11+
return null;
12+
}
13+
// Keep only numeric values
14+
const numbers = list.filter(
15+
(value) => typeof value === "number" && !isNaN(value)
16+
);
17+
18+
// If there are no numbers existing then return null
19+
if (numbers.length === 0) return null;
20+
21+
// Sort numbers without modifying original list
22+
const sorted = [...numbers].sort((a, b) => a - b);
23+
const middle = Math.floor(sorted.length / 2);
24+
25+
// For even length
26+
if (sorted.length % 2 === 0) {
27+
return (sorted[middle - 1] + sorted[middle]) / 2;
28+
}
29+
30+
// For odd length
31+
return sorted[middle];
1232
}
1333

1434
module.exports = calculateMedian;

Sprint-1/fix/median.test.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ describe("calculateMedian", () => {
1313
{ input: [1, 2, 3, 4], expected: 2.5 },
1414
{ input: [1, 2, 3, 4, 5, 6], expected: 3.5 },
1515
].forEach(({ input, expected }) =>
16-
it(`returns the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
16+
it(`returns the median for [${input}]`, () =>
17+
expect(calculateMedian(input)).toEqual(expected))
1718
);
1819

1920
[
@@ -24,17 +25,39 @@ describe("calculateMedian", () => {
2425
{ input: [110, 20, 0], expected: 20 },
2526
{ input: [6, -2, 2, 12, 14], expected: 6 },
2627
].forEach(({ input, expected }) =>
27-
it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
28+
it(`returns the correct median for unsorted array [${input}]`, () =>
29+
expect(calculateMedian(input)).toEqual(expected))
2830
);
2931

3032
it("doesn't modify the input array [3, 1, 2]", () => {
3133
const list = [3, 1, 2];
3234
calculateMedian(list);
3335
expect(list).toEqual([3, 1, 2]);
3436
});
37+
// Test for single number array
38+
it("returns the value for a single-element array", () => {
39+
expect(calculateMedian([5])).toBe(5);
40+
});
41+
// Test for only 1 numeric value in an array
42+
it("returns the only numeric value in mixed array", () => {
43+
expect(calculateMedian(["apple", 10, "banana"])).toBe(10);
44+
});
45+
// Test for decimal numbers only
46+
it("works with decimal numbers", () => {
47+
expect(calculateMedian([2.5, 3.5, 4.5])).toBe(3.5);
48+
});
3549

36-
[ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val =>
37-
it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null))
50+
[
51+
"not an array",
52+
123,
53+
null,
54+
undefined,
55+
{},
56+
[],
57+
["apple", null, undefined],
58+
].forEach((val) =>
59+
it(`returns null for invalid input (${val})`, () =>
60+
expect(calculateMedian(val)).toBe(null))
3861
);
3962

4063
[
@@ -45,6 +68,7 @@ describe("calculateMedian", () => {
4568
{ input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 },
4669
{ input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 },
4770
].forEach(({ input, expected }) =>
48-
it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
71+
it(`filters out non-numeric values and calculates the median for [${input}]`, () =>
72+
expect(calculateMedian(input)).toEqual(expected))
4973
);
5074
});

Sprint-1/implement/dedupe.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
function dedupe() {}
1+
function dedupe(arr) {
2+
return [...new Set(arr)];
3+
}
4+
module.exports = dedupe;

Sprint-1/implement/dedupe.test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,26 @@ 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("returns original array if there are no duplicates", () => {
27+
expect(dedupe([10, 45, 85, 20])).toEqual([10, 45, 85, 20]);
28+
expect(dedupe(["hello", "a", "hi", "b"])).toEqual(["hello", "a", "hi", "b"]);
29+
});
2430

2531
// Given an array with strings or numbers
2632
// When passed to the dedupe function
2733
// Then it should remove the duplicate values, preserving the first occurence of each element
34+
test("returns first occurance for array of strings or numbers", () => {
35+
expect(dedupe([10, 10, 20, 20, 30, 40])).toEqual([10, 20, 30, 40]);
36+
expect(dedupe(["hello", "hello", "hi", "a", "hi"])).toEqual([
37+
"hello",
38+
"hi",
39+
"a",
40+
]);
41+
});

Sprint-1/implement/max.js

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

411
module.exports = findMax;

Sprint-1/implement/max.test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,48 @@ 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
26+
test("given an array with one number, returns that number", () => {
27+
expect(findMax([5])).toBe(5);
28+
});
2429

2530
// Given an array with both positive and negative numbers
2631
// When passed to the max function
2732
// Then it should return the largest number overall
33+
test("returns the largest number overall", () => {
34+
expect(findMax([-25, 3, 12, -32])).toBe(12);
35+
});
2836

2937
// Given an array with just negative numbers
3038
// When passed to the max function
3139
// Then it should return the closest one to zero
40+
test("returns the closest number to zero", () => {
41+
expect(findMax([-25, -345, -1, -72])).toBe(-1);
42+
});
3243

3344
// Given an array with decimal numbers
3445
// When passed to the max function
3546
// Then it should return the largest decimal number
47+
test("returns the largest decimal number", () => {
48+
expect(findMax([1.85, 2.563, 12.23, 0.24])).toBe(12.23);
49+
});
3650

3751
// Given an array with non-number values
3852
// When passed to the max function
3953
// Then it should return the max and ignore non-numeric values
54+
test("returnes max and ignores non numeric values", () => {
55+
expect(findMax(["stay", 3, "quiet", -32, 5])).toBe(5);
56+
});
4057

4158
// Given an array with only non-number values
4259
// When passed to the max function
4360
// Then it should return the least surprising value given how it behaves for all other inputs
61+
test("returns infinity in non number values", () => {
62+
expect(findMax(["hi", "sally", "a", "alpha"])).toBe(-Infinity);
63+
});

Sprint-1/implement/sum.js

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

411
module.exports = sum;

Sprint-1/implement/sum.test.js

Lines changed: 18 additions & 3 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("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("returns the same number when an array has just 1 number", () => {
24+
expect(sum([8])).toBe(8);
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
25-
30+
test("given an array with negative numbers, returns correct sum", () => {
31+
expect(sum([-50, -20, 10])).toBe(-60);
32+
});
2633
// Given an array with decimal/float numbers
2734
// When passed to the sum function
2835
// Then it should return the correct total sum
29-
36+
test("given an array with decimal/float numbers, returns correct sum", () => {
37+
expect(sum([2.8, 4.2, 7.7])).toBe(14.7);
38+
});
3039
// Given an array containing non-number values
3140
// When passed to the sum function
3241
// Then it should ignore the non-numerical values and return the sum of the numerical elements
42+
test("given an array with non number values, returns sum of numbers", () => {
43+
expect(sum(["quiet", -20, "good", 10, 25])).toBe(15);
44+
});
3345

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("returns 0 foe non number values", () => {
50+
expect(sum(["sally", "cousin", "hi", "a"])).toBe(0);
51+
});

Sprint-1/refactor/includes.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// Refactor the implementation of includes to use a for...of loop
22

33
function includes(list, target) {
4-
for (let index = 0; index < list.length; index++) {
5-
const element = list[index];
4+
for (const element of list) {
65
if (element === target) {
76
return true;
87
}

0 commit comments

Comments
 (0)