-
Notifications
You must be signed in to change notification settings - Fork 2.1k
add population calculation logic #2243
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
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,16 @@ | ||
| 'use strict'; | ||
|
|
||
| // write your code here | ||
| const populations = document.querySelectorAll('.population'); | ||
|
|
||
| const numbers = Array.from(populations) | ||
| .map((span) => span.textContent.replace(/,/g, '').trim()) | ||
| .map(Number); | ||
|
|
||
| const total = numbers.reduce((sum, num) => sum + num, 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. Reducing an array that may contain NaN values will produce NaN for the total. Make sure to filter the |
||
| const average = numbers.length ? total / numbers.length : 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. Average is computed using |
||
|
|
||
| document.querySelector('.total-population').textContent = | ||
| total.toLocaleString('en-US'); | ||
|
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. You format numbers with |
||
|
|
||
| document.querySelector('.average-population').textContent = | ||
| Math.round(average).toLocaleString('en-US'); | ||
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.
You convert span text to Number here but do not validate the result. The task requires ensuring each string can be converted to a number; if conversion fails you'll get NaN. Consider filtering out non-numeric entries (e.g., check Number.isFinite) or handle invalid values explicitly before further processing.