-
Notifications
You must be signed in to change notification settings - Fork 2k
completed the task #2187
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?
completed the task #2187
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,31 @@ | ||
| 'use strict'; | ||
|
|
||
| // write your code here | ||
| const population = document.querySelectorAll('.population'); | ||
| const averagePopulation = document.querySelector('.average-population'); | ||
| const totalPopulation = document.querySelector('.total-population'); | ||
| const newPopulation = [...document.querySelectorAll('.population')]; | ||
| let totalPopulationResult = 0; | ||
|
|
||
| for (const onePopulation of newPopulation) { | ||
| totalPopulationResult += Number(onePopulation.innerText.split(',').join('')); | ||
| } | ||
|
|
||
| const newTotalPopulationResult = String(totalPopulationResult).split(''); | ||
|
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. This logic for adding thousands separators is effective, but it's repeated again on lines 23-28. To avoid code duplication, consider creating a reusable function for this task. You could also look into the standard |
||
| const resultTotal = []; | ||
|
|
||
| while (newTotalPopulationResult.length) { | ||
| resultTotal.unshift(newTotalPopulationResult.splice(-3).join('')); | ||
| } | ||
|
|
||
| const averagePopulationResult = Math.round( | ||
| totalPopulationResult / population.length, | ||
| ); | ||
| const newAveragePopulationResul = String(averagePopulationResult).split(''); | ||
|
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. There's a small typo in this variable name. It should probably be |
||
| const resultAverage = []; | ||
|
|
||
| while (newAveragePopulationResul.length) { | ||
| resultAverage.unshift(newAveragePopulationResul.splice(-3).join('')); | ||
| } | ||
|
|
||
| totalPopulation.innerText = resultTotal.join(','); | ||
| averagePopulation.innerText = resultAverage.join(','); | ||
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.
This line queries the DOM for the same elements as on line 3. It's more efficient to query the DOM only once and reuse the resulting
NodeList. Thepopulationvariable from line 3 can be used directly in thefor...ofloop on line 9.