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
22 changes: 21 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
'use strict';
const populations = document.querySelectorAll('.population');
const totalPopulation = document.querySelector('.total-population');
const averagePopulation = document.querySelector('.average-population');

// write your code here
let values = [];
let sum = 0;
let average = 0;

populations.forEach((element, index)=> {
values.push(element.textContent);
values[index] = Number(values[index].split(',').join(''))
Comment on lines +11 to +12

Choose a reason for hiding this comment

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

These two lines can be combined into one for better readability. You are pushing the text content to the values array and then immediately overwriting that same element with its numeric version. It would be more direct to parse the number from element.textContent and push the result into the values array in a single step.

});

console.log(values);

Check failure on line 16 in src/scripts/main.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected console statement
sum = values.reduce((sum, el) => sum + el, 0);

Check failure on line 18 in src/scripts/main.js

View workflow job for this annotation

GitHub Actions / build (20.x)

'sum' is already declared in the upper scope on line 8 column 5
average = Math.round(sum / values.length);

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

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