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
23 changes: 19 additions & 4 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,24 @@
// 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)|| list.length === 0) {
return null;
}

let onlyArrOfNumbers = list.filter(el => typeof el === "number");

if (onlyArrOfNumbers.length === 0) {
return null;
}

let sortedArr = onlyArrOfNumbers.sort((a,b) => a - b);

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

if (sortedArr.length % 2 === 1) {
return sortedArr[middleIndex];
} else {
return (sortedArr[middleIndex - 1] + sortedArr[middleIndex]) / 2;
}
}
module.exports = calculateMedian;
13 changes: 12 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
function dedupe() {}
// start with the function;
function dedupe(arr) {
if (arr.length === 0) return [];
let cleanArr = [];
for (let item of arr) {
if (!cleanArr.includes(item)) {
cleanArr.push(item);
}
}
return cleanArr;
}
module.exports = dedupe;
15 changes: 13 additions & 2 deletions Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const dedupe = require("./dedupe.js");
Dedupe Array
📖 Dedupe means **deduplicate**
In this kata, you will need to deduplicate the elements of an array
E.g. dedupe(['a','a','a','b','b','c']) target output: ['a','b','c']
Expand All @@ -16,12 +16,23 @@ 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("for empty array it should return an empty array", () => {
const arr = [];
expect(dedupe(arr)).toEqual([]);
});

// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
test("for array with no duplicates should return the original array", () => {
const arr = [1, 2, 3, 4];
expect(dedupe(arr)).toEqual(arr);
});

// 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("removes duplicates and keeps first occurrence", () => {
const arr = [1, 2, 1, 3, 4];
expect(dedupe(arr)).toEqual([1, 2, 3, 4]);
});
5 changes: 5 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
// start your function;
function findMax(elements) {
if (elements.length === 0) return -Infinity;
let onlyNumArr = elements.filter((el) => typeof el === "number");
let maxNum = Math.max(...onlyNumArr);
return maxNum;
}

module.exports = findMax;
29 changes: 28 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,55 @@ 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("if given empty array it should return Infinity", () => {
const elements = [];
expect(findMax(elements)).toEqual(-Infinity);
});

// Given an array with one number
// When passed to the max function
// Then it should return that number
test("if given 1 number, it should return that number", () => {
const elements = [8];
expect(findMax(elements)).toEqual(8);
});

// 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 positive and negative value, should return largest number overall", () => {
const elements = [1, 3, 4, -3, -5];
expect(findMax(elements)).toEqual(4);
});

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

// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
test("if given decimal number, should return largest decimal number", () => {
const elements = [1.2, 5.5, 9.1];
expect(findMax(elements)).toEqual(9.1);
});

// 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("if given non-number values it should return only max number", () => {
const elements = [2, 5, 7, "hi", "apple", 9];
expect(findMax(elements)).toEqual(9);
});

// 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("if given only non-number values it should return -Infinity ", () => {
const elements = ["a", "n", "hi"];
expect(findMax(elements)).toEqual(-Infinity);
});
8 changes: 8 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
function sum(elements) {
if (elements.length === 0) return 0;
let numberElement = elements.filter((el) => typeof el === "number");
if (numberElement.length === 0) return Infinity;
let total = 0;
for (let num of numberElement) {
total += num;
}
return Math.floor(total);
}

module.exports = sum;
25 changes: 24 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,47 @@ 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("Empty array should be returned Zero", () => {
let elements = [];
expect(sum(elements)).toEqual(0);
});

// Given an array with just one number
// When passed to the sum function
// Then it should return that number
test("given one number should be returned same as original", () => {
let elements = [5];
expect(sum(elements)).toEqual(5);
});

// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
test("negative number should return total with no effect", () => {
let elements = [1, 3, -2];
expect(sum(elements)).toEqual(2);
});

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test("decimal number should return the correct total", () => {
let elements = [1, 2.3, 4.5, 6];
expect(sum(elements)).toEqual(13);
});

// 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("non-number values be ignored, function still should return total", () => {
let elements = [1, 2, 3, "hi", "b"];
expect(sum(elements)).toEqual(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
test("non-number values should return Infinity", () => {
let elements = ["a", "d", "hi"];
expect(sum(elements)).toEqual(Infinity);
});
6 changes: 6 additions & 0 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
// 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) {
return true;
}
} */
for (let index of list) {
if (index === target) {
return true;
}
}
return false;
}
Expand Down
Loading
Loading