From a3cb49c2797ac86a7455b78ef12d0c3f31d23bc7 Mon Sep 17 00:00:00 2001 From: BeginerAlex Date: Mon, 9 Sep 2024 18:08:12 +0200 Subject: [PATCH] task solution --- README.md | 4 ++-- src/scripts/main.js | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 36dc318d..4c21110d 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://begineralex.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 10 LARGEST COUNTRIES BY POPULATION diff --git a/src/scripts/main.js b/src/scripts/main.js index c6e3f878..158aae7e 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,3 +1,21 @@ 'use strict'; -// write your code here +const populationElements = document.querySelectorAll('.population'); + +let totalPopulation = 0; + +populationElements.forEach((element) => { + const populationText = element.textContent.trim(); + const populationNumber = parseFloat(populationText.replace(/,/g, '')); + + if (!isNaN(populationNumber)) { + totalPopulation += populationNumber; + } +}); + +const averagePopulation = totalPopulation / populationElements.length; +const totalPopulationElement = document.querySelector('.total-population'); +const averagePopulationElement = document.querySelector('.average-population'); + +totalPopulationElement.textContent = totalPopulation.toLocaleString(); +averagePopulationElement.textContent = averagePopulation.toLocaleString();