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
10 changes: 7 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
// 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];
return median;
if (!Array.isArray(list)) {return null;}
let numberArray = list.filter(n => Number.isFinite(n))
if (numberArray.length == 0) {return null};
numberArray.sort((a, b) => a - b)

if (numberArray.length % 2 == 1) {return numberArray[((numberArray.length - 1) / 2)]}
else {return (numberArray[((numberArray.length / 2))] + numberArray[((numberArray.length / 2) - 1)]) / 2}
}

module.exports = calculateMedian;
12 changes: 11 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
function dedupe() {}
function dedupe(array) {
const unique = [];
array.forEach((item) => {
if (!unique.includes(item)) {
unique.push(item);
}
});
return unique
}

module.exports = dedupe;
28 changes: 27 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,39 @@ 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");
describe("dedupe", () => {
[
{ input: [], expected: [] },
].forEach(({ input, expected }) =>
it(`returns empty array for [${input}]`, () => expect(dedupe(input)).toEqual(expected))
);


// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array

[
{ input: [1, 2, 3], expected: [1, 2, 3] },
{ input: [3, 2, 1, 4, 5], expected: [3, 2, 1, 4, 5] },
{ input: ["hello", "a", "b", "c"], expected: ["hello", "a", "b", "c"] },
{ input: [1, 2, "hello", 4, "a", 6], expected: [1, 2, "hello", 4, "a", 6] },
].forEach(({ input, expected }) =>
it(`returns the median for [${input}]`, () => expect(dedupe(input)).toEqual(expected))
);

// 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
// first occurrence of each element from the original array.

[
{ input: [1, 1, 2, 2, 3, 3,], expected: [1, 2, 3] },
{ input: ["a", "a", "b", "b", "c", "c"], expected: ["a", "b", "c"] },
{ input: ["qwe", "qwe", 3, 3, "hello", "hello" , "hello"], expected: ["qwe", 3, "hello"] },
{ input: ["a", "a", "a", "a"], expected: ["a"] },
{ input: [1, 2, 2, 2, 2, 2], expected: [1, 2] },
].forEach(({ input, expected }) =>
it(`returns the median for [${input}]`, () => expect(dedupe(input)).toEqual(expected))
);
});
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(list) {
const numbers = list.filter(n => Number.isFinite(n));
if (numbers.length == 0) { return "-Infinity"}
else {return Math.max(...numbers)};
}

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

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

describe("calculateMedian", () => {
// 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");

it("empty array returns -Infinity", () => {
const list = [];
expect(findMax(list)).toEqual("-Infinity");
});

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

it("array with one number returns that number", () => {
const list = [2];
expect(findMax(list)).toEqual(2);
});

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

it("array with +ve and -ve numbers, returns largest number overall", () => {
const list = [10, -15];
expect(findMax(list)).toEqual(10);
});


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

it("array -ve numbers, returns closest to zero", () => {
const list = [-1, -2, -3, -15];
expect(findMax(list)).toEqual(-1);
});


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

it("array with decimal numbers should return largest decimal number", () => {
const list = [1.9, 2.8999, 3.1];
expect(findMax(list)).toEqual(3.1);
});


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

it("array with non-number values, returns the max and ignore non-numeric values", () => {
const list = [1, "a", 2, "b", 3, "c"];
expect(findMax(list)).toEqual(3);
});


// 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

it("array with only non-number values, return the least surprising value given how it behaves for all other inputs", () => {
const list = ["a", "b", "c"];
expect(findMax(list)).toEqual("-Infinity");
});
});
6 changes: 5 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
function sum(elements) {
function sum(list) {
const numbers = list.filter(n => Number.isFinite(n));
let total = 0;
for (n of numbers) {total += n};
return total;
}

module.exports = sum;
31 changes: 30 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,56 @@ const sum = require("./sum.js");

// Acceptance Criteria:

describe("sum", () => {
// Given an empty array
// When passed to the sum function
// Then it should return 0
test.todo("given an empty array, returns 0")
it("empty array returns 0", () => {
const list = [];
expect(sum(list)).toEqual(0);
});

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

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum

it("array with negative numbers returns correct sum", () => {
const list = [-1, -2, -3];
expect(sum(list)).toEqual(-6);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum

it("array with decimal numbers returns correct sum", () => {
const list = [1.1, 2.2, 3.3];
expect(sum(list)).toEqual(6.6);
});

// 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

it("array with non-number values returns sum of numerical values only", () => {
const list = [1, 2, "apple", 4, "banana", 3];
expect(sum(list)).toEqual(10);
});

// 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

it("array with non-number values only returns least surprising output", () => {
const list = ["a", "b", "c"];
expect(sum(list)).toEqual(0);
});
});
13 changes: 10 additions & 3 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// Refactor the implementation of includes to use a for...of loop

function includes(list, target) {
for (item of list) {if (item === target) {return true;}}
return false;
}

module.exports = includes;


/* Old code:

for (let index = 0; index < list.length; index++) {
const element = list[index];
if (element === target) {
return true;
}
}
return false;
}

module.exports = includes;
*/
Loading