Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Migracode|Afere Precious Onome|Module js 2|Sprint 1 #236

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
17 changes: 15 additions & 2 deletions week-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,22 @@
// If you're in the week-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];
if (!Array.isArray(list) || list.length === 0) {
throw new Error("input must not be an empty array");
}
const sortList = [...list].sort((a, b) => a - b);
const middleNumber = Math.floor(sortList.length / 2);

if (sortList.length % 2 !== 0) {
return sortList[middleNumber];
}
const leftMiddleNum = sortList[middleNumber - 1];
const rightMiddleNum = sortList[middleNumber];
const median = (leftMiddleNum + rightMiddleNum) / 2;
return median;
}

module.exports = calculateMedian;

console.log(calculateMedian([1, 2, 3, 4]));
console.log(calculateMedian([1, 2, 3, 4, 5, 6]));
5 changes: 4 additions & 1 deletion week-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
function dedupe() {}
function dedupe(arr) {
return [...new Set(arr)];
}
module.exports = dedupe;
19 changes: 18 additions & 1 deletion week-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const dedupe = require("./dedupe.js");
/*
Dedupe Array

Expand All @@ -24,3 +23,21 @@ test.todo("given an empty array, it returns an empty array");
// Given an array with strings or numbers
// When passed to the dedupe function
// Then it should remove the duplicate values

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

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

test("given an array with strings or numbers, it removes the duplicate values", () => {
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]);
});
});
9 changes: 9 additions & 0 deletions week-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function max(array) {
const Numbers = array.filter((items) => typeof items === "number");

if (Numbers.length === 0) {
return -Infinity;
}
return Math.max(...Numbers);
}
module.exports = max;
19 changes: 19 additions & 0 deletions week-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,22 @@ test.todo("given an empty array, returns -Infinity");
// Given an array with non-number values
// When passed to the max function
// Then it should return the max and ignore non-numeric values

const max = require("./max");
describe("max function", () => {
test("given an empty array,returns -infinity", () => {
expect(max([])).toBe(-Infinity);
});
test("given an array with one number, returns that number", () => {
expect(max([6])).toBe(6);
});
test("given an array with both positive and negative number,returns the largest number", () => {
expect(max([1, 3, -7, 6, -8])).toBe(6);
});
test("given an array with decimal number,returns the largest decimal number", () => {
expect(max([1.3, 1.4, 1.2, 2.5])).toBe(2.5);
});
test("given an array with non-number values,returns the max and ignore non-numeric values", () => {
expect(max([1, 3, "hey", 5, "hello"])).toBe(5);
});
});
6 changes: 6 additions & 0 deletions week-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function sum(array) {
const sumOfNum = array.filter((items) => typeof items === "number");

return sumOfNum.reduce((acc, current) => acc + current, 0);
}
module.exports = sum;
17 changes: 17 additions & 0 deletions week-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,20 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical
// 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
const sum = require("./sum");
describe("sum function", () => {
test("given an empty array,return should be 0", () => {
expect(sum([])).toBe(0);
});
test("given an array with just one number,return should be that same number", () => {
expect(sum([6])).toBe(6);
});
test("given an array containing negative number,return the correct total", () => {
expect(sum([1, -2, 3, -4, 5])).toBe(3);
});
test("given an array containing non numerical number,return should ignore the non-numerical number and return the sum of the numerical number", () => {
expect(sum([1, "hello", 2, 3, "hi"])).toBe(6);
});
});
Loading