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
20 changes: 17 additions & 3 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) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
// this line of code checks if the list is an array and if it has any elements. If not, it returns null
if (!Array.isArray(list) || list.length === 0) {
return null;
}
// this line of code filters the list to only include numbers. And if there are no numbers in the list, it returns null
const numbersOnly = list.filter((item) => typeof item === "number");
if (numbersOnly.length === 0) {
return null;
}
// this line of code sorts the numbers in ascending order and calculates the median based on whether the length of the sorted array is even or odd
const sorted = [...numbersOnly].sort((a, b) => a - b);
const middleIndex = Math.floor(sorted.length / 2);
if (sorted.length % 2 === 0) {
const median = (sorted[middleIndex - 1] + sorted[middleIndex]) / 2;
return median;
}
return sorted[middleIndex];
}

module.exports = calculateMedian;
21 changes: 20 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
function dedupe() {}
function dedupe(arr) {
if (!Array.isArray(arr)) {
return null;
}

const newVersion = new Set(arr);

// If no duplicates, return the original array
if (newVersion.size === arr.length) {
return arr;
}

// Otherwise return the deduped array
const newArray = [...newVersion];
return newArray;
}

console.log(dedupe([1, 2, 3]));
console.log(dedupe([10, 30, 20, 50, 20, 10]));
console.log(dedupe(5, 1, 1, 2, 3, 2, 5, 8));
5 changes: 4 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ test.todo("given an empty array, it returns an empty array");

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

*/
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) {
let max = -Infinity;
for (const value of elements) {
if (typeof value === "number" && !Number.isNaN(value)) {
if (value > max) {
max = value;
}
}
}
return max;
}
console.log(findMax([])); // -Infinity
console.log(findMax([42])); // 42
console.log(findMax([-10, 20, -5, 7])); // 20
console.log(findMax([-50, -3, -100])); // -3
console.log(findMax([1.5, 3.2, 2.8])); // 3.2
console.log(findMax(["hey", 10, "hi", 60, 10])); // 60
console.log(findMax(["a", {}, null])); // -Infinity

module.exports = findMax;
27 changes: 21 additions & 6 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,43 @@ 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("given an empty array, it should return -Infinity", () => {
expect(findMax([])).toBe(-Infinity);
});

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

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

test("given an array with both positive and negative numbers, it should return the largest overall", () => {
expect(findMax([99, 86])).toBe(99);
});
// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero

test("given an array with negative numbers, it should return the closest number to zero", () => {
expect(findMax([-14, -24, -1120, -7])).toBe(-7);
});
// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number

test("given an array with decimal numbers, it should return the largest decimal number", () => {
expect(findMax([0.8, 1.3, 1.79, 1.8])).toBe(1.8);
});
// 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("given an array with non-numeric values, it should return the max without non-numeric values", () => {
expect(findMax(["yay", 12, "yey", 7, "yonatan", 31])).toBe(31);
});
// 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("given an array with only non-number values, it should return the least suprising value given", () => {
expect(findMax(["f", "r", {}, null, "$"])).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 (const myNum of elements) {
if (typeof myNum === "number" && !Number.isNaN(myNum)) {
total += myNum;
}
}
return total;
}

module.exports = sum;
24 changes: 18 additions & 6 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,40 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical
const sum = require("./sum.js");

// Acceptance Criteria:

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

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

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

test("given an array with negative numbers, returns the correct total sum", () => {
expect(sum([-25, -100, 75])).toBe(-50);
});
// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum

test("given an array with decimal/float numbers, returns the correct total sum", () => {
expect(sum([1.5, 2.5, 3.1])).toBe(7.1);
});
// 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("given an array with non-number values, returns the sum of the numerical elements", () => {
expect(sum(["yoyo", 32, 10, "aha"])).toBe(42);
});
// 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("given an array with only non-numeric values, returns 0", () => {
expect(sum(["£", "&", null, undefined, "abc"])).toBe(0);
});
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 (const element of list) {
if (element === target) {
return true;
}
Expand Down
Loading