Skip to content
Open
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
18 changes: 16 additions & 2 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,23 @@
// or 'list' has mixed values (the function is expected to sort only numbers).

function calculateMedian(list) {
if (!Array.isArray(list)) {
return null;
}
list = list.filter((item) => typeof item === "number");
if (list.length === 0) {
return null;
}

list.sort((a, b) => a - b);
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
const sumOfIndex = list[middleIndex] + list[middleIndex - 1];
const middleValue = sumOfIndex / 2;
if (list.length % 2 > 0) {
return list[middleIndex];
} else {
return middleValue;
}
}

module.exports = calculateMedian;
14 changes: 13 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
function dedupe() {}
function dedupe(arr) {
if (arr.length === 0) {
return [];
}
const uniqueSet = new Set(arr);
if (arr.length !== uniqueSet.size) {
return [...uniqueSet];
} else {
return [...arr];
}
}

module.exports = dedupe;
48 changes: 46 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,57 @@ E.g. dedupe([1, 2, 1]) returns [1, 2]
// 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");
test("given an empty array, it returns an empty array", () => {
const arr = [];
const emptyArray = dedupe(arr);
expect(emptyArray).toEqual([]);
});

// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
test("the function should return the cope of original array when no duplicates of array pass to it", () => {
const arr = [2, 3, 4, 5];

const result = dedupe(arr);
expect(result).toEqual(arr);
expect(result).not.toBe(arr);
});
test("the function should return the cope of original array when no duplicates of array pass to it ", () => {
const arr = [13, 9, 8, 7];

const result = dedupe(arr);
expect(result).toEqual(arr);
expect(result).not.toBe(arr);
});
// 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.
test("the function should return the first occurrence of each element when duplicated arrays of element pass to it", () => {
const arr = [3, 3, 3, 7, 8, 9, 9, 6, 6];

const newResult = dedupe(arr);
expect(newResult).toEqual([3, 7, 8, 9, 6]);
});
test("the function should return the first occurrence of each element when duplicated arrays of element pass to it", () => {
const arr = [3, 3, "hi", 8, 8, "hi", "you", 8, "you", "hi"];

const newResult = dedupe(arr);
expect(newResult).toEqual([3, "hi", 8, "you"]);
});
test("the function should return the first occurrence of each element when duplicated arrays of element pass to it", () => {
const arr = [
"orange",
"egg",
"egg",
"banana",
"orange",
"apple",
"banana",
"apple",
];

const newResult = dedupe(arr);
expect(newResult).toEqual(["orange", "egg", "banana", "apple"]);
});
16 changes: 16 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
function findMax(elements) {
if (elements.length === 0) {
return Infinity;
} else if (elements.length === 1) {
return elements[0];
}
const number = elements.filter((value) => typeof value === "number");
if (number.length === 0) {
return undefined;
}
let max = elements[0];
for (let i = 0; i < elements.length; i++) {
if (max < elements[i]) {
max = elements[i];
}
}
return max;
}

module.exports = findMax;
57 changes: 51 additions & 6 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,73 @@ 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");
test("when given an empty array to the function it returns Infinity", () => {
const elements = [];

const result = findMax(elements);
expect(result).toEqual(Infinity);
});

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

test("when the given is array of one number it should return that number", () => {
const elements = [30];
const result = findMax(elements);
expect(result).toEqual(30);
});
// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall

test("should return the largest number when the array contains both negative and positive numbers", () => {
const elements = [-10, -12, -7, 0, 2, 4];
const result = findMax(elements);
expect(result).toEqual(4);
});
test("should return the largest number when the array contains both negative and positive numbers", () => {
const elements = [-20, -6, -2, 0, 7, 9, 5];
const result = findMax(elements);
expect(result).toEqual(9);
});
// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero

test("should return number that is closest to zero when the array contains all negative numbers", () => {
const elements = [-20, -6, -2, -4, -7, -9, -5];
const result = findMax(elements);
expect(result).toEqual(-2);
});
test("should return number that is closest to zero when the array contains all negative numbers", () => {
const elements = [-20, -11, -1, 0, -7, -9, -5];
const result = findMax(elements);
expect(result).toEqual(0);
});
// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number

test("should return the largest decimal number when the array contains decimal numbers", () => {
const elements = [2.7, 6.3, 7.3, 4.9, 7.9, 4.5];
const result = findMax(elements);
expect(result).toEqual(7.9);
});
test("should return the largest decimal number when the array contains decimal numbers", () => {
const elements = [11.8, 16.3, 27.3, 41.9, 0.9, 14.5];
const result = findMax(elements);
expect(result).toEqual(41.9);
});
// Given an array with non-number values
// When passed to the max function
// Then it should return the max and ignore non-numeric values

test("should return the largest number ignoring non-numerical values when the array contains non-numerical value", () => {
const elements = [11, "apple", 27, "hi", 9, "zero"];
const result = findMax(elements);
expect(result).toEqual(27);
});
// 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
test("should return only number value, when the array contains non-numerical value", () => {
const elements = ["apple", "hi", "book", "zero"];
const result = findMax(elements);
expect(result).toEqual(undefined);
});
15 changes: 15 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
function sum(elements) {
if (elements.length === 0) {
return 0;
}
const number = elements.filter((value) => typeof value === "number");
if (number.length === 0) {
return undefined;
}
if (elements.length === 1) {
return elements[0];
}
let arraySum = 0;
for (let i = 0; i < number.length; i++) {
arraySum += number[i];
}
return arraySum;
}

module.exports = sum;
44 changes: 40 additions & 4 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,60 @@ 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")
test("when empty array is given it should returns 0", () => {
const elements = [];
const result = sum(elements);
expect(result).toEqual(0);
});

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

test("when array with just one number given it should returns that number", () => {
const elements = [13];
const result = sum(elements);
expect(result).toEqual(13);
});
test("when array with just one number given it should returns that number", () => {
const elements = [9];
const result = sum(elements);
expect(result).toEqual(9);
});
// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum

test("when array with negative numbers given it should return the correct total sum", () => {
const elements = [-9, -2, -4, -7];
const result = sum(elements);
expect(result).toEqual(-22);
});
test("when array with negative numbers given it should return the correct total sum", () => {
const elements = [-11, -9, -2, -5];
const result = sum(elements);
expect(result).toEqual(-27);
});
// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum

test("when array that contain decimal or float number pass to the function it should return the correct total sum", () => {
const elements = [0.9, 1 / 2, 4.3, 3.7, 1 / 4];
const result = sum(elements);
expect(result).toEqual(9.649999999999999);
});
// Given an array containing non-number values
// When passed to the sum function
// Then it should ignore the non-numerical values and return the sum of the numerical elements
test("when array that contain number and non-number pass to the function it should return the sum of the numerical elements", () => {
const elements = [9, 1, 4, "one", 3, "two", 4];
const result = sum(elements);
expect(result).toEqual(21);
});

// Given an array with only non-number values
// When passed to the sum function
// Then it should return the least surprising value given how it behaves for all other inputs
test("when array that contain non-number pass to the function it should return undefined", () => {
const elements = ["window", "one", "name", "two"];
const result = sum(elements);
expect(result).toEqual(undefined);
});
3 changes: 1 addition & 2 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Refactor the implementation of includes to use a for...of loop

function includes(list, target) {
for (let index = 0; index < list.length; index++) {
const element = list[index];
for (let element of list) {
if (element === target) {
return true;
}
Expand Down
Loading