diff --git a/src/scripts/main.js b/src/scripts/main.js index c6e3f8784..57318f422 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,3 +1,23 @@ 'use strict'; -// write your code here +const populationSpans = document.querySelectorAll('.population'); +const populationArray = Array.from(populationSpans); +const populationNumbers = populationArray.map((span) => { + const text = span.textContent; + const cleanText = text.replace(/[^\d]/g, ''); + const number = Number(cleanText); + + return number; +}); + +const total = populationNumbers.reduce((acc, num) => { + return acc + num; +}, 0); +const average = Math.round(total / populationNumbers.length); +const totalFormatted = total.toLocaleString(); +const averageFormatted = average.toLocaleString(); +const totalSpan = document.querySelector('.total-population'); +const averageSpan = document.querySelector('.average-population'); + +totalSpan.textContent = totalFormatted; +averageSpan.textContent = averageFormatted;