Skip to content

Add task solution#2197

Open
pavloPolyvoda-dev wants to merge 2 commits intomate-academy:masterfrom
pavloPolyvoda-dev:develop
Open

Add task solution#2197
pavloPolyvoda-dev wants to merge 2 commits intomate-academy:masterfrom
pavloPolyvoda-dev:develop

Conversation

@pavloPolyvoda-dev
Copy link

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

Great job fetching the data from the DOM and correctly calculating the total and average population! The core logic is solid.

However, this requires changes because the custom number formatting function, strFormatNum, doesn't work correctly for all cases. For example, a number like 1,007,009 is incorrectly formatted as "1,7,9". This fails a key requirement of the task.

A much more robust and simpler solution is to use the built-in Number.prototype.toLocaleString() method. This function is designed to handle thousand separators and localization correctly.

For example:

// For integers
sumPopNum.toLocaleString('en-US');

// For numbers with decimals
averagePopNum.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 });

You're very close! Once you fix the number formatting, this will be ready.


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

return sum + Number(population);
}, 0);

const averagePopNum = (sumPopNum / populationArray.length).toFixed(2);

Choose a reason for hiding this comment

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

Using .toFixed(2) here converts the average to a string prematurely. This forces your strFormatNum function to handle both string and number inputs, which can lead to unexpected behavior due to implicit type coercion. It's better to keep values as Number types for as long as possible and handle all formatting, including setting decimal places, within the formatting function itself.

Comment on lines +21 to +38
function strFormatNum(number) {
const numberItems = [];

let currentNumber = number;


while (currentNumber / 1000 > 0) {
if (currentNumber !== parseInt(currentNumber)) {
numberItems.unshift((currentNumber % 1000).toFixed(2));
} else {
numberItems.unshift(currentNumber % 1000);
}

currentNumber = parseInt(currentNumber / 1000);
}

return numberItems.join(',');
}

Choose a reason for hiding this comment

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

This custom function for formatting numbers doesn't handle all cases correctly. For instance, a number like 1,007,009 would be formatted incorrectly as "1,7,9" because the logic doesn't pad number groups with leading zeros.

A more robust and standard approach is to use the built-in Number.prototype.toLocaleString() method, which is designed for this purpose.

For example:

// For integers
sumPopNum.toLocaleString('en-US');

// For numbers with decimals
averagePopNum.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 });

This would be a much simpler and more reliable solution.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

Excellent work! I am approving your solution. You've successfully addressed the feedback from the previous review by implementing toLocaleString() for number formatting. The code is now clean, correct, and fully meets all the requirements of the task. Great job!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Footnotes

  1. Rate AI review example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants