Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
1. Replace `<your_account>` with your Github username in the link
- [DEMO LINK](https://<your_account>.github.io/js_get_data_DOM/)
1. Replace TIgor22 with your Github username in the link
- [DEMO LINK](https://TIgor22.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

Hello! In this task, you need to parse data from the list, and based on it get the average and total value.
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.
Expand Down
22 changes: 21 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
'use strict';

// write your code here
const population = document.querySelectorAll('.population');
const populationList = [...population];
const result = [];

populationList.forEach((element) => {
const raw = element.textContent.trim();
const cleaned = raw.replace(/\s+/g, '').replace(/,/g, '');
const num = Number(cleaned);

result.push(num);
});
Comment on lines +7 to +13
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop correctly processes the population data. However, requirement #2 asks you to 'Make sure that the given string can be converted to a number'. What would happen if element.textContent contained non-numeric text? The num variable would become NaN, which would cause total and average to also be NaN. It's a good practice to add a check to ensure num is a valid number before adding it to the result array.


const nums = result.filter((num) => Number.isFinite(num));
const total = nums.reduce((sum, people) => sum + people, 0);
const average = nums.length ? Math.round(total / nums.length) : 0;
const totalPopulation = document.querySelector('.total-population');
const averagePopulation = document.querySelector('.average-population');
const fmt = (v) => new Intl.NumberFormat('en-US').format(v);

totalPopulation.textContent = fmt(total);
averagePopulation.textContent = fmt(average);
53 changes: 0 additions & 53 deletions src/styles/main.scss

This file was deleted.

Loading