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
17 changes: 13 additions & 4 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@
// 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 number = list.filter(n => typeof n === "number");
if(number.length === 0) {return null;}
number.sort((a,b) => a-b)
const middleIndex = Math.floor(number.length / 2);


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

module.exports = calculateMedian;


module.exports = calculateMedian
6 changes: 5 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
function dedupe() {}
function dedupe(list) {
return [...new Set(list)];
}

module.exports = dedupe;
20 changes: 18 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,28 @@ E.g. dedupe([1, 2, 1]) target output: [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");
test("given an empty array, it returns an empty array", () => {
const array = [];
const currentOutput = dedupe(array);
const targetOutput = [];
expect(currentOutput).toEqual(targetOutput);
});

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

test("given an array with no duplication, it copy of the original array ", () => {
const array = [1, 2, 3];
const currentOutput = dedupe(array);
const targetOutput = [1, 2, 3];
expect(currentOutput).toEqual(targetOutput);
});
// 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("given an array with string , it removes duplication ", () => {
const array = ["a", "a", 1, 1, "ba", 4];
const currentOutput = dedupe(array);
const targetOutput = ["a", 1, "ba", 4];
expect(currentOutput).toEqual(targetOutput);
});
7 changes: 7 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
function findMax(elements) {
let maximumNumber = -Infinity;
for (const item of elements) {
if (typeof item === "number" && item > maximumNumber) {
maximumNumber = item;
}
}
return maximumNumber;
}

module.exports = findMax;
47 changes: 42 additions & 5 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,65 @@ 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", () => {
const maxElement = [];
const currentOutput = findMax(maxElement);
const targetOutput = -Infinity;
expect(currentOutput).toEqual(targetOutput);
});

// 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", () => {
const maxElement = [1];
const currentOutput = findMax(maxElement);
const targetOutput = 1;
expect(currentOutput).toEqual(targetOutput);
});

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

test("given an array with one both positive and negative numbers , return largest number", () => {
const maxElement = [2, -4, 8, -1];
const currentOutput = findMax(maxElement);
const targetOutput = 8;
expect(currentOutput).toEqual(targetOutput);
});
// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero

test("given an array with negative numbers, returns the closest to zero", () => {
const maxElement = [-1, -4, -12];
const currentOutput = findMax(maxElement);
const targetOutput = -1;
expect(currentOutput).toEqual(targetOutput);
});
// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number

test("given an array with decimal numbers returns the largest decimal number", () => {
const maxElement = [2.5, 4.5, 9.8];
const currentOutput = findMax(maxElement);
const targetOutput = 9.8;
expect(currentOutput).toEqual(targetOutput);
});
// 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("given an array with one non-number return the max and ignore non-numeric value, ", () => {
const maxElement = ["hello", 4, 8, 10];
const currentOutput = findMax(maxElement);
const targetOutput = 10;
expect(currentOutput).toEqual(targetOutput);
});
// 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("given an array with only non-number values returns -Infinity", () => {
const maxElement = ["history", "nation"];
const currentOutput = findMax(maxElement);
const targetOutput = -Infinity;
expect(currentOutput).toEqual(targetOutput);
});
7 changes: 7 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
function sum(elements) {
let total = 0;
for (const element of elements) {
if (typeof element === "number") {
total += element;
}
}
return total;
}

module.exports = sum;
37 changes: 36 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,59 @@ 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", () => {
const list = [];
const currentOutput = sum(list);
const targetOutput = 0;
expect(currentOutput).toEqual(targetOutput);
});

// 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", () => {
const list = [20];
const currentOutput = sum(list);
const targetOutput = 20;
expect(currentOutput).toEqual(targetOutput);
});

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
test("given an array containing negative numbers[-20, 30, -50] , returns the correct total sum ", () => {
const list = [-20, 30, -50];
const currentOutput = sum(list);
const targetOutput = -40;
expect(currentOutput).toEqual(targetOutput);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test("given an array with decimal/float numbers [1.5, -2.4, 4.5], returns the correct total sum", () => {
const list = [1.5, -2.4, 4.5];
const currentOutput = sum(list);
const targetOutput = 3.6;
expect(currentOutput).toEqual(targetOutput);
});

// 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("given an array containing non-numerical value, ignore none-numerical value, returns the sum of numerical elements", () => {
const list = [-20, "hello", 30, "a"];
const currentOutput = sum(list);
const targetOutput = 10;
expect(currentOutput).toEqual(targetOutput);
});

// 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("given an array with only non-number values, returns the least surprising value based on the behavior of other inputs", () => {
const list = ["a", "b", null, undefined];
const currentOutput = sum(list);
const targetOutput = 0;
expect(currentOutput).toEqual(targetOutput);
});
3 changes: 1 addition & 2 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// 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];
for (const element of list) {
if (element === target) {
return true;
}
Expand Down