From 1e5cdc75547e518e12ac076ab7c8c0f87ee713d6 Mon Sep 17 00:00:00 2001 From: Anatolii Lutai Date: Mon, 16 Feb 2026 14:42:50 +0200 Subject: [PATCH 1/3] add task solution --- src/scripts/main.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/scripts/main.js b/src/scripts/main.js index c6e3f8784..0031235ca 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,3 +1,27 @@ 'use strict'; -// write your code here +const spanData = document.querySelectorAll('.population'); +const convertedSpanData = Array.from(spanData).map((span) => { + span.textContent = span.textContent.trim().replace(/[,\s]/g, ''); + + if (Number.isNaN(Number(span.textContent))) { + return 0; + } + + const population = Number(span.textContent); + + return population; +}); + +const totalPopulation = convertedSpanData.reduce((acc, population) => { + return acc + population; +}, 0); +const averagePopulation = Math.round( + totalPopulation / convertedSpanData.length, +); + +const totalPopulationElement = document.querySelector('.total-population'); +const averagePopulationElement = document.querySelector('.average-population'); + +totalPopulationElement.textContent = totalPopulation.toLocaleString(); +averagePopulationElement.textContent = averagePopulation.toLocaleString(); From f258080845631afa51b9f05d323dddf3254a0d35 Mon Sep 17 00:00:00 2001 From: Anatolii Lutai Date: Mon, 16 Feb 2026 14:44:33 +0200 Subject: [PATCH 2/3] replace name account in readme.md file --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 42684231e..db071814d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ 1. Replace `` with your Github username in the link - - [DEMO LINK](https://.github.io/js_get_data_DOM/) + - [DEMO LINK](https://A1daros.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` + - There are no tests for this task so use `npm run lint` command instead of `npm test` ### Task: TOP 9 LARGEST COUNTRIES BY POPULATION @@ -9,6 +9,7 @@ Hello! In this task, you need to parse data from the list, and based on it get t You no need to change styles or HTML layout in this task. Change only `main.js` file. Steps to do this challenge: + 1. Get all text data from `span` with class `population` 2. Make sure that the given string can be converted to a number and convert it to number. 3. Calculate average and total value-based to parsed numbers. From 9cc41270a3bf763a47151cab54d77a0c9d5b70d2 Mon Sep 17 00:00:00 2001 From: Anatolii Lutai Date: Mon, 16 Feb 2026 15:02:56 +0200 Subject: [PATCH 3/3] fix: avoid modifying original DOM elements when parsing population data --- src/scripts/main.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/scripts/main.js b/src/scripts/main.js index 0031235ca..4f7e456ba 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -2,13 +2,15 @@ const spanData = document.querySelectorAll('.population'); const convertedSpanData = Array.from(spanData).map((span) => { - span.textContent = span.textContent.trim().replace(/[,\s]/g, ''); + const raw = span.textContent; + const cleaned = raw.trim().replace(/[,\s]/g, ''); + const numValue = Number(cleaned); - if (Number.isNaN(Number(span.textContent))) { + if (Number.isNaN(numValue)) { return 0; } - const population = Number(span.textContent); + const population = numValue; return population; });