Skip to content

Solution - #2334

Open
MkxxiM wants to merge 2 commits into
mate-academy:masterfrom
MkxxiM:develop
Open

MkxxiM wants to merge 2 commits into
mate-academy:masterfrom
MkxxiM:develop

Conversation

@MkxxiM

@MkxxiM MkxxiM commented Aug 22, 2026

Copy link
Copy Markdown

No description provided.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Review Summary

The code has two functional issues that must be addressed before approval. First, the average calculation is computed inside the loop on every iteration, which is inefficient and conceptually incorrect—calculate the total in the loop, then compute the average once after the loop completes. Second, and more critically, the backToStringFormat function uses a flawed algorithm that can produce malformed output (e.g., '123' becoming ',123') due to incorrect substring index calculations. This is a functional bug that produces wrong output for certain inputs. Consider using JavaScript's built-in toLocaleString() or Intl.NumberFormat instead for reliable thousands formatting.


✨ 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 thread src/scripts/main.js Outdated
Comment on lines +15 to +17
for (let i = 0; i < arrayOfElements.length; i++) {
totalPopulation += arrayOfElements[i];
avaragePopulation = Math.floor(totalPopulation / arrayOfElements.length);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The average is being recalculated on every loop iteration. Calculate the total first in the loop, then compute average once after the loop completes using the total.

Comment thread src/scripts/main.js Outdated
Comment on lines +31 to +43
function backToStringFormat(from) {
const str = from.toString();
let result = '';

for (let i = str.length; i >= 0; i = i - 3) {
result = str.substring(i - 3, i) + result;

if (i > 3) {
result = ',' + result;
}
}

return result;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The backToStringFormat function uses a flawed algorithm that can produce malformed output. For example, '123' would become ',123' due to how the substring indices are calculated. Consider using JavaScript's built-in toLocaleString() or Intl.NumberFormat instead for reliable thousands formatting.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Review Summary

Your solution correctly implements all task requirements. The average calculation now runs efficiently with a single loop for totaling and one computation for the average, avoiding the previous O(n²) issue. The population values are properly formatted using toLocaleString('en-US') to add thousands separators, and both the average-population and total-population display elements are correctly updated with the formatted results. The code is functional and meets the core requirements of the task.


✨ 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