diff --git a/README.md b/README.md index 384d92ef..b8d223a0 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://Stako21.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..21c8140c 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,3 +1,31 @@ -'use strict'; +// 'use strict'; -// write your code here +const sorce = [...document.querySelectorAll('span.population')]; +const population = []; + +sorce.forEach((pop) => { + const popNumber = pop.innerText.replace(/,/gi, ''); + + if (!isNaN(popNumber)) { + population.push(+popNumber); + } +}); + +const totalResult = population.reduce((acc, val) => acc + val); + +const totalResultFormated = totalResult + .toString() + .replace(/\B(?=(\d{3})+(?!\d))/g, ','); + +const averagrResult = + population.reduce((acc, val) => acc + val) / population.length; + +const averagrResultFormated = Math.floor(averagrResult) + .toString() + .replace(/\B(?=(\d{3})+(?!\d))/g, ','); + +document.querySelector('span.total-population').textContent = + totalResultFormated; + +document.querySelector('span.average-population').textContent = + averagrResultFormated;