Skip to content
Open

sol #2234

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: 32 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
'use strict';

// write your code here
const values = document.getElementsByClassName('population');
const totalPopulation = document.querySelector('.total-population');
const averagePopulation = document.querySelector('.average-population');

function CalculateTotalPopulation() {
// eslint-disable-next-line no-shadow
let total = 0;
let sourceCount = 0;

for (const population of values) {
const textValue = population.textContent.replaceAll(',', '');
const value = Number(textValue);
Comment thread
leemonch1k-space marked this conversation as resolved.

if (!isNaN(value)) {
total += value;
sourceCount += 1;
}
}

return { total, sourceCount };
}

function calculateAveragePopulation(totalValue, sourceCount) {
return Math.round(totalValue / sourceCount);
}

const total = CalculateTotalPopulation();
const average = calculateAveragePopulation(total.total, total.sourceCount);

totalPopulation.textContent = total.total.toLocaleString('en-US');

averagePopulation.textContent = average.toLocaleString('en-US');
Loading