From 87e6a2395c7648969bc5e7f55e5c45739b9ab6ca Mon Sep 17 00:00:00 2001 From: Anastasiia Holubko Date: Wed, 11 Feb 2026 00:15:59 +0100 Subject: [PATCH] Solution --- README.md | 2 +- src/scripts/main.js | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 42684231e..8cb8ec014 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ 1. Replace `` with your Github username in the link - - [DEMO LINK](https://.github.io/js_get_data_DOM/) + - [DEMO LINK](https://aholubko.github.io/js_get_data_DOM/) 2. Follow [this instructions](https://mate-academy.github.io/layout_task-guideline/) - There are no tests for this task so use `npm run lint` command instead of `npm test` diff --git a/src/scripts/main.js b/src/scripts/main.js index c6e3f8784..dbcf4626f 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,3 +1,30 @@ 'use strict'; // write your code here +const populationNodes = document.querySelectorAll('.population'); + +const populations = Array.from(populationNodes, (node) => { + const rawText = node.textContent; + const normalized = rawText.replace(/,/g, '').trim(); + const n = Number(normalized); + + return n; +}).filter((n) => Number.isFinite(n)); + +const total = populations.reduce((sum, n) => sum + n, 0); + +let average; + +if (populations.length === 0) { + average = 0; +} else { + average = total / populations.length; +} + +const format = (n) => + n.toLocaleString('en-US', { + maximumFractionDigits: 0, + }); + +document.querySelector('.total-population').textContent = format(total); +document.querySelector('.average-population').textContent = format(average);