-
-
Notifications
You must be signed in to change notification settings - Fork 94
WEST MIDLANDS-JAN-ITP|SEGUN FOLAYAN|DATA GROUPS|SPRINT-1 #473
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?
Conversation
Sprint-1/fix/median.js
Outdated
@@ -4,8 +4,17 @@ | |||
|
|||
function calculateMedian(list) { | |||
const middleIndex = Math.floor(list.length / 2); | |||
const median = list.splice(middleIndex, 1)[0]; | |||
return median; | |||
const median=0; |
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.
Your logic is good, but the code looks a bit repetitive. The variable median is unnecessary. Could you refactor the code to return the result directly instead of assigning it to the variable median?
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.
function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
if (list.length % 2 === 1) {
return list[middleIndex];
}
return (list[middleIndex] + list[middleIndex - 1]) / 2; lists
}
module.exports = calculateMedian;
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.
Thank you
|
||
|
||
function dedupe(arr) { | ||
return [...new Set(arr)]; |
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.
Could you explain the characteristics of Set object
and why we need to use the spread operator here?
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.
- Uniqueness-It does not contain duplicates.
- No index access- IT cannot be accessed using indices like arrays.
- Insertion order-The values in the set are iterated in the order in which they are added.
- Type flexibility- A Set can store values of any type (primitive or object), and even objects can be stored as unique values (by reference).
- The spread operator is used to convert the Set back into an array.
- Thank you
Sprint-1/implement/sum.js
Outdated
} | ||
|
||
} | ||
return total; |
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.
JavaScript uses floating-point numbers, which are not always precise and lead to rounding issues.
If you test the following line expect(sum([1.6, 2.5, 3.1])).toBe(7.2) It will cause an error.
Can you refactor the sum() function so it passes this test case?
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.
function sum(elements) {
let total = 0;
for (let Index = 0; Index < elements.length; Index++) {
if (typeof elements[Index] === "number") {
total += elements[Index];
} else {
total = total + 0;
}
}
return parseFloat(total.toFixed(10));
}
.......................Thank you
|
||
// 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, then it should return the correct sum", () => { | ||
expect(sum([1.25,1.75,1.6])).toEqual(4.6); | ||
} |
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.
Can you add expect(sum([1.6, 2.5, 3.1])).toBe(7.2)
and refactor the code to pass the test?
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.
function sum(elements) {
let total = 0;
for (let Index = 0; Index < elements.length; Index++) {
if (typeof elements[Index] === "number") {
total += elements[Index];
} else {
total = total + 0;
}
}
return parseFloat(total.toFixed(10));
}
module.exports = sum;
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.
Many Thanks
Good job 👍 Let me know if you have any questions! |
Learners, PR Template
Self checklist
Changelist
Questions
Ask any questions you have for your reviewer.