diff --git a/README.md b/README.md index 384d92ef..8e8a7261 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://AndriiZakharenko.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 c6e3f878..0396d43c 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,3 +1,26 @@ 'use strict'; -// write your code here +const populationElements = document.querySelectorAll('.population'); + +let sum = 0; + +for (const element of populationElements) { + const populationText = element.textContent; + + const populationTextCleaned = populationText.replace(/,/g, ''); + + const populationNumber = parseInt(populationTextCleaned, 10); + + sum += populationNumber; +} + +const totalPopulationElement = document.querySelector('.total-population'); + +totalPopulationElement.textContent = sum.toLocaleString('en-US'); + +const averagePopulation = sum / populationElements.length; + +const averagePopulationElement = document.querySelector('.average-population'); + +averagePopulationElement.textContent = + averagePopulation.toLocaleString('en-US');