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
27 changes: 24 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,31 @@
// Hint: Please consider scenarios when 'list' doesn't have numbers (the function is expected to return null)
// 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;
//}

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 nums = list.filter(x => typeof x ==="number");

if(nums.length ===0) return null;

nums.sort((a, b) => a - b);

const mid = Math.floor(nums.length / 2);

if (nums.length % 2 ===1){

return nums[mid];
}
else{
return (nums[mid - 1] + nums[mid]) / 2;
}
}

module.exports = calculateMedian;
70 changes: 35 additions & 35 deletions Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,44 @@
const calculateMedian = require("./median.js");

describe("calculateMedian", () => {
[
{ input: [1, 2, 3], expected: 2 },
{ input: [1, 2, 3, 4, 5], expected: 3 },
{ input: [1, 2, 3, 4], expected: 2.5 },
{ input: [1, 2, 3, 4, 5, 6], expected: 3.5 },
].forEach(({ input, expected }) =>
it(`returns the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
);
it("returns median for sorted arrays", () =>{
expect(calculateMedian([1, 2, 3])).toBe(2);
expect(calculateMedian([1, 2, 3, 4, 5])).toBe(3);
expect(calculateMedian([1, 2, 3, 4])).toBe(2.5);
expect(calculateMedian([1, 2, 3, 4, 5, 6])).toBe(3.5);
});

[
{ input: [3, 1, 2], expected: 2 },
{ input: [5, 1, 3, 4, 2], expected: 3 },
{ input: [4, 2, 1, 3], expected: 2.5 },
{ input: [6, 1, 5, 3, 2, 4], expected: 3.5 },
{ input: [110, 20, 0], expected: 20 },
{ input: [6, -2, 2, 12, 14], expected: 6 },
].forEach(({ input, expected }) =>
it(`returns the correct median for unsorted array [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
);
it("returns median for unsorted arrays", () =>{
expect(calculateMedian([3, 1, 2])).toBe(2);
expect(calculateMedian([5, 1, 3, 4, 2])).toBe(3);
expect(calculateMedian([4, 2, 1, 3])).toBe(2.5);
expect(calculateMedian([6, 1, 5, 3, 2, 4])).toBe(3.5);
expect(calculateMedian([110, 20, 0])).toBe(20);
expect(calculateMedian([6, -2, 2, 12, 14])).toBe(6);
});

it("doesn't modify the input array [3, 1, 2]", () => {
const list = [3, 1, 2];
calculateMedian(list);
expect(list).toEqual([3, 1, 2]);
it("does not modify the original array", () => {
const arr = [3, 1, 2];
calculateMedian(arr);
expect(arr).toEqual([3, 1, 2]);
});

[ 'not an array', 123, null, undefined, {}, [], ["apple", null, undefined] ].forEach(val =>
it(`returns null for non-numeric array (${val})`, () => expect(calculateMedian(val)).toBe(null))
);
it("returns null for invalid or empty arrays", () =>{
expect(calculateMedian("not an array")).toBe(null);
expect(calculateMedian(123)).toBe(null);
expect(calculateMedian(null)).toBe(null);
expect(calculateMedian(undefined)).toBe(null);
expect(calculateMedian({})).toBe(null);
expect(calculateMedian([])).toBe(null);
expect(calculateMedian(["apple", null, undefined])).toBe(null);
});

[
{ input: [1, 2, "3", null, undefined, 4], expected: 2 },
{ input: ["apple", 1, 2, 3, "banana", 4], expected: 2.5 },
{ input: [1, "2", 3, "4", 5], expected: 3 },
{ input: [1, "apple", 2, null, 3, undefined, 4], expected: 2.5 },
{ input: [3, "apple", 1, null, 2, undefined, 4], expected: 2.5 },
{ input: ["banana", 5, 3, "apple", 1, 4, 2], expected: 3 },
].forEach(({ input, expected }) =>
it(`filters out non-numeric values and calculates the median for [${input}]`, () => expect(calculateMedian(input)).toEqual(expected))
);
it("filters out non-numbers and returns correct median", () =>{
expect(calculateMedian([1, 2, "3", null, undefined, 4])).toBe(2);
expect(calculateMedian(["apple", 1, 2, 3, "banana", 4])).toBe(2.5);
expect(calculateMedian([1, "2", 3, "4", 5])).toBe(3);
expect(calculateMedian([1, "apple", 2, null, 3, undefined, 4])).toBe(2.5);
expect(calculateMedian([3, "apple", 1, null, 2, undefined, 4])).toBe(2.5);
expect(calculateMedian(["banana", 5, 3, "apple", 1, 4, 2])).toBe(3);
});
});
19 changes: 18 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
function dedupe() {}
function dedupe(arr){

if (!Array.isArray(arr) || arr.length === 0) return [];

const result = [];

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

if (!result.includes(arr[i])){

result.push(arr[i]);
}
}

return result;

}
module.exports = dedupe;
22 changes: 21 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,32 @@ 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");

describe("dedupe function", () =>{
it("returns an empty array for an empty array", () =>{

expect(dedupe([])).toEqual([]);

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

it("returns the same array if there are no duplicates", () =>{
expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]);

expect(dedupe(["a", "b", "c"])).toEqual(["a", "b", "c"]);

});
// 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

it("removes duplicates and preserves first occurrence", () =>{

expect(dedupe([1, 2, 1])).toEqual([1, 2]);
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
expect(dedupe(["a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]);

});
});
17 changes: 16 additions & 1 deletion Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
function findMax(elements) {
}
if (!Array.isArray(elements)) return -Infinity;

const numbers = elements.filter(x => typeof x ==="number");
if (numbers.length === 0) return -Infinity;

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

return max;
}

console.log(findMax([10, 5, 30, 2]));
console.log(findMax(['a', 10, 5]));
console.log(findMax([]));

module.exports = findMax;
28 changes: 26 additions & 2 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,52 @@ 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");

describe("findMax function", () => {
// empty array
it("returns -Infinity for an empty array", () => {
expect(findMax([])).toBe(-Infinity);
});
// Given an array with one number
// When passed to the max function
// Then it should return that number

it("returns that number for a single-element array", () => {
expect(findMax([42])).toBe(42);
});
// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall

it("returns the largest number overall", () =>{
expect(findMax([-1, 0, 5, -10, 3])).toBe(5);
});
// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero

it("returns the closest to zero for all negative numbers", () =>{
expect(findMax([-10, -3, -7])).toBe(-3);
});
// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number

it("returns the largest decimal number", () => {
expect(findMax([1.1, 2.2, 1.5])).toBe(2.2);
});
// Given an array with non-number values
// When passed to the max function
// Then it should return the max and ignore non-numeric values

it("ignores non-numbers and returns the max", () => {
expect(findMax([1, "a", 5, null, 2])).toBe(5);
});
// 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

it("returns -Infinity if no numbers are present", () => {
expect(findMax(["a", null, "b"])).toBe(-Infinity);
});

});
22 changes: 20 additions & 2 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
function sum(elements) {
}
function sum(elements){

if (!Array.isArray(elements) || elements.length ===0) return 0;

const numbers = elements.filter(x => typeof x === "number");

let total =0;

for (let i =0; i< numbers.length; i++){

total += numbers[i];

}

return total;
}

console.log(sum([10, 20, 30]));
console.log(sum([1, "a", 2, null, 3]));
console.log(sum([]));

module.exports = sum;
45 changes: 44 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,67 @@ 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")

describe("sum function", ()=>{
it("returns 0 for an empty array", () =>{

expect(sum([])).toBe(0);

});

// Given an array with just one number
// When passed to the sum function
// Then it should return that number

it("returns that number for a single-element array", ()=>{

expect(sum([42])).toBe(42);

});

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum

it("sums negative numbers correctly", ()=>{

expect(sum([-1, -2, -3])).toBe(-6);

expect(sum([5, -3, 2])).toBe(4);

});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum

it("sums decimal numbers correctly", () =>{

expect(sum([1.5, 2.5, 3])).toBe(7);

expect(sum([0.1, 0.2, 0.3])).toBeCloseTo(0.6);

});

// 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

it("ignores non-numbers and sums only numerical elements", () =>{

expect(sum(["hi", 10, "hey", 20])).toBe(30);

expect(sum([1, "apple", 2, null, 3])).toBe(6);

});

// 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

it("returns 0 if there are no numerical elements", () =>{

expect(sum(["apple", null, undefined, "banana"])).toBe(0);

});
});
9 changes: 5 additions & 4 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// 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) {
function includes(list, target){

for (const element of list){

if (element === target){
return true;
}
}
Expand Down