-
-
Notifications
You must be signed in to change notification settings - Fork 157
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -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
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. Can you figure out why the "doesn't modify the input" test in 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. Shallow copy ([...list]): Ensures the original order and content are preserved. |
||
});*/ | ||
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
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 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. 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. Okey. I've got it. |
||
}); | ||
|
||
|
||
|
||
|
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; |
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; |
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; |
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; |
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; |
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 | ||
}); |
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.
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 oflist
.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.
Okey. I did it.