Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 17 additions & 2 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,23 @@
// or 'list' has mixed values (the function is expected to sort only numbers).

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
let median = null;
//return null if the list is not an array
if (!Array.isArray(list)) return median;

// return null if none of the items in the array is a number
if (!list.some((item) => typeof item === "number")) return median;

let numericArray = list.filter((item) => typeof item === "number");
let sortedArray = numericArray.toSorted((a, b) => a - b);
const middleIndex = Math.floor(sortedArray.length / 2);

//check if the number of items in the array are even or odd
// and then calculate the median accordingly
if (sortedArray.length % 2 === 0)
median = (sortedArray[middleIndex - 1] + sortedArray[middleIndex]) / 2;
else median = sortedArray[middleIndex];

return median;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice, organised return statement. If the above code falls through, this will catch it.
But question ❓ Can a median be null? Does it have to be a number? Or is null fine?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes, it can't be null. I have added a check at the end for that. Let me know if it seems right. Thanks

}

Expand Down
22 changes: 17 additions & 5 deletions Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ describe("calculateMedian", () => {
{ input: [1, 2, 3, 4], expected: 2.5 },
{ input: [1, 2, 3, 4, 5, 6], expected: 3.5 },
].forEach(({ input, expected }) =>
it(`returns the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
it(`returns the median for [${input}]`, () =>
expect(calculateMedian(input)).toEqual(expected))
);

[
Expand All @@ -24,7 +25,8 @@ describe("calculateMedian", () => {
{ input: [110, 20, 0], expected: 20 },
{ input: [6, -2, 2, 12, 14], expected: 6 },
].forEach(({ input, expected }) =>
it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
it(`returns the correct median for unsorted array [${input}]`, () =>
expect(calculateMedian(input)).toEqual(expected))
);

it("doesn't modify the input array [3, 1, 2]", () => {
Expand All @@ -33,8 +35,17 @@ describe("calculateMedian", () => {
expect(list).toEqual([3, 1, 2]);
});

[ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val =>
it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null))
[
"not an array",
123,
null,
undefined,
{},
[],
["apple", null, undefined],
].forEach((val) =>
it(`returns null for non-numeric array (${val})`, () =>
expect(calculateMedian(val)).toBe(null))
);

[
Expand All @@ -45,6 +56,7 @@ describe("calculateMedian", () => {
{ input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 },
{ input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 },
].forEach(({ input, expected }) =>
it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
it(`filters out non-numeric values and calculates the median for [${input}]`, () =>
expect(calculateMedian(input)).toEqual(expected))
);
});
8 changes: 7 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
function dedupe() {}
function dedupe(arr) {
if (!Array.isArray(arr)) throw new Error(arr + " is not an array");
else if (arr.length === 0) return arr;
else return Array.from(new Set(arr));
}

module.exports = dedupe;
70 changes: 59 additions & 11 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,66 @@ E.g. dedupe(['a','a','a','b','b','c']) target output: ['a','b','c']
E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) target output: [5, 1, 2, 3, 8]
E.g. dedupe([1, 2, 1]) target output: [1, 2]
*/
describe("dedupe", () => {
// Given an empty array
// When passed to the dedupe function
// Then it should return an empty array
it("given an empty array it should return an empty array", () => {
const array = [];
const dedupeArray = dedupe(array);
expect(dedupeArray).toEqual([]);
});

// Acceptance Criteria:
// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
[["a", "b", "c"], ["A", 1, "j", "?"], ["c"]].forEach((val) =>
it(`returns copy of the original array if there are no duplicates in [${val}]`, () =>
expect(dedupe(val)).toEqual(val))
);
// Given an array with strings or numbers
// When passed to the dedupe function
// Then it should remove the duplicate values, preserving the first occurrence of each element
[
{ input: [1, 2, 1, 3, 1, 2, 10, 5, 0, 10], expected: [1, 2, 3, 10, 5, 0] },
{ input: [1, 2, 1, 4], expected: [1, 2, 4] },
{ input: [1, 1, 1, 1, 1], expected: [1] },
{
input: ["banana", "apple", "apple", "banana", "apple", "banana"],
expected: ["banana", "apple"],
},
{
input: [" ", "empty", "", " ", "", "empty"],
expected: [" ", "empty", ""],
},
{ input: ["2", "2", "3", "1"], expected: ["2", "3", "1"] },
].forEach(({ input, expected }) =>
it(`returns a copy of array removing the duplicates from [${input}]`, () =>
expect(dedupe(input)).toEqual(expected))
);

// 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");
// Given an input value that is not array could be null or undefined or just a number or string
// When passed to the dedupe function
// Then it should thrown an error
test("should thrown an error if the input is null", () => {
expect(() => dedupe(null)).toThrow(null + " is not an array");
});

// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
test("should thrown an error if the input is a number", () => {
const number = 123;
expect(() => dedupe(number)).toThrow(number + " is not an array");
});
test("should thrown an error if the input is a string", () => {
const string = "this is a string";
expect(() => dedupe(string)).toThrow(string + " is not an array");
});

// Given an array with strings or numbers
// When passed to the dedupe function
// Then it should remove the duplicate values, preserving the first occurence of each element
test("should thrown an error if the input is undefined", () => {
expect(() => dedupe(undefined)).toThrow(undefined + " is not an array");
});

test("should thrown an error if the input is an object", () => {
const emptyObject = {};
expect(() => dedupe(emptyObject)).toThrow(emptyObject + " is not an array");
});
});
5 changes: 4 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
function findMax(elements) {
function findMax(array) {
if (!Array.isArray(array)) throw new Error(array + " is not an array");
const numbersArray = array.filter((value) => typeof value === "number");
return Math.max(...numbersArray);
}

module.exports = findMax;
150 changes: 121 additions & 29 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,124 @@ We have set things up already so that this file can see your function from the o

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

// Given an empty array
// 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");

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

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number

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

// Given an array with only non-number values
// When passed to the max function
// Then it should return the least surprising value given how it behaves for all other inputs
describe("findMax", () => {
// Given an empty array
// When passed to the max function
// Then it should return -Infinity
it("if an empty array is passed to the the findMax function, -Infinity should be returned", () => {
emptyArray = [];
expect(findMax(emptyArray)).toEqual(-Infinity);
});

// Given an array with one number
// When passed to the max function
// Then it should return that number
[[1], [70], [0], [-25], [100129]].forEach((val) =>
it(`When an array with only one number is passed i.e. [${val}], it should return that number`, () =>
expect(findMax(val)).toEqual(val[0]))
);

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall
[
{ input: [12, -2, 4, 6, 0], expected: 12 },
{ input: [2, 5, 990, -4], expected: 990 },
{ input: [0, -1, -5], expected: 0 },
{ input: [0, -1, 300, 3], expected: 300 },
].forEach(({ input, expected }) =>
it(`returns the max number from the array [${input}]`, () =>
expect(findMax(input)).toEqual(expected))
);

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
[
{ input: [-4, -2, -1902, -2, -1], expected: -1 },
{ input: [-9088, -9087, -990788, -4888777], expected: -9087 },
{ input: [-1, -5], expected: -1 },
].forEach(({ input, expected }) =>
it(`returns the max number from the array of negative numbers only [${input}]`, () =>
expect(findMax(input)).toEqual(expected))
);

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
[
{ input: [-4.2, 2.8, -19.8009, 2.4, 1.5], expected: 2.8 },
{ input: [-90.88, 0.001, -990.788], expected: 0.001 },
{ input: [-1.11, -5.3], expected: -1.11 },
].forEach(({ input, expected }) =>
it(`returns the max number from the array of decimal numbers [${input}]`, () =>
expect(findMax(input)).toEqual(expected))
);

// Given an array with non-number values
// When passed to the max function
// Then it should return the max and ignore non-numeric values
[
{ input: [-4, -2, "what is this", -1902, -2, "??", -1], expected: -1 },
{
input: [-9088, ".oi9e9", "1000000", -9087, 990788, -4888777],
expected: 990788,
},
{ input: [-1.11, "here", -5.233, "ignore me please"], expected: -1.11 },
].forEach(({ input, expected }) =>
it(`returns the max number from the array of numbers and non-numbers values [${input}]`, () =>
expect(findMax(input)).toEqual(expected))
);

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

/* Ans: If there is no number in the array than considering the behavior for the above inputs
the least surprising value in return would be -Infinity as our array has zero elements which are numbers*/
[
{
input: ["Not a number", "what is this", "least surprising", "??", "what"],
expected: -Infinity,
},
{
input: ["kkdkas", "Ahan!", "23"],
expected: -Infinity,
},
{
input: ["here", "Least surprising", "????", "vale is", "-Infinity"],
expected: -Infinity,
},
].forEach(({ input, expected }) =>
it(`returns the least surprising value for only non-numbers array [${input}]`, () =>
expect(findMax(input)).toEqual(expected))
);

// Given an input that is not array could be null or undefined or just a number or string
// When passed to the findMax function
// Then it should thrown an error

test("should thrown an error if the input is null", () => {
expect(() => findMax(null)).toThrow(null + " is not an array");
});

test("should thrown an error if the input is a number", () => {
const number = 980;
expect(() => findMax(number)).toThrow(number + " is not an array");
});
test("should thrown an error if the input is a string", () => {
const string = "just a string";
expect(() => findMax(string)).toThrow(string + " is not an array");
});

test("should thrown an error if the input is undefined", () => {
expect(() => findMax(undefined)).toThrow(undefined + " is not an array");
});

test("should thrown an error if the input is an object", () => {
const emptyObject = {};
expect(() => findMax(emptyObject)).toThrow(
emptyObject + " is not an array"
);
});
});
9 changes: 8 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
function sum(elements) {
function sum(array) {
if (!Array.isArray(array)) throw new Error(array + " is not an array");
let numbersArray = array.filter((value) => typeof value === "number");
const sum = numbersArray.reduce(
(accumulator, currentValue) => accumulator + currentValue,
0
);
return sum;
}

module.exports = sum;
Loading
Loading