diff --git a/README.md b/README.md index 42684231e..d1ef0213d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ 1. Replace `` with your Github username in the link - - [DEMO LINK](https://.github.io/js_get_data_DOM/) + - [DEMO LINK](https://Yevhen-Srdk.github.io/js_get_data_DOM/) 2. Follow [this instructions](https://mate-academy.github.io/layout_task-guideline/) - There are no tests for this task so use `npm run lint` command instead of `npm test` diff --git a/src/scripts/main.js b/src/scripts/main.js index c6e3f8784..300eeb3d0 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,3 +1,24 @@ 'use strict'; -// write your code here +const population = document.querySelectorAll('.population'); +let totalPopulationCount = 0; +let isNumberCount = 0; + +for (const item of population) { + const valueNormalization = item.textContent.replace(/[\u00A0,\s]/g, ''); + const value = Number(valueNormalization); + + if (Number.isFinite(value)) { + totalPopulationCount += value; + isNumberCount++; + } +} + +const averagePopulationResult = + isNumberCount > 0 ? Math.round(totalPopulationCount / isNumberCount) : 0; + +const totalPopulation = document.querySelector('.total-population'); +const averagePopulation = document.querySelector('.average-population'); + +totalPopulation.textContent = totalPopulationCount.toLocaleString('en-US'); +averagePopulation.textContent = averagePopulationResult.toLocaleString('en-US');