-
Notifications
You must be signed in to change notification settings - Fork 1.9k
solution #2021
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?
solution #2021
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,39 @@ | ||
| 'use strict'; | ||
|
|
||
| // write code here | ||
| const list = document.querySelector('ul'); | ||
| const sortList = function (l) { | ||
| const children = [...l.children]; | ||
|
|
||
| children.sort((a, b) => { | ||
| const salaryA = +a.dataset.salary.replace(/[^0-9]/g, ''); | ||
| const salaryB = +b.dataset.salary.replace(/[^0-9]/g, ''); | ||
|
|
||
| return salaryB - salaryA; | ||
| }); | ||
|
|
||
| children.forEach((item) => list.appendChild(item)); | ||
|
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. Here you append sorted items to the global |
||
| }; | ||
| const getEmployees = function (l) { | ||
| const children = [...l.children]; | ||
| const res = []; | ||
| const obj = { | ||
| name: 0, | ||
| position: 0, | ||
| salary: 0, | ||
| age: 0, | ||
| }; | ||
|
Comment on lines
+19
to
+24
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. You define a single |
||
|
|
||
| for (const item of children) { | ||
| obj.name = item.textContent.trim(); | ||
| obj.position = item.dataset.position; | ||
| obj.salary = item.dataset.salary; | ||
| obj.age = item.dataset.age; | ||
|
Comment on lines
+26
to
+30
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. Inside the loop you’re setting |
||
|
|
||
| res.push(obj); | ||
|
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. Because |
||
| } | ||
|
|
||
| return res; | ||
| }; | ||
|
|
||
| getEmployees(list); | ||
| sortList(list); | ||
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.
In
sortList, this correctly sorts bydata-salaryin descending order and converts the salary string to a number; however, consider extracting this parsing logic into a helper function as suggested in the task description so it can be reused ingetEmployeesas well.