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
2 changes: 1 addition & 1 deletion 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://aholubko.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 Down
27 changes: 27 additions & 0 deletions src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
'use strict';

// write your code here
const populationNodes = document.querySelectorAll('.population');

const populations = Array.from(populationNodes, (node) => {
const rawText = node.textContent;
const normalized = rawText.replace(/,/g, '').trim();
const n = Number(normalized);

return n;
}).filter((n) => Number.isFinite(n));

const total = populations.reduce((sum, n) => sum + n, 0);

let average;

if (populations.length === 0) {
average = 0;
} else {
average = total / populations.length;
}

const format = (n) =>
n.toLocaleString('en-US', {
maximumFractionDigits: 0,
});

document.querySelector('.total-population').textContent = format(total);
document.querySelector('.average-population').textContent = format(average);
Comment on lines +29 to +30

Choose a reason for hiding this comment

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

This updates only the first matched element because document.querySelector(...) returns a single node. The requirement says to replace Calculate it! in the span element(s) with classes average-population and total-population (plural). Use document.querySelectorAll(...) and iterate over the NodeList to set textContent on every matching element.

Loading