Skip to content
Closed
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
12 changes: 9 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
// If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
const arrLength = list.length;
const middleIndex = Math.floor(arrLength / 2);
if (arrLength % 2 === 0) {
const num2 = list[middleIndex - 1];
const num1 = list[middleIndex];
return (num1 + num2) / 2;
} else {
return list[middleIndex];
}
}

module.exports = calculateMedian;
1 change: 1 addition & 0 deletions Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe("calculateMedian", () => {
test("returns the average of middle values for even length array", () => {
expect(calculateMedian([1, 2, 3, 4])).toEqual(2.5);
expect(calculateMedian([1, 2, 3, 4, 5, 6])).toEqual(3.5);
expect(calculateMedian([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])).toEqual(5.5);
});

test("doesn't modify the input", () => {
Expand Down
30 changes: 29 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
function dedupe() {}
// function dedupe(arr) {
// const uniqueArray = [];

// for (let i = 0; i < arr.length; i++) {
// let isDuplicate = false;

// // iterates over uniqueArray and checks if arr[i] is in that array
// for (let j = 0; j < uniqueArray.length; j++) {
// if (arr[i] === uniqueArray[j]) {
// isDuplicate = true;
// }
// }

// // if arr[i] is not already in uniqueArray, add it to uniqueArray
// if (isDuplicate === false) {
// uniqueArray.push(arr[i]);
// }
// }

// return uniqueArray;
// }
// Refactored code using SET
function dedupe(arr) {
return [...new Set(arr)]; // Convert Set back to an array
}

console.log(dedupe([1, 2, 3, 3, 4, 4, 5, 8, 8]));

module.exports = dedupe;
22 changes: 22 additions & 0 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,29 @@ test.todo("given an empty array, it returns an empty array");
// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
test.todo("given an array with no duplicates, it returns original array");

// Given an array with strings or numbers
// When passed to the dedupe function
// Then it should remove the duplicate values, preserving the first occurence of each element
test.todo(
"given an array with duplicates, removes duplicate preserving the first occurence of each element"
);

describe("dedupe", () => {
test("given an empty array, it returns an empty array", () => {
expect(dedupe([])).toEqual([]);
});

test("given an array with no duplicates, it returns original array", () => {
expect(dedupe([1, 2, 3, 4])).toEqual([1, 2, 3, 4]);
expect(dedupe(["c", "a", "t"])).toEqual(["c", "a", "t"]);
expect(dedupe([1, 2, 11])).toEqual([1, 2, 11]);
});

test("removes duplicates from an array", () => {
expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]);
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
});
});
24 changes: 24 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
// function findMax(elements) {
// if (elements.length === 0) {
// return -Infinity;
// }

// let max = -Infinity;

// for (let i = 0; i < elements.length; i++) {
// if (typeof elements[i] === "number" && !isNaN(elements[i])) {
// if (elements[i] > max) {
// max = elements[i];
// }
// }
// }

// return max;
// }

//Refactored code using Math.max
function findMax(elements) {
const validNumbers = elements.filter(
(num) => typeof num === "number" && !isNaN(num)
);

return validNumbers.length ? Math.max(...validNumbers) : -Infinity;
}

module.exports = findMax;
54 changes: 52 additions & 2 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,73 @@ 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
test.todo("given an array with one number, returns 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
test.todo(
"given an array with positive and negative number, returns 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
test.todo(
"given an array with negative number, returns the largest number which is closest to 0"
);

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

test.todo(
"given an array with decimal numbers, returns 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

test.todo(
"given an array with non-number values, returns 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
test.todo(
"given an array with non-number values, returns the least surprising value"
);

describe("findMax function", () => {
test("given an empty array, returns -Infinity", () => {
expect(findMax([])).toEqual(-Infinity);
});

test("given an array with one number, returns that number", () => {
expect(findMax([5])).toEqual(5);
expect(findMax([-10])).toEqual(-10);
});

test("given an array with positive and negative numbers, returns the largest number overall", () => {
expect(findMax([-1, 2, 3, -10])).toEqual(3);
expect(findMax([-100, 0, 50, -200])).toEqual(50);
});

test("given an array with negative numbers, returns the largest number which is closest to 0", () => {
expect(findMax([-5, -2, -8, -10])).toEqual(-2);
expect(findMax([-100, -50, -1, -20])).toEqual(-1);
});

test("given an array with decimal numbers, returns the largest decimal number", () => {
expect(findMax([1.5, 2.7, 3.9, 3.8])).toEqual(3.9);
expect(findMax([-1.1, -0.5, -0.9])).toEqual(-0.5);
});

test("given an array with non-number values, returns the max and ignores non-numeric values", () => {
expect(findMax([5, "a", null, 10, undefined, 3])).toEqual(10);
expect(findMax(["hello", 2, {}, 7, NaN, 1])).toEqual(7);
});

test("given an array with only non-number values, returns the least surprising value", () => {
expect(findMax(["a", {}, null, undefined])).toEqual(-Infinity);
expect(findMax([NaN, "hello", {}])).toEqual(-Infinity);
});
});
10 changes: 9 additions & 1 deletion 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++) {
if (typeof elements[i] === "number" && !isNaN(elements[i])) {
total += elements[i];
}
}
// return total;
return Number(total.toFixed(2)); // to avoid getting lot of decimal points for sum of floating number
}
module.exports = sum;
43 changes: 42 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,65 @@ 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.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
test.todo("given an array with just one number, return that number");

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
test.todo("given an array with negative numbers, return correct total sum");

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test.todo(
"given an array with decimal/float numbers, return 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
test.todo(
"given an array containing non-number values, ignore non numeric and return correct total sum for 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
test.todo(
"given an array with only number values, return the least surprising value given how it behaves for all other inputs"
);

describe("sum", () => {
test("given an empty array, returns 0", () => {
expect(sum([])).toEqual(0);
});

test("given an array with just one number, return that number", () => {
expect(sum([5])).toEqual(5);
expect(sum([-3])).toEqual(-3);
});

test("given an array with negative numbers, return correct total sum", () => {
expect(sum([-1, -2, -3])).toEqual(-6);
expect(sum([-10, 5, -5])).toEqual(-10);
});

test("given an array with decimal/float numbers, return correct total sum", () => {
expect(sum([1.5, 2.5, 3.5])).toEqual(7.5);
expect(sum([-1.1, -2.2, 3.3])).toBeCloseTo(0); //using to be close to to fix issue with unpredicted decimal points
});

test("given an array containing non-number values, ignore non numeric and return correct total sum for numerical elements", () => {
expect(sum([1, "a", 2, null, 3])).toEqual(6);
expect(sum([true, false, 4, "5", 6])).toEqual(10);
});

test("given an array with only non-number values, return the least surprising value given how it behaves for all other inputs", () => {
expect(sum(["a", true, null, undefined])).toEqual(0);
});
});
15 changes: 13 additions & 2 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
// 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;
// }

//Refactored code

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