Skip to content
39 changes: 36 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,42 @@
// 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;
}

for (let i = 0; i < numbers.length; i++) {
for (let j = i + 1; j < numbers.length; j++) {
if (numbers[i] > numbers[j]) {
const temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}

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


if (numbers.length % 2 === 0) {

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

return numbers[middleIndex];
}
}


module.exports = calculateMedian;
16 changes: 15 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
function dedupe() {}
function dedupe(elements) {
const unique = [];

for (let i = 0; i < elements.length; i++) {
const current = elements[i];

if (!unique.includes(current)) {
unique.push(current);
}
}

return unique;
}

module.exports = dedupe;
31 changes: 29 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,39 @@ 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 currentOutput = dedupe([]);
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 duplicates, it returns an the same array", () => {
const input = [1,4,6,"d","a","x","e",0,7,8];
const currentOutput = dedupe(input);
const targetOutput = [1,4,6,"d","a","x","e",0,7,8];

expect(currentOutput).toEqual(targetOutput);
});

test("The copy must not be the very same array in memory", () => {
const input = [3, 1, 2];
const currentOutput = dedupe(input);

expect(currentOutput).toEqual([3, 1, 2]);
expect(currentOutput).not.toBe(input); // make sure the function returns a copy
});
// 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
// Then it should remove the duplicate values, preserving the first occurrence of each element

test("Given an array with strings or numbers, it returns an the array whit no duplicated elements", () => {
const currentOutput = dedupe(['a','a','a','b','b','c']);
const targetOutput = ['a','b','c'];

expect(currentOutput).toEqual(targetOutput);
});
18 changes: 18 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
function findMax(elements) {
if (elements.length === 0) {
return -Infinity;
}

let max = -Infinity;

for (let i = 0; i < elements.length; i++) {
let current = elements[i];

// using Number.isFinite keeps NaN or Infinity out of the comparison
if (typeof current === "number" && Number.isFinite(current)) {
if (current > max) {
max = current;
}
}
}

return max;
}

module.exports = findMax;
49 changes: 48 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,75 @@ 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 currentOutput = findMax([]);
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 currentOutput = findMax([42]);
const targetOutput = 42;

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 positive and negative numbers, returns the largest", () => {
const currentOutput = findMax([-25, 5, 20, -3, 15]);
const targetOutput = 20;

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 of only negative numbers, returns the closest to zero", () => {
const currentOutput = findMax([-50, -3, -20, -10]);
const targetOutput = -3;

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 of decimal numbers, returns the largest decimal", () => {
const currentOutput = findMax([1.1, 3.5, 2.9, 3.4]);
const targetOutput = 3.5;

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 non-number values, returns correct max (ignoring non-numbers)", () => {
const currentOutput = findMax([10, "hi", 50, true, 3]);
const targetOutput = 50;

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 currentOutput = findMax(["a", null, undefined, {}, [], true]);
const targetOutput = -Infinity;

expect(currentOutput).toEqual(targetOutput);
});
16 changes: 16 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
function sum(elements) {
// This variable will store the total sum
let total = 0;

// Go through each element in the array
for (let i = 0; i < elements.length; i++) {
let current = elements[i]; // get the current element

// Only add it if it's a number
// Number.isFinite skips NaN and Infinity, so only real numbers add to total
if (typeof current === "number" && Number.isFinite(current)) {
total = total + current; // add to total
}
}

// Return the sum of all numbers
return total;
}

module.exports = sum;
42 changes: 41 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,64 @@ 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 currentOutput = sum([]);
const targetPitPut= 0;

expect(currentOutput).toEqual(targetPitPut);
})


// Given an array with just one number
// When passed to the sum function
// Then it should return that number
test("given an array whit only 1 number, returns same input",()=>{
const currentOutput = sum([4]);
const targetPitPut= 4;

expect(currentOutput).toEqual(targetPitPut);
})


// 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 whit a negative number, returns correct total (subtract the negative number)",()=>{
const currentOutput = sum([1,2,3,4,-5,]);
const targetPitPut= 5;

expect(currentOutput).toEqual(targetPitPut);
})


// 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 whit a decimal/float number, returns correct total (decimal/float number)",()=>{
const currentOutput = sum([1,2,3,4,3.5,]);
const targetPitPut= 13.5;

expect(currentOutput).toBeCloseTo(targetPitPut); // allow tiny floating point differences
})


// 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 whit a non-number values, returns correct total (ignore the NaN and sum the others)",()=>{
const currentOutput = sum([1,2,3,4,"hi",5]);
const targetPitPut= 15;

expect(currentOutput).toEqual(targetPitPut);
})

// 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 (0)", () => {
const currentOutput = sum(["hello", null, undefined, {}, [], true, NaN]);
const targetOutput = 0;

expect(currentOutput).toEqual(targetOutput);
});
7 changes: 3 additions & 4 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// 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;
return true;
}
}
return false;
return false;
}

module.exports = includes;