Skip to content
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

Solution #1528

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
'use strict';

// write your code here
const populationElements = document.querySelectorAll('.population');

const populations = Array.from(populationElements).map((element) => {
// Удаляем запятые из строки населения и преобразуем в число
return parseInt(element.textContent.replace(/,/g, ''), 10);
});

const totalPopulation = populations.reduce((acc, curr) => acc + curr, 0);

const averagePopulation = totalPopulation / populations.length;

const totalPopulationElement = document.querySelector('.total-population');
const averagePopulationElement = document.querySelector('.average-population');

// Прямо вставляем числа с разделителями тысяч, используя toLocaleString()
totalPopulationElement.innerText = totalPopulation.toLocaleString();

const roundedAverage = Math.round(averagePopulation);

averagePopulationElement.innerText = addThousandSeparators(roundedAverage);

Choose a reason for hiding this comment

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

You're using a custom function addThousandSeparators to format the average population number, but you've used toLocaleString for the total population. It would be more consistent to use toLocaleString for both, as it's a built-in function that handles number localization.


function addThousandSeparators(number) {
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
Loading