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 #1549

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
32 changes: 31 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
'use strict';

// write your code here
function formatNumber(num) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
Comment on lines +3 to +5

Choose a reason for hiding this comment

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

The formatNumber function is correctly implemented to format numbers with commas as thousand separators. No changes are needed here.


const populationElements = document.querySelectorAll('.population');

Choose a reason for hiding this comment

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

The querySelectorAll method is used correctly to select elements with the class 'population'. Ensure that these elements exist in the HTML to avoid errors.


let totalPopulation = 0;
let count = 0;

populationElements.forEach((element) => {
const populationText = element.textContent.trim();
const populationNumber = parseInt(populationText.replace(/,/g, ''), 10);
Comment on lines +13 to +14

Choose a reason for hiding this comment

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

The code correctly trims whitespace and removes commas from the text content before parsing it as an integer. This ensures that the population numbers are accurately converted.


if (!isNaN(populationNumber) && populationNumber > 0) {
totalPopulation += populationNumber;
count++;
}
Comment on lines +16 to +19

Choose a reason for hiding this comment

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

The condition checks if the parsed population number is valid and positive, which is a good practice to avoid incorrect calculations.

});

const averagePopulation = count > 0 ? totalPopulation / count : 0;

Choose a reason for hiding this comment

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

The calculation of the average population is correct, and the use of a ternary operator ensures that division by zero is avoided.


const averageElements = document.querySelectorAll('.average-population');
const totalElements = document.querySelectorAll('.total-population');
Comment on lines +24 to +25

Choose a reason for hiding this comment

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

Ensure that elements with the classes 'average-population' and 'total-population' exist in the HTML to avoid errors when updating their text content.


averageElements.forEach((element) => {
element.textContent = formatNumber(averagePopulation.toFixed(0));

Choose a reason for hiding this comment

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

The average population is formatted and rounded to the nearest whole number before being displayed, which is appropriate for this context.

});

totalElements.forEach((element) => {
element.textContent = formatNumber(totalPopulation);

Choose a reason for hiding this comment

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

The total population is formatted with commas before being displayed, which improves readability.

});
Loading