Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
69c0396
Adjusted code for medium with even and odd numbers.
HM-127BTY Oct 31, 2026
6c18a29
Shortened the tests by using // to make it less overwhelming
HM-127BTY Oct 31, 2026
a992437
removed // from more tests and see those tests failed, now working on…
HM-127BTY Nov 2, 2026
328458f
added a sort for the array with : `list.sort((a, b) => a - b)` and ch…
HM-127BTY Nov 2, 2026
fcd612f
removed // from more tests and ran into fail with "doesn't modify the…
HM-127BTY Nov 2, 2026
28f23bb
Added in "copyList" so that I could make a copy of original Array wit…
HM-127BTY Nov 2, 2026
e3a93cc
starting on next test for throwing error on empty array
HM-127BTY Nov 2, 2026
3e41283
Added a throw new Error when the array is empty.
HM-127BTY Nov 2, 2026
9590c71
remove // from more tests and now working on the new errors t obe thrown
HM-127BTY Nov 2, 2026
8fc182a
adding in .some to check for anything that isnt a number with .some a…
HM-127BTY Nov 2, 2026
1f84354
Removed last // and run tests to see that all tests are working.
HM-127BTY Nov 2, 2026
a8d6728
Making the code to deduplicate, found out about ...Set and attempting…
HM-127BTY Nov 2, 2026
473b428
got my code to work for the first test, better understanding of ...ne…
HM-127BTY Nov 2, 2026
fed43f7
got into the flow of adding tests and ran into format bug, from lines…
HM-127BTY Nov 2, 2026
e6a2a7f
Added in tests and finished of task.
HM-127BTY Nov 2, 2026
bf24000
Made the code required and done 2 tests sofar, one with single number…
HM-127BTY Nov 3, 2026
b6fc9e2
Added more tests to look for median.
HM-127BTY Nov 3, 2026
349d421
added code and test for when array is empty to return Infinity
HM-127BTY Nov 3, 2026
0eb9caf
Added test and code to throw error when is not array and array not co…
HM-127BTY Nov 3, 2026
d48bbb7
Built code and added first test to check for Mean of numbers in array.
HM-127BTY Nov 3, 2026
5090836
fixed errors and removed todo. added test for single number in rray
HM-127BTY Nov 3, 2026
6f6333d
added test and code for empty array
HM-127BTY Nov 3, 2026
3cfe575
Added code and test for thrown error for when not array
HM-127BTY Nov 3, 2026
7ddb8cb
editing test order as showing in Acceptance Criteria.
HM-127BTY Nov 3, 2026
92cea6a
Added in describeMedian to the code and first test.
HM-127BTY Nov 3, 2026
59cd442
added thrown error in code and test for empty array
HM-127BTY Nov 3, 2026
904a8ae
Finished adding in the catch part for the tests and in teh code.
HM-127BTY Nov 3, 2026
9977484
Built code for sum, was able to copy paste from past tasks in this sp…
HM-127BTY Nov 3, 2026
c4c5fbc
added in some codes to work with the sum of numbers.
HM-127BTY Nov 3, 2026
9d8c1e2
added in test and code for decimals up to 2 decimals.
HM-127BTY Nov 3, 2026
31b226e
added in thrown error for value that isn't a number
HM-127BTY Nov 3, 2026
cfe837f
added in thrown error test for no array
HM-127BTY Nov 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,31 @@
// Hint: Please consider scenarios when 'list' isn't an array, is empty,
// or contains values that aren't numbers (the function is expected to throw - see the tests).

// function calculateMedian(list) {
// const middleIndex = Math.floor(list.length / 2);
// const median = list.splice(middleIndex, 1)[0];
// return median;
// }

// module.exports = calculateMedian;

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
if (!Array.isArray(list)) {
throw new Error("calculateMedian requires an array of numbers");
} else if (list.some((item) => typeof item !== "number")) {
throw new Error("calculateMedian requires an array of numbers");
} else if (list.length === 0) {
throw new Error("calculateMedian requires a non-empty array");
}
const copyList = Array.from(list);
let sortList = copyList.sort((a, b) => a - b);
if (sortList.length % 2 === 1) {
const middleIndex = Math.floor(sortList.length / 2);
return sortList[middleIndex];
}
const rightMiddleIndex = Math.floor(sortList.length / 2 - 1);
const leftMiddleIndex = Math.floor(sortList.length / 2);
return (sortList[rightMiddleIndex] + sortList[leftMiddleIndex]) / 2;
}

module.exports = calculateMedian;
6 changes: 5 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
function dedupe() {}
function dedupe(array) {
return [...new Set(array)];
}

module.exports = dedupe;
48 changes: 39 additions & 9 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,58 @@
const dedupe = require("./dedupe.js");
/*
Dedupe Array

📖 Dedupe means **deduplicate**

*/
describe("deduplicate", () => {
/*
In this kata, you will need to deduplicate the elements of an array

E.g. dedupe(['a','a','a','b','b','c']) returns ['a','b','c']
E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) returns [5, 1, 2, 3, 8]
E.g. dedupe([1, 2, 1]) returns [1, 2]
*/

// Acceptance Criteria:

// Given an empty array
// When passed to the dedupe function
// Then it should return an empty array
test.todo("given an empty array, it returns an empty array");
// Acceptance Criteria:

// Given an empty array
// When passed to the dedupe function
// Then it should return an empty array
it(`returns the empty array when given an empty array`, () => {
expect(dedupe([])).toEqual([]);
});
});
// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
it("doesn't modify the input array [3, 1, 1, 2]", () => {
const array = [3, 1, 1, 2];
dedupe(array);
expect(array).toEqual([3, 1, 1, 2]);
});

// Given an array of strings or numbers
// When passed to the dedupe function
// Then it should return a new array with duplicates removed while preserving the
// Then it should return a new array with duplicates removed while preserving the
// first occurrence of each element from the original array.
it(`returns the deduplicate for string letters`, () => {
expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]);
});

it(`returns the deduplicate for numbers`, () => {
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
});

it(`returns the deduplicate while not numerical in order`, () => {
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
});

it(`returns the deduplicate for string and numbers `, () => {
expect(dedupe([5, 1, 1, 2, 3, "a", "a", "a", "c"])).toEqual([
5,
1,
2,
3,
"a",
"c",
]);
});
8 changes: 7 additions & 1 deletion Sprint-1/implement/describe-median.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ function calculateMedian(list) {
}

// Implement this function. See describe-median.test.js for the acceptance criteria.
function describeMedian(list) {}
function describeMedian(list) {
try {
return `The median is ${calculateMedian(list)}`;
} catch (error) {
return `Could not calculate a median: ${error.message}`;
}
}

module.exports = describeMedian;
14 changes: 13 additions & 1 deletion Sprint-1/implement/describe-median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,24 @@ const describeMedian = require("./describe-median.js");
// When passed to describeMedian
// Then it should return "The median is " followed by the median
// Delete this test.todo and replace it with a test.
test.todo('given [1, 2, 3], returns "The median is 2"');
it("returns 'The median is 2'", () => {
expect(describeMedian([1, 2, 3])).toEqual("The median is 2");
});

// Given an empty array
// When passed to describeMedian
// Then it should return "Could not calculate a median: calculateMedian requires a non-empty array"
it("returns error 'Could not calculate a median: calculateMedian requires a non-empty array' when array empty", () => {
expect(describeMedian([])).toEqual(
"Could not calculate a median: calculateMedian requires a non-empty array"
);
});

// Given something that isn't an array of numbers, e.g. "banana"
// When passed to describeMedian
// Then it should return "Could not calculate a median: calculateMedian requires an array of numbers"
it("Throws error 'calculateMedian requires an array of numbers' when it isn't an array of numbers", () => {
expect(describeMedian(["banana"])).toEqual(
"Could not calculate a median: calculateMedian requires an array of numbers"
);
});
15 changes: 15 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
function findMax(elements) {
if (elements.length === 0) {
return "Infinity";
}
Comment on lines +2 to +4

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elements does not necessary have the .length property/attribute. A better approach can be making sure it is an array first. Please fix it. Thank you.

if (!Array.isArray(elements)) {
throw new Error("findMax requires an array of numbers");
}

const copyElements = Array.from(elements);
for (const item of copyElements) {
if (typeof item !== "number") {
throw new Error("findMax requires an array of numbers");
}
}
let sortElements = copyElements.sort((a, b) => a - b);
return sortElements.at(-1);
}

module.exports = findMax;
26 changes: 25 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,52 @@ const findMax = require("./max.js");
// When passed to the max function
// Then it should return -Infinity
// Delete this test.todo and replace it with a test.
test.todo("given an empty array, returns -Infinity");
it("returns the Infinity for []", () => {
expect(findMax([])).toEqual("Infinity");
});
Comment on lines +19 to +21

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems not quite accurate to me. Please consider reading line 17 very carefully, and fix the typo in this test case. Thank you.


// Given an array with one number
// When passed to the max function
// Then it should return that number
it("returns the median for [40]", () => {
expect(findMax([40])).toEqual(40);
});

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall
it("returns the median for [20, 30, 10, 40]", () => {
expect(findMax([20, 30, 10, 40])).toEqual(40);
});

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
it("returns the median for [-40, -20, -30, -70]", () => {
expect(findMax([-40, -20, -30, -70])).toEqual(-20);
});

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
it("returns the median for [-40.4, -20.7, -30.3, -70.6]", () => {
expect(findMax([-40.4, -20.7, -30.3, -70.6])).toEqual(-20.7);
});

// Given an array containing a value that isn't a number
// When passed to the max function
// Then it should throw Error("findMax requires an array of numbers")
it("Throws error 'findMax requires an array of numbers' when containing a value that isn't a number", () => {
expect(() => findMax([10, 40, "hello", 30])).toThrow(
new Error("findMax requires an array of numbers")
);
});

// Given something that isn't an array at all, such as "hey", 42 or no argument
// When passed to the max function
// Then it should throw Error("findMax requires an array of numbers")
it("Throws error 'findMax requires an array of numbers' Given something that isn't an array at all", () => {
expect(() => findMax(42)).toThrow(
new Error("findMax requires an array of numbers")
);
});
17 changes: 16 additions & 1 deletion Sprint-1/implement/mean.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
function calculateMean(list) {}
function calculateMean(list) {
if (list.length === 0) {
throw new Error("calculateMean requires a non-empty array");
}
Comment on lines +2 to +4

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

list does not necessary have the .length property/attribute. A better approach can be making sure it is an array first. Please fix it. Thank you.


if (!Array.isArray(list)) {
throw new Error("calculateMean requires an array of numbers");
}

const copyList = Array.from(list);
let total = 0;
for (let i = 0; i < copyList.length; i++) {
total += copyList[i];
}
return total / copyList.length;
}

module.exports = calculateMean;
36 changes: 35 additions & 1 deletion Sprint-1/implement/mean.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,62 @@ const calculateMean = require("./mean.js");
// When passed to calculateMean
// Then it should return their mean
// Delete this test.todo and replace it with a test.
test.todo("given [1, 2, 6], returns 3");
it("Returns correct value of 3 for mean of numbers in array of [1, 2, 6]", () => {
expect(calculateMean([1, 2, 6])).toEqual(3);
});

// Given an array with a single number
// When passed to calculateMean
// Then it should return that number
it("returns mean of single number in array", () => {
expect(calculateMean([6])).toEqual(6);
});

// Given an array containing negative or decimal numbers
// When passed to calculateMean
// Then it should return the correct mean
it("returns mean containing negative or decimal numbers in array", () => {
expect(calculateMean([-6, 8.5, 23])).toEqual(8.5);
});

// Given an empty array
// When passed to calculateMean
// Then it should throw Error("calculateMean requires a non-empty array")
it("Throws error 'calculateMean requires a non-empty array' Given empty", () => {
expect(() => calculateMean([])).toThrow(
new Error("calculateMean requires a non-empty array")
);
});

// Given a value that isn't an array, e.g. "banana", 42, null or {}
// When passed to calculateMean
// Then it should throw Error("calculateMean requires an array of numbers")
it("Throws error 'calculateMean requires an array of numbers' Given value that isn't an array", () => {
expect(() =>
calculateMean("Hello bob").toThrow(
new Error("calculateMean requires an array of numbers")
)
);
});

// Given no argument at all
// When passed to calculateMean
// Then it should throw Error("calculateMean requires an array of numbers")
it("Throws error 'calculateMean requires an array of numbers' Given Given no argument at all", () => {
expect(() =>
calculateMean().toThrow(
new Error("calculateMean requires an array of numbers")
)
);
});

// Given an array containing a non-number value, e.g. [1, "2", 3]
// When passed to calculateMean
// Then it should throw Error("calculateMean requires an array of numbers")
it("Throws error 'calculateMean requires an array of numbers' an array containing a non-number value, e.g. [1, '2', 3]", () => {
expect(() =>
calculateMean([1, "2", 3]).toThrow(
new Error("calculateMean requires an array of numbers")
)
);
});
22 changes: 22 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
function sum(elements) {
if (elements.length === 0) {
return 0;
}
Comment on lines +2 to +4

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elements does not necessary have the .length property/attribute. A better approach can be making sure it is an array first. Please fix it. Thank you.

if (!Array.isArray(elements)) {
throw new Error("sum requires an array of numbers");
}
for (const item of elements) {
if (typeof item !== "number") {
throw new Error("sum requires an array of numbers");
}

const copyElements = Array.from(elements);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For your future reference, copy an array is not needed as your algorithm just reads but does not modify any elements in it.

let total = 0;
for (let i = 0; i < copyElements.length; i++) {
total += copyElements[i];
}
if (Number.isInteger(total)) {
return total;
} else {
return Number(total.toFixed(2));
}
}
}

module.exports = sum;
23 changes: 22 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,45 @@ const sum = require("./sum.js");
// Given an empty array
// When passed to the sum function
// Then it should return 0
test.todo("given an empty array, returns 0")
it("returns 0 when given an empty array", () => {
expect(sum([])).toEqual(0);
});

// Given an array with just one number
// When passed to the sum function
// Then it should return that number
it("returns same number when given an array with just one number", () => {
expect(sum([5])).toEqual(5);
});

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
it("returns total when given an array with negative numbers", () => {
expect(sum([-5, -2, -10])).toEqual(-17);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
it("returns total when given an array with decimal/float numbers", () => {
expect(sum([2.6, 4.6, 8.12])).toEqual(15.32);
});

// Given an array containing a value that isn't a number
// When passed to the sum function
// Then it should throw Error("sum requires an array of numbers")
it("throws an error 'sum requires an array of numbers' when value isn't a number", () => {
expect(()=> sum(["hello", "bob"])).toThrow(
new Error("sum requires an array of numbers")
);
});

// Given something that isn't an array at all, such as "hey", 42 or no argument
// When passed to the sum function
// Then it should throw Error("sum requires an array of numbers")
it("throws an error 'sum requires an array of numbers' when value isn't an array", () => {
expect(() => sum(["2", 2, 5, "bob"])).toThrow(
new Error("sum requires an array of numbers")
);
});
Loading