Skip to content
Open
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;
}

const onlyArrOfNumbers = list.filter((el) => typeof el === "number"); // to make the code safer better option is "const", as before I used "let".

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

const sortedArr = onlyArrOfNumbers.sort((a, b) => a - b); // as we are not reassigning it then better option 'const' instead of "let".

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 [];
const 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);
});
Comment on lines 26 to +30

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case should consider that the returned array is a copy of the original array. References is a relevant topic here: https://programming.codeyourfuture.io/data-groups/sprints/1/prep/#references

How would you check for a copy of the array in the test?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RomanSanaye what did you think of this idea?


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

Comment on lines +3 to 8

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could anything be a const here?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RomanSanaye did you have any thoughts on this?

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);
});
Comment on lines +67 to +70

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This choice feels appropriate - good work

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 0; // least surprising value;
let total = 0;
for (let num of numberElement) {
total += num;
}
return Number.isInteger.total ? total : Number(total.toFixed(2));
}
Comment on lines 1 to 10

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again - which variables here need to be a let and which can be made stricter with a const

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say this comment was not addressed

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RomanSanaye Can numberElement or total be made into const variables?


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.8);
});
Comment on lines +40 to +43

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good work!


// 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 Zero", () => {
let elements = ["a", "d", "hi"];
expect(sum(elements)).toEqual(0);
});
Comment on lines +56 to +59

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lovely work

9 changes: 2 additions & 7 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
// Refactor the implementation of includes to use a for...of loop
// this new version looks great.

function includes(list, target) {
for (let index = 0; index < list.length; index++) {
const element = list[index];
if (element === target) {
return true;
}
}
return false;
return list.includes(target) ? true : false;
}

module.exports = includes;
Loading
Loading