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
27 changes: 24 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,30 @@
// 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;

const numbers = [];
for (let i = 0; i < list.length; i++) {
if (typeof list[i] === "number") {
numbers.push(list[i]);
}
}

if (numbers.length === 0) {
return null;
}

numbers.sort(function (a, b) {
return a - b;
});

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

if (numbers.length % 2 !== 0) {
return numbers[middleIndex];
}

return (numbers[middleIndex - 1] + numbers[middleIndex]) / 2;
}

module.exports = calculateMedian;
13 changes: 12 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
function dedupe() {}
function dedupe(arr) {
if (!Array.isArray(arr)) return [];
const result = [];
for (let i = 0; i < arr.length; i++) {
if (!result.includes(arr[i])) {
result.push(arr[i]);
}
}
return result;
}

module.exports = dedupe;
49 changes: 36 additions & 13 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const dedupe = require("./dedupe.js");
/*

describe("dedupe", () => {
/*

Dedupe Array

📖 Dedupe means **deduplicate**
Expand All @@ -11,18 +14,38 @@ E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) returns [5, 1, 2, 3, 8]
E.g. dedupe([1, 2, 1]) returns [1, 2]
*/

// Acceptance Criteria:
// Acceptance Criteria:

// Given an empty array
// When passed to the dedupe function
// Then it should return an empty array
test("given an empty array, it returns an empty array", () => {
const input = [];
const output = dedupe(input);
expect(output).toEqual([]);
});
// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
test("array with no duplicates returns a copy", () => {
const input = [1, 2, 3];
const output = dedupe(input);
expect(output).toEqual([1, 2, 3]);
expect(output).not.toBe(input);
});

// 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");
// 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.
test("removes duplicates while preserving first occurrence", () => {
const input1 = ["a", "a", "a", "b", "b", "c"];
expect(dedupe(input1)).toEqual(["a", "b", "c"]);

// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
const input2 = [5, 1, 1, 2, 3, 2, 5, 8];
expect(dedupe(input2)).toEqual([5, 1, 2, 3, 8]);

// 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.
const input3 = [1, 2, 1];
expect(dedupe(input3)).toEqual([1, 2]);
});
});
13 changes: 13 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
function findMax(elements) {
if (!Array.isArray(elements)) return null;

let maxValue = -Infinity;

for (let i = 0; i < elements.length; i++) {
if (typeof elements[i] === "number" && isFinite(elements[i])) {
if (elements[i] > maxValue) {
maxValue = elements[i];
}
}
}

return maxValue;
}

module.exports = findMax;
79 changes: 49 additions & 30 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,52 @@ We have set things up already so that this file can see your function from the o
*/

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");

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

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

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

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

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

// 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("findMax", () => {
// 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("given an empty array, returns -Infinity", () => {
expect(findMax([])).toBe(-Infinity);
});

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

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall
test("array with positive and negative numbers returns the largest number", () => {
expect(findMax([-10, 5, 3, -2])).toBe(5);
});

// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero
test("array with only negative numbers returns the closest to zero", () => {
expect(findMax([-10, -3, -7])).toBe(-3);
});
// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
test("array with decimal numbers returns the largest decimal", () => {
expect(findMax([1.5, 2.7, 2.6, 0.9])).toBe(2.7);
});
// 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("array with non-number values ignores them and returns max of numbers", () => {
expect(findMax([10, "a", 5, null, 7])).toBe(10);
});

// 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("array with only non-number values returns -Infinity", () => {
expect(findMax(["a", null, "b"])).toBe(-Infinity);
});
});
11 changes: 11 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
function sum(elements) {
if (!Array.isArray(elements)) return 0;

let total = 0;

for (let i = 0; i < elements.length; i++) {
if (typeof elements[i] === "number" && isFinite(elements[i])) {
total += elements[i];
}
}

return total;
}

module.exports = sum;
70 changes: 44 additions & 26 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,47 @@ 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")

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

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

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

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

// 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", () => {
// Acceptance Criteria:

// Given an empty array
// When passed to the sum function
// Then it should return 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("array with one number returns that number", () => {
expect(sum([5])).toBe(5);
});

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
test("array with negative numbers returns correct sum", () => {
expect(sum([-2, -3, 5])).toBe(0);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test("array with decimal numbers returns correct sum", () => {
expect(sum([1.5, 2.5, 1])).toBe(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("ignores non-number values and sums numbers only", () => {
expect(sum([10, "a", 5, null, 7])).toBe(22);
});

// 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 array has only non-number values", () => {
expect(sum(["a", null, "b"])).toBe(0);
});
});
Loading