-
-
Notifications
You must be signed in to change notification settings - Fork 149
ZA-ITP-May-2025 | Christian Mayamba | Module-Data-Groups | Week 1 #573
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 3 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 |
---|---|---|
|
@@ -6,9 +6,35 @@ | |
// 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)) { | ||
return null; | ||
} | ||
|
||
const numbersArray = list.filter((item) => typeof item === "number"); | ||
numbersArray.sort((a, b) => a - b); | ||
const middleIndex = Math.floor(numbersArray.length / 2); | ||
|
||
if ( | ||
list.every( | ||
(item) => typeof item === "string" || item === null || item === undefined | ||
) | ||
) { | ||
return null; | ||
} | ||
|
||
if (numbersArray.length % 2 === 0) { | ||
const tempMedian = | ||
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.
|
||
numbersArray[middleIndex] + numbersArray[middleIndex - 1]; | ||
const median1 = tempMedian / 2; | ||
return median1; | ||
Comment on lines
+28
to
+29
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. Is there a reason you're calling this (And in fact, maybe you don't need a variable at all here - what value do you think the variable provides over just writing |
||
} else { | ||
const middleIndex = Math.floor(numbersArray.length / 2); | ||
const median = numbersArray.splice(middleIndex, 1)[0]; | ||
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. This branch uses |
||
return median; | ||
} | ||
} | ||
|
||
module.exports = calculateMedian; | ||
|
||
myArray = calculateMedian(["11", "mango"]); | ||
console.log(myArray); |
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. Tests passed, well done |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,20 @@ | ||
function findMax(elements) { | ||
if (elements.length === 0) { | ||
return "-Infinity"; | ||
} | ||
|
||
if (elements.every((item) => typeof item === "string")) { | ||
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. Same question as before - is "I can't find the max of an array of only strings" really the general statement here? |
||
return "undefined"; | ||
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. Returning the string "undefined" seems like an odd choice here. Can you think of a more appropriate value to return? |
||
} | ||
|
||
const numbersArray = elements.filter((item) => typeof item === "number"); | ||
elements.sort((a, b) => a - b); | ||
const maxNum = elements[elements.length - 1]; | ||
|
||
return maxNum; | ||
} | ||
const verArray = [-5, -10, -65, -6]; | ||
|
||
console.log(findMax(verArray)); | ||
|
||
module.exports = findMax; |
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. Tests passed, well done |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,21 @@ | ||
function sum(elements) { | ||
if (elements.length === 0) { | ||
return 0; | ||
} | ||
|
||
if (elements.every((item) => typeof item === "string")) { | ||
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. Same questions as above |
||
return "undefined"; | ||
} | ||
|
||
const numbersArray = elements.filter((item) => typeof item === "number"); | ||
|
||
let sum1 = 0; | ||
for (let i = 0; i < numbersArray.length; i++) { | ||
sum1 += numbersArray[i]; | ||
} | ||
return sum1; | ||
} | ||
testVar = [10, "jesus", 20, "hi"]; | ||
console.log(sum(testVar)); | ||
|
||
module.exports = sum; |
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. Tests passed, well done |
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.
This code looks like it wouldn't handle extra test-cases we could write, e.g. what would happen if the list contained only boolean values (true/false)?
We want to write code which is general - that is, it fixes the core problem statement, rather than a particular set of tests - please try to rewrite this so that it's more general.