Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution #1524

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
14 changes: 13 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
'use strict';

// write your code here
const population = [...document.querySelectorAll('.population')];
const toNumbers = population.map((element) => {
return parseInt(element.textContent.replace(/,/g, ''));
Comment on lines +4 to +5

Choose a reason for hiding this comment

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

The arrow function inside the map method doesn't need to have a return statement since it's a single line of code. You can simplify it to (element) => parseInt(element.textContent.replace(/,/g, '')).

});
Comment on lines +3 to +6

Choose a reason for hiding this comment

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

It's a bit confusing to use the variable name 'population' for an array of DOM elements, and then 'toNumbers' for the actual population numbers. Consider renaming 'population' to something like 'populationElements' or 'populationNodes', and 'toNumbers' to 'populations' or 'populationNumbers'.


const totalSum = toNumbers.reduce((sum, element) => sum + element, 0);

Choose a reason for hiding this comment

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

It's a good practice to provide an initial value for the reduce method, even though in this case, it defaults to the first element of the array when not provided. It makes the code more explicit and helps avoid potential bugs in the future if the code changes.

const average = Math.round(totalSum / toNumbers.length);

document.querySelector('.average-population').textContent =
average.toLocaleString();

document.querySelector('.total-population').textContent =
totalSum.toLocaleString();
Comment on lines +11 to +15

Choose a reason for hiding this comment

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

The code is repeatedly querying the DOM to find the '.average-population' and '.total-population' elements. This can be inefficient, especially if the DOM is large or if this operation is done frequently. Consider storing these elements in variables at the start of the script and reusing those variables.

Loading