diff --git a/src/scripts/main.js b/src/scripts/main.js index c6e3f8784..b5cc5a478 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,3 +1,28 @@ 'use strict'; -// write your code here +const populationElements = document.querySelectorAll('.population'); + +let total = 0; + +populationElements.forEach(function (element) { + const text = element.textContent; + + // видаляємо ВСЕ крім цифр + const cleanText = text.replace(/[^\d]/g, ''); + + const number = Number(cleanText); + + if (!isNaN(number)) { + total += number; + } +}); + +const average = populationElements.length + ? total / populationElements.length + : 0; + +const formattedTotal = total.toLocaleString(); +const formattedAverage = Math.round(average).toLocaleString(); + +document.querySelector('.total-population').textContent = formattedTotal; +document.querySelector('.average-population').textContent = formattedAverage;