Skip to content

add task solution#2194

Open
LiliiaVol wants to merge 3 commits intomate-academy:masterfrom
LiliiaVol:develop
Open

add task solution#2194
LiliiaVol wants to merge 3 commits intomate-academy:masterfrom
LiliiaVol:develop

Conversation

@LiliiaVol
Copy link

No description provided.

@LiliiaVol
Copy link
Author

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 getting the core calculations for total and average population working! However, a couple of changes are needed before this can be approved.

The primary issue is the logic for formatting numbers with thousands separators. Your current implementation is hardcoded and doesn't work correctly for all numbers, which is a key requirement. For a much simpler and more reliable solution, I recommend looking into the built-in number.toLocaleString() method.

Additionally, the task instructions specified that only the main.js file should be changed. Please revert the modifications made to the style files.

Once these issues are addressed, your solution will be in great shape. Keep up the good work!


✨ 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

population.forEach((populationContent) => {
let num = '';

for (let i = 0; populationContent.textContent.length >= i; i++) {

Choose a reason for hiding this comment

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

The loop condition here should be i < populationContent.textContent.length. Using >= causes the loop to run one extra time where populationContent.textContent[i] will be undefined. While it doesn't break your code in this case because of how Number(undefined) is handled, it's a common source of bugs and logically incorrect.

Comment on lines +26 to +33
for (let i = arrayOfNum.length - 1; i >= 0; i--) {
if (i % 3 === 0 && i !== arrayOfNum.length - 1) {
result += ',';
result += arrayOfNum[i];
} else {
result += arrayOfNum[i];
}
}

Choose a reason for hiding this comment

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

This logic for adding thousands separators is not quite right. The condition i % 3 === 0 depends on the index from the start of the number, but it should be based on groups of three digits from the end. As a result, this will format numbers incorrectly (e.g., 12345 would become 1,2345). A much simpler and more reliable way to achieve this is with the built-in number.toLocaleString() method.

return result;
}

result = result.split(',').join('');

Choose a reason for hiding this comment

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

It's inefficient to remove the commas from the formatted result string just to calculate the average. The original numeric sum is still available in the total variable. You should use total directly to perform this calculation, for example: const average = Math.round(total / population.length);.

result = '';

for (let i = arrayOfNumAvarage.length - 1; i >= 0; i--) {
if (i === 5 || i === 2) {

Choose a reason for hiding this comment

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

This condition is "hardcoded" and will only work for formatting a 9-digit number. This logic is not robust and will fail if the average population has a different number of digits. Your formatting logic should be generic enough to work for any number. The toLocaleString() method mentioned earlier would be a great solution here as well.

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 on the JavaScript logic! Your use of toLocaleString() to format the numbers is a great solution and works perfectly.

However, this submission requires a change because it includes modifications to style files, such as src/styles/main.scss. The task instructions explicitly state that only the main.js file should be modified.

Please remove the style files from your pull request and resubmit with only the changes to main.js. After that small correction, your solution will be ready to go!


✨ 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

Comment on lines +12 to +19
for (let i = 0; populationContent.textContent.length > i; i++) {
if (
Number(populationContent.textContent[i]) ||
Number(populationContent.textContent[i]) === 0
) {
num += populationContent.textContent[i];
}
}

Choose a reason for hiding this comment

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

This loop works for extracting the digits, but a more common and concise way to handle this is by using the replace() method to remove the commas from the string before converting it to a number. For example: const numberString = populationContent.textContent.replace(/,/g, '');.

return result;
}

result = Math.round(Number(total) / population.length);

Choose a reason for hiding this comment

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

Since total is already a number, wrapping it in Number() here is redundant. You can simplify this to Math.round(total / population.length).

Comment on lines +37 to +38
document.querySelector('.total-population').textContent = getTotalSum('total');
document.querySelector('.average-population').textContent = getTotalSum('');

Choose a reason for hiding this comment

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

Calling getTotalSum twice causes the total to be calculated two separate times, which is inefficient. A better approach is to calculate the total only once, then derive the average from that total, and finally update both elements in the HTML.


body {
background: #eee;
counter-reset: section;

Choose a reason for hiding this comment

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

The task instructions specify that only the main.js file should be changed. Please remove any style files from your submission to adhere to this requirement.

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

I'm approving this solution as an educational override to help you progress, but please pay close attention to the feedback for future tasks.

Great job on the JavaScript logic! Your calculations are correct, and you've used toLocaleString() perfectly to format the numbers.

However, a critical requirement for this task was to only modify the main.js file. Your submission included changes to style files (.scss), which should be avoided. Please make sure to only commit the required files in your future pull requests.

Also, I noticed a small typo in your variable name: avarage should be average. Correcting this improves code readability.

Keep up the great work, and remember to carefully read the task requirements!


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

Footnotes

  1. Rate AI review example

Comment on lines 1 to +5
@font-face {
font-family: Roboto, Arial, Helvetica, sans-serif;
src: url('../fonts/Roboto-Regular-webfont.woff') format('woff');
font-weight: normal;
font-style: normal;
src: url('../fonts/Roboto-Regular-webfont.woff') format('woff');

Choose a reason for hiding this comment

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

According to the task requirements, only the main.js file should be modified. Please remove this file and other style files from your submission.

};

const totalResult = getTotalSum('total');
const avarage = getTotalSum('');

Choose a reason for hiding this comment

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

There's a small typo in this variable name. It should be average.

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