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
15 changes: 13 additions & 2 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@
// 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];
if (!Array.isArray(list)) return null;

const numbers = list.filter(val => typeof val === 'number');
if (numbers.length === 0) return null;
numbers.sort((a, b) => a - b);

if (numbers.length % 2 === 0) {
const mid1 = numbers[numbers.length / 2 - 1];
const mid2 = numbers[numbers.length / 2];
return (mid1 + mid2) / 2;
}
const middleIndex = Math.floor(numbers.length / 2);
const median = numbers[middleIndex];
return median;
}

Expand Down
11 changes: 10 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
function dedupe() {}
function dedupe(list) {
let listWithoutDuplicates = [];
for (let i = 0; i < list.length; i++) {
if(listWithoutDuplicates.includes(list[i]) === false) {
listWithoutDuplicates.push(list[i]);
}
}
return listWithoutDuplicates;
}
module.exports = dedupe;
16 changes: 16 additions & 0 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,19 @@ test.todo("given an empty array, it returns an empty 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

describe("dedupe", () => {
it("given an empty array, it returns an empty array", () => {
expect(dedupe([])).toEqual([]);
});

it("given an array with no duplicates, it returns a copy of the original array", () => {
expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]);
});

it("given an array with strings or numbers, it removes the duplicate values, preserving the first occurrence of each element", () => {
expect(dedupe(['a','a','a','b','b','c'])).toEqual(['a','b','c']);
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
});
});
9 changes: 8 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
function findMax(elements) {
let max = -Infinity;
for (let i = 0; i < elements.length; i++) {
if (typeof elements[i] === "number" && elements[i] > max) {
max = elements[i];
}
}
return max;
}

module.exports = findMax;
module.exports = findMax;
25 changes: 25 additions & 0 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,28 @@ test.todo("given an empty array, returns -Infinity");
// 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("max", () => {
test("given an empty array, returns -Infinity", () => {
expect(findMax([])).toBe(-Infinity);
});

test("given an array with one number, returns that number", () => {
expect(findMax([42])).toBe(42);
});
test("given an array with both positive and negative numbers, returns the largest number overall", () => {
expect(findMax([-10, 0, 5, -20, 15])).toBe(15);
});
test("given an array with just negative numbers, returns the closest one to zero", () => {
expect(findMax([-50, -20, -5, -1])).toBe(-1);
});
test("given an array with decimal numbers, returns the largest decimal number", () => {
expect(findMax([1.5, 2.3, 0.7, 3.8])).toBe(3.8);
});
test("given an array with non-number values, returns the max and ignores non-numeric values", () => {
expect(findMax(['hey', 10, 'hi', 60, 10])).toBe(60);
});
test("given an array with only non-number values, returns the least surprising value given how it behaves for all other inputs", () => {
expect(findMax(['hey', 'hi', 'hello'])).toBe(-Infinity);
});
});
7 changes: 7 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
function sum(elements) {
let total = 0;
for (let element of elements) {
if (typeof element === 'number') {
total += element;
}
}
return total;
}

module.exports = sum;
25 changes: 25 additions & 0 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,28 @@ test.todo("given an empty array, returns 0")
// 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
describe("sum", () => {
test("given an empty array, returns 0", () => {
expect(sum([])).toBe(0);
});

test("given an array with just one number, returns that number", () => {
expect(sum([5])).toBe(5);
});

test("given an array containing negative numbers, returns the correct total sum", () => {
expect(sum([-1, -2, -3])).toBe(-6);
});

test("given an array with decimal/float numbers, returns the correct total sum", () => {
expect(sum([1.5, 2.5, 3.0])).toBe(7.0);
});

test("given an array containing non-number values, ignores them and returns the sum of the numerical elements", () => {
expect(sum(['hey', 10, 'hi', 60, 10])).toBe(80);
});

test("given an array with only non-number values, returns 0", () => {
expect(sum(['hey', 'hi', 'hello'])).toBe(0);
});
});
7 changes: 2 additions & 5 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
// 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 (const element of list) {
if (element === target) {
return true;
}
}
return false;
}

module.exports = includes;
module.exports = includes;