diff --git a/src/scripts/main.js b/src/scripts/main.js index c6e3f8784..1b56f69d3 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,3 +1,36 @@ 'use strict'; // write your code here +const allElement = document.querySelectorAll('.population'); +const numbers = []; + +allElement.forEach((el) => { + const text = el.textContent.replace(/[, ]+/g, ''); + const num = Number(text); + + if (Number.isFinite(num)) { + numbers.push(num); + } +}); + +const total = numbers.reduce((sum, el) => sum + el, 0); +let average = 0; + +if (numbers.length > 0) { + average = Math.round(total / numbers.length); +} + +const totalFormat = total.toLocaleString('en-US'); +const averageFormat = average.toLocaleString('en-US'); + +const totalEl = document.querySelectorAll('.total-population'); + +totalEl.forEach((el) => { + el.textContent = totalFormat; +}); + +const averageEl = document.querySelectorAll('.average-population'); + +averageEl.forEach((el) => { + el.textContent = averageFormat; +});