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
36 changes: 33 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,39 @@
// 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;
//check if the input is an array
if (!Array.isArray(list)) {
return null;
}

//check if list consists of numbers
list = list.filter((item) => typeof item === "number" && !isNaN(item));

//if nothing remained in the updated list, it was non-numbers, so that should return null

if (list.length === 0) return null;

//sort a copy of the array
const listCopy = Array.from(list);
const sortedList = listCopy.sort((a, b) => a - b);

//find the position of the median
let medianPosition = (list.length + 1) / 2 - 1;

//if integer, that will be an exact position
if (medianPosition % 1 === 0) {
let median = sortedList[medianPosition];
return median;
} else {
// if not a whole number, we need to average the 2 middle numbers
let mid1 = Math.floor((list.length + 1) / 2) - 1;
let mid2 = Math.floor((list.length + 1) / 2);

median = 0.5 * (sortedList[mid1] + sortedList[mid2]);
return median;
}
}

// console.log(calculateMedian([1, 2, 3, 4, 5, 6, 7, 8]));

module.exports = calculateMedian;
18 changes: 17 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
function dedupe() {}
function dedupe(array) {
//create a duplicate,to avoid changing the array
const copiedArray = Array.from(array);

//Initialise an empty array (to collect non-duplicates in)
const dedupedArray = [];

//create a for loop to collect non-duplicates
for (element of copiedArray) {
if (!dedupedArray.includes(element)) {
dedupedArray.push(element);
}
}
return dedupedArray;
}

module.exports = dedupe;
12 changes: 10 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ 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("should return an empty array for 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
test("should return the original array, if all elements are unique in it", () => {
expect(dedupe([4, 5, "hello"])).toEqual([4, 5, "hello"]);
});

// 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("should return same elements, same order, but duplicates removed for an array with duplicates", () => {
expect(dedupe(["a", "a", 3, "b", 3, "b", "c"])).toEqual(["a", 3, "b", "c"]);
});
6 changes: 6 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
function findMax(elements) {
// filter out non-number items from elements
elements = elements.filter((element) => typeof element === "number");

// find maximum
const max = Math.max(...elements);
return max;
}

module.exports = findMax;
23 changes: 21 additions & 2 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,48 @@ 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");
test("should return -Infinity for an empty array", () => {
expect(findMax([])).toEqual(-Infinity);
});

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

// 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 passed negative and positive numbers", () => {
expect(findMax([-5, 5, -2, 2])).toEqual(5);
});

// 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 the number closest to 0, when the numbers are all negative in the array", () => {
expect(findMax([-5, -3, -1, -9])).toEqual(-1);
});

// 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 number among decimals", () => {
expect(findMax([2.3, 1.6, 9.4, 11.25])).toEqual(11.25);
});

// 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 ignore non-numeric values, and calculate the max of the numbers", () => {
expect(findMax(["all", null, true, [2, 3], 3, 7])).toEqual(7);
});

// 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 undefined if all the elements are non-numbers", () => {
expect(findMax(["nope", null, []])).toEqual(-Infinity);
});
13 changes: 13 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
function sum(elements) {
//filter out non-numbers
elements = elements.filter((element) => typeof element === "number");

// initialise a sum total
total = 0;

// loop through the elements of the array and add each to the total
for (let i = 0; i < elements.length; i++) {
total = total + elements[i];
}

// finally return the total
return total;
}

module.exports = sum;
20 changes: 19 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,42 @@ 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("should return 0 for 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
test("should return the number for an array of that single 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
test("should return the correct sum even with negative numbers in the array", () => {
expect(sum([-5, -2, -3, 5])).toEqual(-5);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test("should correctly sum an array containing decimals", () => {
expect(sum([2.3, 2.7])).toEqual(5);
});

// 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("should ignore non-numbers and calculate the sum of the numbers in the array", () => {
expect(sum([null, "", 5, 7, true, "ten"])).toEqual(12);
});

// 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("should return 0 for only non-number inputs", () => {
expect(sum([null, "", true, "ten"])).toEqual(0);
});
14 changes: 11 additions & 3 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// 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];
// if (element === target) {
// return true;
// }
// }
// return false;
// }

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

module.exports = includes;
Loading