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

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

allElement.forEach((el) => {
const text = el.textContent.replace(/[, ]+/g, '');
const num = Number(text);

if (Number.isFinite(num)) {
numbers.push(num);
}
});

const total = numbers.reduce((sum, el) => sum + el, 0);
let average = 0;

if (numbers.length > 0) {
average = Math.round(total / numbers.length);
}

const totalFormat = total.toLocaleString('en-US');
const averageFormat = average.toLocaleString('en-US');

Choose a reason for hiding this comment

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

The task requires the calculated average to have the 'same numeric style as populations presented,' which are whole numbers. Your current implementation might display a number with decimal places. Consider rounding the average value before formatting it to ensure it's a whole number.


const totalEl = document.querySelectorAll('.total-population');

totalEl.forEach((el) => {
el.textContent = totalFormat;
});

const averageEl = document.querySelectorAll('.average-population');

averageEl.forEach((el) => {
el.textContent = averageFormat;
});
Loading