-
Notifications
You must be signed in to change notification settings - Fork 2k
added solution #2188
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
base: master
Are you sure you want to change the base?
added solution #2188
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,34 @@ | ||
| 'use strict'; | ||
|
|
||
| // write your code here | ||
| const spanPopulation = [...document.querySelectorAll('.population')]; | ||
| const averagePopulation = document.querySelector('.average-population'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No null-check for |
||
| const totalPopulation = document.querySelector('.total-population'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No null-check for |
||
| const total = spanPopulation.reduce( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This reduce sums every span value without validating conversion to a finite number. Checklist #3 requires verifying strings are convertible before using them — instead, build an array of parsed numbers (parse + filter with Number.isFinite) and reduce that array. |
||
| (sum, n) => sum + Number(n.textContent.trim().split(',').join('')), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cleaning here uses split/join to remove commas. Use a single replace to remove commas and whitespace (including NBSP) before Number(), e.g. |
||
| 0, | ||
| ); | ||
|
Comment on lines
6
to
9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Comment on lines
6
to
9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reduce-based summation here uses |
||
| const average = Math.round(total / spanPopulation.length); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Average is computed as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Average is computed as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Average is calculated using |
||
|
|
||
| function convertNumbers(number) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| const resultNumber = []; | ||
| const numberToString = String(number); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| for (let i = numberToString.length; i > 0; i -= 3) { | ||
| if (i < 3) { | ||
| resultNumber.unshift(numberToString.slice(0, i)); | ||
|
|
||
| break; | ||
| } | ||
| resultNumber.unshift(numberToString.slice(i - 3, i)); | ||
| } | ||
|
|
||
| return resultNumber.join(','); | ||
| } | ||
|
|
||
| if (averagePopulation) { | ||
| averagePopulation.textContent = convertNumbers(average); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| if (totalPopulation) { | ||
| totalPopulation.textContent = convertNumbers(total); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also check the |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| @font-face { | ||
| font-family: Roboto, Arial, Helvetica, sans-serif; | ||
| src: url('../fonts/Roboto-Regular-webfont.woff') format('woff'); | ||
| font-weight: normal; | ||
| font-style: normal; | ||
| src: url('../fonts/Roboto-Regular-webfont.woff') format('woff'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure the font file exists at the given relative path; if the file is missing the @font-face rule will fail and the custom font won't load. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,19 @@ | ||
| @import 'fonts'; | ||
|
|
||
| body { | ||
| background: #eee; | ||
| counter-reset: section; | ||
|
|
||
| display: flex; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| margin: 0; | ||
| align-items: center; | ||
| justify-content: center; | ||
|
|
||
| width: 100vw; | ||
| height: 100vh; | ||
| justify-content: center; | ||
| align-items: center; | ||
| margin: 0; | ||
|
|
||
| font-family: Roboto, sans-serif; | ||
| counter-reset: section; | ||
|
|
||
| background: #eee; | ||
| } | ||
|
|
||
| h1 { | ||
|
|
@@ -18,28 +22,32 @@ h1 { | |
|
|
||
| .list { | ||
| margin: 32px 0; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Further style rules were edited here (e.g. list and spacing). Reverting the entire styles folder (or this file) to the original will ensure the submission follows the task constraint to modify only main.js. |
||
| list-style: none; | ||
| padding: 0; | ||
| list-style: none; | ||
| &__item { | ||
| margin: 8px; | ||
| border-bottom: 1px solid darkgrey; | ||
| position: relative; | ||
|
|
||
| display: flex; | ||
| justify-content: space-between; | ||
| padding: 4px 4px 4px 30px; | ||
| position: relative; | ||
|
|
||
| img { | ||
| margin-right: 8px; | ||
| } | ||
| margin: 8px; | ||
| padding: 4px 4px 4px 30px; | ||
| border-bottom: 1px solid darkgrey; | ||
|
|
||
| &::before { | ||
| color: #565754; | ||
| counter-increment: section; | ||
| content: counter(section); | ||
| counter-increment: section; | ||
|
|
||
| position: absolute; | ||
| top: 50%; | ||
| left: 5px; | ||
| transform: translateY(-60%); | ||
|
|
||
| color: #565754; | ||
| } | ||
|
|
||
| img { | ||
| margin-right: 8px; | ||
| } | ||
| } | ||
| } | ||
|
|
||
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.
Selecting the elements this way is okay, but when you later read values prefer
textContent(and.trim()) instead ofinnerHTMLto get raw text without accidental HTML or extra whitespace.