From 4131337488e011c2debaf46496db07751d457c91 Mon Sep 17 00:00:00 2001 From: Yevhen Serdiuk Date: Sun, 8 Feb 2026 18:23:23 +0200 Subject: [PATCH 1/3] add solution --- README.md | 2 +- src/scripts/main.js | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 42684231e..d1ef0213d 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://Yevhen-Srdk.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..44e284f9c 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,3 +1,18 @@ 'use strict'; -// write your code here +const population = document.querySelectorAll('.population'); +let totalPopulationCount = 0; + +for (const item of population) { + totalPopulationCount += Number(item.textContent.replace(/,/g, '')); +} + +const averagePopulationResult = Math.round( + totalPopulationCount / population.length, +); + +const totalPopulation = document.querySelector('.total-population'); +const avaragePopulation = document.querySelector('.average-population'); + +totalPopulation.textContent = totalPopulationCount.toLocaleString('en-US'); +avaragePopulation.textContent = averagePopulationResult.toLocaleString('en-US'); From 4c0664b0f439a32a33297380a40d77b02fc17340 Mon Sep 17 00:00:00 2001 From: Yevhen Serdiuk Date: Sun, 8 Feb 2026 18:35:52 +0200 Subject: [PATCH 2/3] Added number check --- src/scripts/main.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/scripts/main.js b/src/scripts/main.js index 44e284f9c..2310819b2 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -2,17 +2,23 @@ const population = document.querySelectorAll('.population'); let totalPopulationCount = 0; +let isNumberCount = 0; for (const item of population) { - totalPopulationCount += Number(item.textContent.replace(/,/g, '')); + const value = Number(item.textContent.replace(/,/g, '')); + + if (Number.isFinite(value)) { + totalPopulationCount += value; + isNumberCount++; + } } const averagePopulationResult = Math.round( - totalPopulationCount / population.length, + totalPopulationCount / isNumberCount, ); const totalPopulation = document.querySelector('.total-population'); -const avaragePopulation = document.querySelector('.average-population'); +const averagePopulation = document.querySelector('.average-population'); totalPopulation.textContent = totalPopulationCount.toLocaleString('en-US'); -avaragePopulation.textContent = averagePopulationResult.toLocaleString('en-US'); +averagePopulation.textContent = averagePopulationResult.toLocaleString('en-US'); From 97e7f3372a7eb0e507e7b9153406ea88304e8a3e Mon Sep 17 00:00:00 2001 From: Yevhen Serdiuk Date: Sun, 8 Feb 2026 18:49:26 +0200 Subject: [PATCH 3/3] added checks --- src/scripts/main.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/scripts/main.js b/src/scripts/main.js index 2310819b2..300eeb3d0 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -5,7 +5,8 @@ let totalPopulationCount = 0; let isNumberCount = 0; for (const item of population) { - const value = Number(item.textContent.replace(/,/g, '')); + const valueNormalization = item.textContent.replace(/[\u00A0,\s]/g, ''); + const value = Number(valueNormalization); if (Number.isFinite(value)) { totalPopulationCount += value; @@ -13,9 +14,8 @@ for (const item of population) { } } -const averagePopulationResult = Math.round( - totalPopulationCount / isNumberCount, -); +const averagePopulationResult = + isNumberCount > 0 ? Math.round(totalPopulationCount / isNumberCount) : 0; const totalPopulation = document.querySelector('.total-population'); const averagePopulation = document.querySelector('.average-population');