From 291ac220df798c97ec0a48e5d37bf7b74a5a29a4 Mon Sep 17 00:00:00 2001 From: Inna K Date: Fri, 24 Apr 2026 20:18:42 +0300 Subject: [PATCH] add task solution --- src/scripts/main.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) 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;