Skip to content
Open
Changes from 1 commit
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
17 changes: 17 additions & 0 deletions src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
'use strict';

// write your code here

const countriesPopulation = document.querySelectorAll('.population');
const totalPopulationEl = document.querySelector('.total-population');
const averagePopulationEl = document.querySelector('.average-population');

let totalPopulation = 0;

countriesPopulation.forEach((countryPopulation) => {
totalPopulation += Number(countryPopulation.innerText.replaceAll(',', ''));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The task requires validating that the string can be converted to a number before conversion. Currently, if Number() returns NaN (due to invalid text), the entire calculation becomes NaN. Consider using isNaN() check or parseInt() with validation to ensure data integrity.

});

const averagePopulation = totalPopulation / countriesPopulation.length;

totalPopulationEl.innerText = totalPopulation.toLocaleString();
averagePopulationEl.innerText = averagePopulation.toLocaleString('en-GB', {
maximumFractionDigits: 0,
});
Loading