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

WM5 | Sariat Adenike | Module-JS2 | Week-1 #77

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
Next Next commit
Wrote test to implement the function
  • Loading branch information
sariat0001 committed Nov 9, 2023
commit 6a436e88d270f455a2c9f02931335b26d07c162e
6 changes: 4 additions & 2 deletions week-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

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

module.exports = calculateMedian;
29 changes: 28 additions & 1 deletion week-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ E.g. max(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-nume
// Given an empty array
// When passed to the max function
// Then it should return -Infinity
test.todo("given an empty array, returns -Infinity");
// test.todo("given an empty array, returns -Infinity");

// Given an array with one number
// When passed to the max function
Expand All @@ -26,3 +26,30 @@ 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 {numSort,max} = require("./max");

test("given an empty array, returns -Infinity", () => {
let maximum = [];
expect(max(maximum)).toBe("-Infinity");
});

test("given an array with one number, returns that number", () => {
const maximum = [5];
expect(max(maximum)).toBe(5);
});

test("given an array with both positive and negative numbers, returns the largest number overall", () => {
const maximum = [3, 0, 5, -1, 7, -8];
expect(max(maximum)).toBe(7);
});

test("given an array with decimal numbers, returns the largest decimal number", () => {
const maximum = [3.7, 1.5, 0.5, 7.5, 5.2, 4.1];
expect(max(maximum)).toBe(7.5);
});

test("given an array with non-number values, return the max and ignore non-numeric values", () => {
const maximum = [9, 3.7, 10, 0, -4, 2, "t"];
expect(max(maximum)).toBe(10);
});