-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
base: master
Are you sure you want to change the base?
add solution #1549
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, ','); | ||
} | ||
|
||
const populationElements = document.querySelectorAll('.population'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
}); |
There was a problem hiding this comment.
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.