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

add solution #1531

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
16 changes: 16 additions & 0 deletions src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
'use strict';

// write your code here
const populationArray = document.querySelectorAll('.population');
const totalPopulationElement = document.querySelector('.total-population');
const averagePopulationElement = document.querySelector('.average-population');
let totalPopulation = 0;

populationArray.forEach((item) => {
const population = +item.textContent.replaceAll(',', '');

Choose a reason for hiding this comment

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

The use of replaceAll might not be supported in all environments. Consider using replace with a global regular expression if compatibility is a concern.


totalPopulation += population;
});

totalPopulationElement.textContent = totalPopulation.toLocaleString('en-US');

averagePopulationElement.textContent = (
totalPopulation / populationArray.length

Choose a reason for hiding this comment

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

Ensure that populationArray.length is not zero to avoid division by zero errors. Consider adding a check before performing the division.

).toLocaleString('en-US');
Loading