Skip to content
Open

sol #2234

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

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

total += value;
}

return total;
}

function calculateAveragePopulation(totalValue, populations) {
const sourceCount = populations.length;
Comment thread
leemonch1k-space marked this conversation as resolved.
Outdated

return totalValue / sourceCount;
}

const total = CalculateTotalPopulation();
const average = calculateAveragePopulation(total, values);

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

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