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
22 changes: 19 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,25 @@
// 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;
} // If list is NOT an array, return null.

const numbers = list.filter((item) => typeof item === "number");
// Keep the item only if its type is number.
if (numbers.length === 0) {
return null;
}

numbers.sort((a, b) => a - b); // This sorts numbers from smallest to largest

const middleIndex = Math.floor(numbers.length / 2);

if (numbers.length % 2 === 0) {
return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2;
}

return numbers[middleIndex];
}

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(array) {
const newArray = [];

for (let i = 0; i < array.length; i++) {
if (!newArray.includes(array[i])) {
newArray.push(array[i]);
}
}

return newArray;
}

module.exports = dedupe;
35 changes: 33 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,44 @@ 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");

// Empty array
test("should return an empty array when given an empty array", () => {
const array = [];
const result = dedupe(array);

expect(result).toEqual([]);
});

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

// No duplicates
test("should return a copy when the array has no duplicates", () => {
const array = [1, 2, 3];
const result = dedupe(array);

expect(result).toEqual([1, 2, 3]);
});

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

// Remove duplicate strings
test("should remove duplicate strings and keep first occurrence", () => {
const array = ["a", "a", "a", "b", "b", "c"];
const result = dedupe(array);

expect(result).toEqual(["a", "b", "c"]);
});

// Remove duplicate numbers
test("should remove duplicate numbers and keep first occurrence", () => {
const array = [5, 1, 1, 2, 3, 2, 5, 8];
const result = dedupe(array);

expect(result).toEqual([5, 1, 2, 3, 8]);
});
15 changes: 15 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
function findMax(elements) {
let max = -Infinity; // Start with the smallest possible value.

// loop
for (let i = 0; i < elements.length; i++) {
if (typeof elements[i] === "number") {
// Check if the current element is a number and ignores strings, objects, null

if (elements[i] > max) {
// If it is bigger, update max with this new bigger number
max = elements[i];
}
}
}
return max;
}

// Export the function so it can be used in the test file
module.exports = findMax;
28 changes: 27 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,54 @@ 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, returns -Infinity", () => {
expect(findMax([])).toEqual(-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, returns that 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("returns the largest number from positive and negative numbers", () => {
expect(findMax([-5, 10, -2, 7])).toEqual(10);
});

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

test("returns the closest number to zero when all numbers are negative", () => {
expect(findMax([-10, -3, -20])).toEqual(-3);
});

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

test("returns the largest decimal number", () => {
expect(findMax([1.2, 5.8, 3.4])).toEqual(5.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("ignores non-number values", () => {
expect(findMax(["hey", 10, "hi", 60, 10])).toEqual(60);
});

// 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("returns -Infinity when there are no numbers", () => {
expect(findMax(["banana", "orange"])).toEqual(-Infinity);
});
8 changes: 8 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
function sum(elements) {
let total = 0;
for (let i = 0; i < elements.length; i++) {
// Only add numbers and ignore other data types
if (typeof elements[i] === "number") {
total = total + elements[i];
}
}
return total;
}

module.exports = sum;
24 changes: 23 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,46 @@ 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("given an empty array, returns 0", () => {
expect(sum([])).toEqual(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 one number, returns that 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("returns the correct total with negative numbers", () => {
expect(sum([-5, 10, -2])).toEqual(3);
});

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

test("returns the correct total with decimal numbers", () => {
expect(sum([1.5, 2.5])).toEqual(4);
});

// 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("ignores non-number values and sums numbers only", () => {
expect(sum(["hey", 10, "hi", 60, 10])).toEqual(80);
});

// 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("returns 0 when there are no numbers", () => {
expect(sum(["hello", "world"])).toEqual(0);
});
12 changes: 11 additions & 1 deletion Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
// Refactor the implementation of includes to use a for...of loop

function includes(list, target) {
/*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 (const element of list) {
if (element === target) {
return true;
}
}

return false;
}

Expand Down
Loading