From 3eb41ce1d88d4bf83becb61a6d9c6e39ac9cd093 Mon Sep 17 00:00:00 2001 From: Andrii Zakharenko Date: Tue, 8 Oct 2024 09:32:32 +0300 Subject: [PATCH] add task solution --- README.md | 2 +- src/scripts/main.js | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 384d92ef..8e8a7261 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://AndriiZakharenko.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 c6e3f878..0396d43c 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,3 +1,26 @@ 'use strict'; -// write your code here +const populationElements = document.querySelectorAll('.population'); + +let sum = 0; + +for (const element of populationElements) { + const populationText = element.textContent; + + const populationTextCleaned = populationText.replace(/,/g, ''); + + const populationNumber = parseInt(populationTextCleaned, 10); + + sum += populationNumber; +} + +const totalPopulationElement = document.querySelector('.total-population'); + +totalPopulationElement.textContent = sum.toLocaleString('en-US'); + +const averagePopulation = sum / populationElements.length; + +const averagePopulationElement = document.querySelector('.average-population'); + +averagePopulationElement.textContent = + averagePopulation.toLocaleString('en-US');