-
-
Notifications
You must be signed in to change notification settings - Fork 283
Manchester | 26-ITP-Jan | Abdu Hassen | Sprint 1| Data-Grouping #1120
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
55e404b
c07db61
f2129f5
51e3a0b
760695c
4095a19
eae64c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,9 +6,27 @@ | |
| // or 'list' has mixed values (the function is expected to sort only numbers). | ||
|
|
||
| function calculateMedian(list) { | ||
| if (!Array.isArray(list)) { | ||
| return null; | ||
| } | ||
|
|
||
| list = list.filter((value) => typeof value === "number"); | ||
|
|
||
| if (list.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| list.sort((a, b) => a - b); | ||
|
|
||
| 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 { | ||
| const middleIndex = Math.floor(list.length / 2); | ||
|
cjyuan marked this conversation as resolved.
Outdated
|
||
| const median = list.splice(middleIndex, 1)[0]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use the same syntax you were using on line 24 to access an array element?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i was writing the codes each one by one that why i forgot to combine them and some times little bit confusing putting line of codes together if you have any recommendation of books or techniques that i can read that would be helpful. thank you.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After the implemented function is working correctly, we should refactor our code to further improve the code. The process involves reviewing the code carefully, and reading code is a skill that takes time to develop (through reading and writing more code). If you want something to read, you can try https://javascript.info/ |
||
| return median; | ||
| } | ||
| } | ||
|
|
||
| module.exports = calculateMedian; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,13 @@ | ||
| function dedupe() {} | ||
| function dedupe(arr) { | ||
| if (arr.length === 0) { | ||
| return []; | ||
| } | ||
| if (arr.length !== new Set(arr).size) { | ||
| const cleanedArray = [...new Set(arr)]; | ||
| return cleanedArray; | ||
| } else { | ||
| return arr; | ||
| } | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,12 +16,37 @@ 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("The function should return a copy of the original array if the array contains no duplicates", () => { | ||
| expect(dedupe([2, 4, 5, 6, 8])).toEqual([2, 4, 5, 6, 8]); | ||
| expect(dedupe([3, 7, 5, 8, 14, 19])).toEqual([3, 7, 5, 8, 14, 19]); | ||
| expect(dedupe(["apple", "banana", "milk", "egg"])).toEqual([ | ||
| "apple", | ||
| "banana", | ||
| "milk", | ||
| "egg", | ||
| ]); | ||
| }); | ||
|
cjyuan marked this conversation as resolved.
Comment on lines
+26
to
+38
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a chance that, even though the return value has incorrect elements (for example,
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i did some fixation now it is improved.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not about how many sets of data you are testing, the issue in "how" you carry out the test. Don't think how you implement your function. Think about under what condition a buggy function that returns |
||
| // 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("The function should return a clean array when a duplicated array is passed", () => { | ||
| expect(dedupe([2, 2, 4, 4, 5, 5, 6, 6, 6, 6, 8, 8, 8, 8])).toEqual([ | ||
| 2, 4, 5, 6, 8, | ||
| ]); | ||
| expect(dedupe([1, 1, 1, 2, 2, 3, 3, 4, 4, 6, 6, 7, 7, 8])).toEqual([ | ||
| 1, 2, 3, 4, 6, 7, 8, | ||
| ]); | ||
| expect( | ||
| dedupe(["ahmed", "ahmed", "chris", "chris", "tom", "tom", "joy"]) | ||
| ).toEqual(["ahmed", "chris", "tom", "joy"]); | ||
| expect( | ||
| dedupe([2, 2, "apple", "apple", 5, 5, "samsung", "samsung", 8, 8]) | ||
| ).toEqual([2, "apple", 5, "samsung", 8]); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,20 @@ | ||
| function findMax(elements) { | ||
| if (elements.length === 0) { | ||
| return -Infinity; | ||
| } | ||
|
|
||
| const number = elements.filter((value) => typeof value === "number"); | ||
|
|
||
| if (number.length === 0) { | ||
| return undefined; | ||
| } | ||
| let maxNumber = number[0]; | ||
| for (let i = 1; i < number.length; i++) { | ||
| if (number[i] > maxNumber) { | ||
| maxNumber = number[i]; | ||
| } | ||
| } | ||
| return maxNumber; | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,17 @@ | ||
| function sum(elements) { | ||
| } | ||
| if (elements.length === 0) { | ||
| return 0; | ||
| } | ||
| const number = elements.filter((value) => typeof value === "number"); | ||
| if (number.length === 0) { | ||
| return undefined; | ||
| } | ||
| let sum = 0; | ||
| for (let i = 0; i < number.length; i++) { | ||
| sum += number[i]; | ||
| } | ||
|
|
||
| return sum; | ||
| } | ||
| console.log(sum([])); | ||
|
cjyuan marked this conversation as resolved.
Outdated
|
||
| module.exports = sum; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,24 +13,54 @@ 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("The function should return 0 when an empty array is passed", () => { | ||
| expect(sum([])).toEqual(0); | ||
| }); | ||
|
|
||
| // Given an array with just one number | ||
| // When passed to the sum function | ||
| // Then it should return that number | ||
|
|
||
| test("The function should return single value when single value array is passed", () => { | ||
| expect(sum([7])).toEqual(7); | ||
| expect(sum([14])).toEqual(14); | ||
| expect(sum([0])).toEqual(0); | ||
| expect(sum([100])).toEqual(100); | ||
| }); | ||
| // Given an array containing negative numbers | ||
| // When passed to the sum function | ||
| // Then it should still return the correct total sum | ||
|
|
||
| test("The function should return the correct total value when negative number array is passed", () => { | ||
| expect(sum([-7, -10, -33])).toEqual(-50); | ||
| expect(sum([-6, -10, -18, -4])).toEqual(-38); | ||
| expect(sum([-100, -10, -1, -20])).toEqual(-131); | ||
| expect(sum([-12, -23, -34, -45, -56, -67])).toEqual(-237); | ||
| }); | ||
| // Given an array with decimal/float numbers | ||
| // When passed to the sum function | ||
| // Then it should return the correct total sum | ||
|
|
||
| test("The function should return the correct total value when decimal/float numbers array is passed", () => { | ||
| expect(sum([7.8045, 1.273, 3.19])).toEqual(12.2675); | ||
| expect(sum([4.6, 1.8, 3.18, 5.4])).toEqual(14.98); | ||
| expect(sum([11 / 12, 4 / 6, 1 / 2, 6 / 20])).toEqual(2.383333333333333); | ||
| expect(sum([1 / 2, 2 / 3, 3 / 4, 4 / 5, 5 / 6, 6 / 7])).toEqual( | ||
| 4.4071428571428575 | ||
| ); | ||
|
Comment on lines
+42
to
+48
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why specify different "number of decimal places" in your tests?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the more decimal places I expected value has, the stricter my test needs to be.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just enforce the strictest tolerable difference? If the function can pass this test For adding only a few numbers, the rounding error will be very small. So it's ok to be strict.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok i will implement that in fact in this test i did that the test go through all the decimal numbers until the end that's why i specified each decimal number to test. |
||
| }); | ||
|
cjyuan marked this conversation as resolved.
|
||
| // 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("The function should return the correct total value when an array containing non-number values is passed", () => { | ||
| expect(sum([7.8045, "two", 3.19])).toEqual(10.9945); | ||
| expect(sum(["one", 1.8, 3.18, "zero"])).toEqual(4.98); | ||
| expect(sum([1 / 12, 4 / 6, "Ahmed", 6 / 8])).toEqual(1.5); | ||
| expect(sum(["one", "two", "three", "four", "five", 1 / 2])).toEqual(0.5); | ||
| }); | ||
| // 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("The function should return undefined when an array with only non-number values is passed", () => { | ||
| expect(sum(["male", "two", "age"])).toEqual(undefined); | ||
| expect(sum(["one", "zero"])).toEqual(undefined); | ||
| expect(sum(["world", "apple", "Ahmed", "london"])).toEqual(undefined); | ||
| expect(sum(["one", "two", "three", "four", "five"])).toEqual(undefined); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you plan to consider also
-Infinity,Infinity, andNaNin the median calculation (and in the functions inimplement/max.jsandimplement/sum.js)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to handle all type of input i changed the filter , using built in method of
Number.isFinite(value)can handle all type of values.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not apply the change also to
findMax()andsum()?