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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
1. Replace `<your_account>` with your Github username in the link
- [DEMO LINK](https://<your_account>.github.io/js_get_data_DOM/)
- [DEMO LINK](https://ValentynaBahyrly.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`

Expand All @@ -13,7 +13,7 @@ Steps to do this challenge:
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.
4. Replace `Calculate it!` with calculated average numbers in `span` with classes `average-population` and `total-population`.
5. Calculated `average` and `total` numbers must be separated by a thousands separator (same numeric style as populations presented).
5. Calculated `average` and `total` numbers must be separated by a tбhousands separator (same numeric style as populations presented).
6. Congrats, mate! You did it!

![Preview](./src/images/preview.png).
1 change: 1 addition & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
content="width=device-width, initial-scale=1.0"
/>
<title>Population top</title>
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 line handles numeric strings with commas well, but there's a subtle issue with non-numeric strings like 'N/A'. After the replace call, 'N/A' becomes an empty string '', and Number('') evaluates to 0.

This 0 is then included when calculating the average, which leads to an incorrect result because it increases the count of numbers (allNumbers.length) without affecting the total sum.

Consider using parseInt() instead of Number(), as parseInt('') returns NaN, which would then be correctly filtered out by your logic on line 14.

<script src="main.js"></script>
<link
rel="stylesheet"
href="./styles/main.scss"
Expand Down
27 changes: 26 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
'use strict';

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

const numbers = [];

for (const population of populations) {
const raw = population.textContent.trim();
const number = Number(raw.replace(/,/g, ''));

numbers.push(number);
}

const allNumbers = numbers.filter((n) => !Number.isNaN(n));
const total = allNumbers.reduce((acc, val) => acc + val, 0);

const average = Math.round(total / allNumbers.length);

const formatter = new Intl.NumberFormat('en-US');

const formattedTotal = formatter.format(total);
const formattedAverage = formatter.format(average);

document.querySelector('.total-population').textContent = formattedTotal;
document.querySelector('.average-population').textContent = formattedAverage;

// console.log(formattedTotal);
// console.log(formattedAverage);
Loading