-
-
Notifications
You must be signed in to change notification settings - Fork 153
LONDON | MAY2025 | EMILIANO URUENA | DATA_GROUPS | Sprint-1 #546
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 |
---|---|---|
|
@@ -6,9 +6,29 @@ | |
// 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; | ||
// First of all, we check if list is an array. | ||
if (!Array.isArray(list)) { | ||
return null | ||
} | ||
// the next code from 13 to 16create a new Array called listFiltered checking if the elements are numbers and no NaN> | ||
const listFiltered = list.filter(checkElement); | ||
function checkElement(n){ | ||
return typeof n === "number" && n != isNaN; | ||
} | ||
// return null if there are not numbers in the list. | ||
if (listFiltered.length == 0) { | ||
return null | ||
} | ||
// sort the list | ||
listFiltered.sort(); | ||
const middleIndex = Math.floor(listFiltered.length / 2); | ||
// check for even or odd amount of numbers in the list | ||
if (listFiltered.length % 2 == 0) { | ||
const median = (listFiltered[middleIndex - 1] + listFiltered[middleIndex]) / 2; | ||
return median | ||
} else { | ||
const median = listFiltered.splice(middleIndex, 1)[0]; | ||
return median | ||
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. Using an Inline return statement on this line and line 28 is best practice |
||
} | ||
} | ||
|
||
module.exports = calculateMedian; | ||
module.exports = {calculateMedian} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,12 @@ | ||
function dedupe() {} | ||
|
||
function dedupe(list) { | ||
if (list.length === 0 ) return list; | ||
let newList = [] | ||
for (i = 0 ; i < list.length; i ++){ | ||
if (!newList.includes(list[i])) { | ||
newList.push(list[i]); | ||
} | ||
} | ||
return newList; | ||
} | ||
module.exports = {dedupe} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,19 @@ | ||
function findMax(elements) { | ||
} | ||
if (elements.length === 0) return '-Infinity'; | ||
if (elements.length === 1) return elements; | ||
let maxNum = [] | ||
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 is maxNum an array? A number type as the return value is most appropriate for this function. |
||
for (i = 0 ; i < elements.length ; i++){ | ||
if (typeof elements[i] === 'number'){ | ||
if (maxNum.length === 0){ | ||
maxNum[0] = elements[i]; //give a value for the first time | ||
} else { | ||
if (elements[i] > maxNum[0]){ | ||
maxNum[0] = elements[i]; | ||
} | ||
} | ||
} | ||
} | ||
if (maxNum.length === 0){ return 'Family' } else { return maxNum } | ||
|
||
} | ||
module.exports = findMax; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ 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.todo("given an empty array, returns -Infinity"); | ||
|
||
// Given an array with one number | ||
// When passed to the max function | ||
|
@@ -41,3 +41,52 @@ test.todo("given an empty array, returns -Infinity"); | |
// 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 | ||
|
||
describe("findMax", () => { | ||
[ | ||
{ input: [30, 50, 10, 40], expected: [50]}, | ||
{ input: ['hey', 10, 'hi', 60, 10], expected: [60]}, | ||
{ input: [79], expected: [79]}, | ||
|
||
|
||
].forEach(({input, expected}) => | ||
it(`return max number for [${input}]`,() => expect(findMax(input)).toEqual(expected)) | ||
|
||
); | ||
[ | ||
{ input: [], expected: '-Infinity'}, | ||
|
||
].forEach(({input, expected}) => | ||
it(`return -Infinity to empty array: [${input}]`,() => expect(findMax(input)).toEqual(expected)) | ||
|
||
); | ||
[ | ||
{ input: [-3,5,8,34,-45,0], expected: [34]}, | ||
|
||
].forEach(({input, expected}) => | ||
it(`an array with both positive and negative numbers should return the largest number overall [${input}]`,() => expect(findMax(input)).toEqual(expected)) | ||
|
||
); | ||
[ | ||
{ input: [-34,-13,-65,-678,-45366], expected: [-13]}, | ||
|
||
].forEach(({input, expected}) => | ||
it(`An array with just negative numbers should return the closest one to zero: [${input}]`,() => expect(findMax(input)).toEqual(expected)) | ||
|
||
); | ||
[ | ||
{ input: [0.143,0.31426,0.81], expected: [0.81]}, | ||
|
||
].forEach(({input, expected}) => | ||
it(`An array with decimal number should return the largest decimal number [${input}]`,() => expect(findMax(input)).toEqual(expected)) | ||
|
||
); | ||
[ | ||
{ input: ['e','r','n','a','l','g','l'], expected: 'Family'}, | ||
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 'Family' the best value to return in this case? Why not return something like null. |
||
|
||
].forEach(({input, expected}) => | ||
it(`An array with only non-number values should return the least surprising value given how it behaves for all other inputs [${input}]`,() => expect(findMax(input)).toEqual(expected)) | ||
|
||
); | ||
} | ||
); |
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.length === 1) return elements; | ||
let sum = 0 | ||
let noNum = [] | ||
for (i = 0 ; i < elements.length ; i++){ | ||
if (typeof elements[i] === 'number'){ | ||
sum += elements[i]; | ||
} else { | ||
noNum.push(elements[i]) | ||
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 better way to check if the array contains no numbers, instead of making a new array and adding all the elements to it? Every array in your code will use more memory, and for very large inputs this will be inefficient. |
||
} | ||
} | ||
if (elements.length === noNum.length) return 'Family' | ||
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. Again, is returning 'Family' the most appropriate here? Why not either 0 or null? |
||
return sum; | ||
// if (maxNum.length === 0){ return 'Family' } else { return maxNum } | ||
|
||
|
||
} | ||
//console.log(sum([2,3,5,3])) | ||
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.
Using an anonymous function here will make this method more concise (instead of checkElement).