Skip to content

ITP_GLASGOW_APR | HANNA_MYKYTIUK | MODULE_DATA_GROUPS | SPRINT_1 #491

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

HannaOdud
Copy link

Learners, PR Template

Self checklist

  • I have committed my files one by one, on purpose, and for a reason
  • I have titled my PR with COHORT_NAME | FIRST_NAME LAST_NAME | REPO_NAME | WEEK
  • I have tested my changes
  • My changes follow the style guide
  • My changes meet the requirements of this task

Changelist

Briefly explain your PR.

Questions

Ask any questions you have for your reviewer.

@HannaOdud HannaOdud added the Needs Review Participant to add when requesting review label Apr 10, 2025
@cjyuan cjyuan added the Review in progress This review is currently being reviewed. This label will be replaced by "Reviewed" soon. label Apr 13, 2025
Copy link

@cjyuan cjyuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can make some improvements in some of your code.

Comment on lines +27 to +29
if (typeof list[i] == 'number'){ // add value to new array only if it is numeric
numbersList.push(list[i]);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why check if the elements are of type "number" another time? (Isn't the code at lines 14-23 enough?)

let middleIndex = Math.floor(numbersList.length / 2);
let median;
if ( numbersList.length%2 != 0 ){// check if array elements quantity is odd number
median = numbersList.splice(middleIndex, 1)[0];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is shorter syntax to access an array element.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You missed specify two cases mentioned in this file:

// Given an empty array
// When passed to the dedupe function
// Then it should return an empty array

// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array

Comment on lines +15 to +31
// if elements does not contain numbers we should search for values that can be converted to numbers
if (containsNumbers == false){
for (let i = 0; i < elements.length; i++){
//check if variable can be converted to number and check if variable max is assigned
if (!(typeof max == 'number') && !isNaN(elements[i])){
// if both true assign converted value to max
max = Number(elements[i]);
}
// if max is already assigned and elements[i] value can be converted to number
if (typeof max == 'number' && !isNaN(elements[i])){
// check if new walue is greater than max
if (Number(elements[i]) > max){
max = Number(elements[i]);
}
}
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most people choose to return -Infinity when all elements are non-number. That is, they treat such array as having no numbers.

If we initialised max to -Infinity, then we would not need to check !(typeof max == 'number') and we also do not need lines 19-21. Too much checking can degrade the performance of a program.

We can also return the value of max right before line 31. If the array does not contain any number, then we don't have to execute the remaining code.

Comment on lines +35 to +37
if (!(typeof max == 'number') && typeof elements[i] == 'number'){
max = elements[i];
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would not need these few lines if max was initialised to -Infinity.

max = elements[i];
}
// compare each numeric value with max, and replace max if new value greater
if (typeof max == 'number'){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this check typeof elements[i] == 'number' instead?

Comment on lines +12 to +18
if (containsNumbers == false){
for (let i=0; i<elements.length; i++){
if(!isNaN(elements[i])){
sum += Number(elements[i]);
}
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could return the sum right before line 18 and avoid unnecessary processing.

Comment on lines +36 to +39
it("array with decimal/float numbers should return correct sum", () => {
const value = sum([7.1, 2.5, 0.6]);
expect(value).toEqual(10.2);
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decimal numbers in most programming languages (including JS) are internally represented in "floating point number" format. Floating point arithmetic is not exact. For example, the result of 46.5678 - 46 === 0.5678 is false because 46.5678 - 46 only yield a value that is very close to 0.5678. Even changing the order in which the program add/subtract numbers can yield different values.

So the following could happen

  expect(sum([1.2, 0.6, 0.005])).toEqual(1.805);                // This fail
  expect(sum([1.2, 0.6, 0.005])).toEqual(1.8049999999999997);   // This pass
  expect(sum([0.005, 0.6, 1.2])).toEqual(1.8049999999999997);   // This fail

  console.log(1.2 + 0.6 + 0.005 == 1.805);  // false
  console.log(1.2 + 0.6 + 0.005 == 0.005 + 0.6 + 1.2); // false

Can you find a more appropriate way to test a value (that involves decimal number calculations) for equality?

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review and removed Needs Review Participant to add when requesting review Review in progress This review is currently being reviewed. This label will be replaced by "Reviewed" soon. labels Apr 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Reviewed Volunteer to add when completing a review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants