Skip to content

Develop#2181

Open
Humenyuk1991 wants to merge 4 commits intomate-academy:masterfrom
Humenyuk1991:develop
Open

Develop#2181
Humenyuk1991 wants to merge 4 commits intomate-academy:masterfrom
Humenyuk1991:develop

Conversation

@Humenyuk1991
Copy link

No description provided.

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 am requesting changes because a few key task requirements around correctness and robustness are not fully met yet. Your script correctly collects .population values, strips commas, computes total and average, and writes them to .total-population and .average-population, which covers the core flow well. However, you currently convert strings with Number(...) but do not validate or filter out NaN, so any non-numeric span (or unexpected text) will cause the total and average to become NaN, violating the requirement to “make sure that the given string can be converted to a number.” You should map the cleaned strings to numbers, then filter with something like filter(n => Number.isFinite(n)), and compute the sum and average only from that validated list, dividing by the count of valid entries and guarding against the case where there are zero valid numbers (to avoid a NaN average). In addition, formatNumber uses toLocaleString() without a locale, which may not consistently produce comma thousands separators; explicitly specifying a locale such as num.toLocaleString('en-US') (or using Intl.NumberFormat('en-US')) will ensure the formatting matches the populations shown in the HTML. Once you add number validation, handle the empty/filtered list safely, and enforce a consistent thousands-separator style, your solution will fully meet the functional requirements. Keep the overall structure you have—just harden the parsing and formatting steps—and you’ll be in great shape.


✨ 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

const populationSpans = document.querySelectorAll('.population');
const populations = Array.from(populationSpans).map((span) => {
const text = span.textContent.trim();
const number = Number(text.replace(/,/g, ''));

Choose a reason for hiding this comment

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

Conversion removes commas and calls Number(...), but there is no check for invalid conversions. The requirement says to "Make sure that the given string can be converted to a number" — consider validating by using Number(...) then filtering out NaN (e.g., map to numbers and .filter(n => !Number.isNaN(n))).

return number;
});

const total = populations.reduce((sum, value) => sum + value, 0);

Choose a reason for hiding this comment

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

Reducing populations will produce NaN if any element is NaN. Ensure you reduce over a list of validated numbers (filter out invalid numbers first) so the total is correct.


const total = populations.reduce((sum, value) => sum + value, 0);

const average = total / populations.length;

Choose a reason for hiding this comment

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

Average is computed as total / populations.length. If you filter out invalid values you must divide by the count of valid numbers, and you should guard against division by zero (no valid population entries) to avoid producing NaN in the .average-population output.

const average = total / populations.length;

function formatNumber(num) {
return num.toLocaleString();

Choose a reason for hiding this comment

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

formatNumber uses toLocaleString() without specifying a locale. The task requires the same numeric style as the populations (they use comma thousands separators). To guarantee that across environments, pass an explicit locale (for example num.toLocaleString('en-US') or use new Intl.NumberFormat('en-US').format(num)).

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 am requesting changes because there are a couple of critical requirement gaps that can affect the correctness of the results. First, in src/scripts/main.js, you currently convert cleaned with Number(cleaned) even when cleaned is an empty string (see around the loop where you map span texts to numbers); Number('') becomes 0, so blank or missing .population spans are incorrectly treated as valid zeros, violating the requirement to only include strings that can truly be converted to numbers [CHECKLIST ITEM #3]. You should explicitly skip empty or non-numeric cleaned strings before converting, e.g., by checking if (cleaned === '') continue; or validating with a digits-only regex before calling Number(...). Second, the task specifies that only src/scripts/main.js should be changed, but your PR also modifies other files (like the README and config/workflow files), which conflicts with the “change only main.js” requirement; those unrelated edits need to be reverted.

Beyond those blockers, your use of Intl.NumberFormat('en-US') and the logic for summing, averaging, and guarding against division-by-zero look solid, and you're correctly writing results into .total-population and .average-population. As a small improvement, confirm whether the expected average should be an integer (Math.round(average)) or if a fractional value with fixed precision is required, and adjust the formatting accordingly. Once you skip empty/non-numeric spans and restrict changes to main.js, your solution should fully meet the task’s requirements—nice work overall on the main data-processing flow.


✨ 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


const cleaned = rawText.replace(/,/g, '');

const number = Number(cleaned);

Choose a reason for hiding this comment

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

Right now you call Number(cleaned) even when cleaned is empty; Number('') returns 0 which can silently treat missing/non-numeric spans as valid zeros. To fully satisfy checklist item #3 (make sure the string can be converted), skip empty strings before conversion (for example, check cleaned !== '' or return NaN for empty) so only real numeric text becomes part of the calculation.

: 0;


const formatter = new Intl.NumberFormat('en-US');

Choose a reason for hiding this comment

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

Using Intl.NumberFormat('en-US') is correct and ensures a consistent thousands separator (comma) as required by checklist item #20/#8/#9 — good choice. Keep this explicit locale to match the populations' numeric style.

formatter.format(total);

document.querySelector('.average-population').textContent =
formatter.format(Math.round(average));

Choose a reason for hiding this comment

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

Math.round(average) forces an integer average. Confirm whether the task expects the average to be rounded to an integer; if fractional precision is required, format the number accordingly (e.g. average.toFixed(1) before formatting). This affects the displayed average but not the arithmetic correctness.

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 am approving your solution as an educational override so you can keep progressing while you refine a few important details. You’ve done a solid job wiring up the core flow: selecting .population spans, parsing values, filtering out non-finite numbers, summing them, computing the average, and formatting the output with Intl.NumberFormat('en-US') before writing to .total-population and .average-population ([CHECKLIST ITEM #1, #2, #4, #5, #6]). The main thing to improve is numeric validation: after you strip commas, a span that contains only commas or whitespace currently becomes an empty cleaned string and Number(cleaned) turns into 0, which violates the requirement to only include truly numeric strings ([CHECKLIST ITEM #3]); at the conversion line (in src/scripts/main.js, near where you do Number(cleaned)), add a guard like if (cleaned === '' || !/^[0-9]+$/.test(cleaned)) return NaN; so these are skipped instead of counted. Another area to polish is your average formatting: Math.round(average.toFixed(1)) first converts to a string and then rounds again, which is confusing—decide whether you want an integer average (e.g., Math.round(average)) or a fixed decimal (e.g., Number(average.toFixed(1))) and use that explicitly to avoid subtle rounding bugs. As a minor robustness improvement, consider checking that .total-population and .average-population elements exist before assigning textContent, so the script fails gracefully if the HTML changes. Overall, your structure and use of modern APIs are strong; tightening up validation and rounding behavior will bring this fully in line with the requirements and make your code more reliable in edge cases.


✨ 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 +7 to +11
const cleaned = rawText.replace(/,/g, '');
if (rawText === '') {
return NaN;
}
const number = Number(cleaned);

Choose a reason for hiding this comment

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

After removing commas you check rawText === '' but not whether cleaned is empty or non-numeric. A span containing only commas (,), whitespace or other non-digit characters will produce cleaned === '' and Number(cleaned) becomes 0 — this violates the requirement to only include strings that can truly be converted to numbers. Validate cleaned before conversion (for example if (cleaned === '') return NaN; or if (!/^[0-9]+$/.test(cleaned)) return NaN;) so only valid numeric strings are converted.

Comment on lines +27 to +28
document.querySelector('.average-population').textContent = formatter.format(
Math.round(average.toFixed(1)),

Choose a reason for hiding this comment

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

Math.round(average.toFixed(1)) is misleading: toFixed returns a string and then Math.round coerces and rounds again. Be explicit about expected precision: use Math.round(average) for integer results, or Number(average.toFixed(1)) to keep one decimal, or configure Intl.NumberFormat with maximumFractionDigits/minimumFractionDigits. This makes the output intention clear and avoids subtle bugs.

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