diff --git a/src/scripts/main.js b/src/scripts/main.js index c6e3f8784..0d68b92aa 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,3 +1,22 @@ 'use strict'; -// write your code here +const populations = document.querySelectorAll('.population'); + +const totalPopulations = [...populations] + .map((population) => population.textContent.replace(/\D/g, '')) + .map((population) => Number.parseInt(population, 10)) + .filter((population) => !Number.isNaN(population)); + +const total = totalPopulations.reduce((prev, next) => prev + next, 0); + +let average = 0; + +if (totalPopulations.length !== 0) { + average = total / totalPopulations.length; +} + +document.querySelector('.total-population').textContent = + total.toLocaleString(); + +document.querySelector('.average-population').textContent = + average.toLocaleString();