diff --git a/README.md b/README.md index 42684231e..945e0681b 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://pavloPolyvoda-dev.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..66c99bdf6 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,3 +1,26 @@ 'use strict'; -// write your code here +const populationList = document.querySelectorAll('.population'); +const averagePopulation = document.querySelector('.average-population'); +const totalPopulation = document.querySelector('.total-population'); + +const populationArray = []; + +for (const populationItem of populationList) { + const populationValue = populationItem.textContent.split(',').join(''); + + populationArray.push(populationValue); +} + +const sumPopNum = populationArray.reduce((sum, population) => { + return sum + Number(population); +}, 0); + +const averagePopNum = sumPopNum / populationArray.length; + +totalPopulation.textContent = sumPopNum.toLocaleString('en-US'); + +averagePopulation.textContent = averagePopNum.toLocaleString('en-US', { + minimumFractionDigits: 2, + maximumFractionDigits: 2, +});