Skip to content

ITP-JAN-25 | LONDON | Bakytbek Sydykov | Module-Data-Groups | sprint 1 #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
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 @@ -2,10 +2,25 @@
// Start by running the tests for this function
// If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory

function calculateMedian(list) {
/*function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
if (list.length % 2 === 0) {
return (list[middleIndex - 1] + list[middleIndex]) / 2;
} else {
return list[middleIndex];
}
Comment on lines +7 to +11
Copy link
Contributor

Choose a reason for hiding this comment

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

The arrays specified in median.test.js happened to be sorted. (Those are poorly selected test values)

Normally, before we can easily find the median value in an array, we need to sort the array first.
Can you try inserting a statement to sort the numbers in list first?

Also, because .sort() can modify the content in an array. So it is better to sort a cloned version of list.

Copy link
Author

Choose a reason for hiding this comment

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

Okey. I did it.

}
console.log(calculateMedian([1,2,3,6,7,9]));
module.exports = calculateMedian;*/
function calculateMedian(list) {
const sortedList = [...list].sort((a, b) => a - b);
const middleIndex = Math.floor(sortedList.length / 2);

module.exports = calculateMedian;
if (sortedList.length % 2 === 0) {
return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2;
} else {
return sortedList[middleIndex];
}
}
console.log(calculateMedian([1,2,3,6,7,9]));
module.exports = calculateMedian;
17 changes: 15 additions & 2 deletions Sprint-1/fix/median.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,24 @@ describe("calculateMedian", () => {
expect(calculateMedian([1, 2, 3, 4])).toEqual(2.5);
expect(calculateMedian([1, 2, 3, 4, 5, 6])).toEqual(3.5);
});

test("doesn't modify the input", () => {
});
/*test("doesn't modify the input", () => {
const list = [1, 2, 3];
calculateMedian(list);

expect(list).toEqual([1, 2, 3]);
});

Comment on lines 23 to +26
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you figure out why the "doesn't modify the input" test in median.test.js may fail to check what it describes, and improve the test code accordingly?

Copy link
Author

Choose a reason for hiding this comment

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

Shallow copy ([...list]): Ensures the original order and content are preserved.
toStrictEqual instead of toEqual: Ensures the entire structure and reference remain unchanged.
This will make sure that 'calculateMedian' does not modify the original input.

});*/
test("doesn't modify the input", () => {
const list = [1, 2, 3];
const originalList = [...list];

calculateMedian(list);

expect(list).toStrictEqual(originalList);
Comment on lines +29 to +34
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be better to use an array that is not sorted so that in case the function do try to sort (modify) the array passed to the function, we have a better chance in detecting it.

Copy link
Author

Choose a reason for hiding this comment

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

Okey. I've got it.

});




5 changes: 4 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
function dedupe() {}
function dedupe(arr) {
return [...new Set(arr)];
}
module.exports = dedupe;
15 changes: 14 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,25 @@ 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", () => {
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

test("given an array with no duplicates, it returns a copy of the original array", () => {
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

test("given an array with strings or numbers, it removes the duplicate values, preserving the first occurrence", () => {
expect(dedupe(["a", "a", "a", "b", "b", "c"])).toEqual(["a", "b", "c"]);
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
});
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 @@
function findMax(elements) {

const numericElements = elements.filter((el) => typeof el === "number" && !isNaN(el));
if (numericElements.length === 0) return -Infinity;
return Math.max(...numericElements);
}


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

// 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 both positive and negative numbers, returns the largest number overall", () => {
expect(findMax([-10, 3, 20, -5])).toEqual(20);
});
// 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 just negative numbers, returns the closest one to zero", () => {
expect(findMax([-10, -3, -20, -5])).toEqual(-3);
});
// 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", () => {
expect(findMax([1.5, 2.3, 0.8, 2.9])).toEqual(2.9);
});
// 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 the max and ignores non-numeric values", () => {
expect(findMax([10, "hello", undefined, null, 15, {}, 8])).toEqual(15);
});
// 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", () => {
expect(findMax(["hello", undefined, null, {}, []])).toEqual(-Infinity);
});
8 changes: 7 additions & 1 deletion Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
function sum(elements) {
const numericalelements = elements.filter(el=> typeof el === "number" && !isNaN(el));
let total = 0;
for (let num of numericalelements) {
total = total + num;
}
return total;
}

console.log(sum([-2,-3]));
module.exports = sum;
26 changes: 21 additions & 5 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,40 @@ 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", () => {
expect(sum([])).toEqual(0);
});

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

test("given an array with just one number", () => {
expect(sum([1])).toEqual(1);
});
// 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", () => {
expect(sum([-3,-2])).toEqual(-5);
});
// 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", () => {
expect(sum([2.3,3.1,2.5,7.6])).toEqual(15.5)
});*/
test("given an array with decimal/float numbers", () => {
expect(sum([2.3, 3.1, 2.5, 7.6])).toBeCloseTo(15.5, 10); // 10 is the precision level
});
// 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-number values", () => {
expect(sum([true, "Hello",10,10])).toEqual(20)
});
// 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 containing non-number values", () => {
expect(sum([true, "Hello"])).toEqual(0)
});
6 changes: 2 additions & 4 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// 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 false;
return false;
}

module.exports = includes;
11 changes: 11 additions & 0 deletions prep/mean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function calculateMean(list){
let total = 0;

total+=list[0];
total+=list[1];
total+=list[2];

let result = total/list.length;
return result;
}
module.exports = calculateMean;
9 changes: 9 additions & 0 deletions prep/mean.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

const calculateMean = require("./mean.js");
test("calculates the mean of a list of numbers", () => {
const list = [3, 50, 7];
const currentOutput = calculateMean(list);
const targetOutput = 20;

expect(currentOutput).toEqual(targetOutput); // 20 is (3 + 50 + 7) / 3
});