-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
add solution #1503
base: master
Are you sure you want to change the base?
add solution #1503
Conversation
an-marryKyslenko
commented
Oct 4, 2024
- DEMO LINK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job 👍
Let's make your code better
src/scripts/main.js
Outdated
// write your code here | ||
const populationCollection = document.querySelectorAll('.population'); | ||
const totalElement = document.querySelector('.total-population'); | ||
const avarangeElement = document.querySelector('.average-population'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const avarangeElement = document.querySelector('.average-population'); | |
const averageElement = document.querySelector('.average-population'); |
src/scripts/main.js
Outdated
|
||
return result + +number; | ||
}, 0); | ||
const avarange = Math.floor(total / populationCollection.length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const avarange = Math.floor(total / populationCollection.length); | |
const average = Math.floor(total / populationCollection.length); |
src/scripts/main.js
Outdated
const avarange = Math.floor(total / populationCollection.length); | ||
|
||
function addComma(str, diviteBy = 3) { | ||
const numberToStirng = typeof str !== 'string' ? str.toString() : str; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const numberToStirng = typeof str !== 'string' ? str.toString() : str; | |
const numberToString = typeof str !== 'string' ? str.toString() : str; |
src/scripts/main.js
Outdated
}, 0); | ||
const avarange = Math.floor(total / populationCollection.length); | ||
|
||
function addComma(str, diviteBy = 3) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function addComma(str, diviteBy = 3) { | |
function addComma(str, divideBy = 3) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! Almost done!
src/scripts/main.js
Outdated
function addComma(str, divideBy = 3) { | ||
const numberToString = typeof str !== 'string' ? str.toString() : str; | ||
const result = []; | ||
|
||
for (let i = 0; i < numberToString.length; ) { | ||
let part = ''; | ||
|
||
if (numberToString.length % divideBy && i === 0) { | ||
part = numberToString.slice(i, i + (numberToString.length % divideBy)); | ||
|
||
result.push(part); | ||
i = i + (numberToString.length % divideBy); | ||
continue; | ||
} | ||
|
||
part = numberToString.slice(i, i + divideBy); | ||
|
||
result.push(part); | ||
i = i + divideBy; | ||
} | ||
|
||
return result.join(','); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function addComma(str, divideBy = 3) { | |
const numberToString = typeof str !== 'string' ? str.toString() : str; | |
const result = []; | |
for (let i = 0; i < numberToString.length; ) { | |
let part = ''; | |
if (numberToString.length % divideBy && i === 0) { | |
part = numberToString.slice(i, i + (numberToString.length % divideBy)); | |
result.push(part); | |
i = i + (numberToString.length % divideBy); | |
continue; | |
} | |
part = numberToString.slice(i, i + divideBy); | |
result.push(part); | |
i = i + divideBy; | |
} | |
return result.join(','); | |
} |
src/scripts/main.js
Outdated
return result.join(','); | ||
} | ||
|
||
totalElement.innerText = addComma(total); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
totalElement.innerText = addComma(total); | |
totalElement.innerText = total.toLocaleString('en-US'); |
src/scripts/main.js
Outdated
} | ||
|
||
totalElement.innerText = addComma(total); | ||
averageElement.innerText = addComma(average ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same thing you can accomplish here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!