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
38 changes: 37 additions & 1 deletion src/scripts/main.js
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, '');
Comment on lines +7 to +9

Copy link
Copy Markdown

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 by data-salary in 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 in getEmployees as well.


return salaryB - salaryA;
});

children.forEach((item) => list.appendChild(item));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Here you append sorted items to the global list variable instead of the l parameter passed to sortList. While this works with the current code (since l and list refer to the same element), it is more robust and in line with the function signature to use l.appendChild(item) so the function behaves correctly for any list passed in.

};
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You define a single obj outside the loop and then mutate it for each child; this means every element in res will reference the same object, ending up with identical data (from the last item). You need to create a new employee object inside the loop for each item.


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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Inside the loop you’re setting obj.salary directly from item.dataset.salary, which is a string. The requirements state salary from the data attribute must be converted to a number (ideally using a helper function). Update this to parse the salary to a numeric value, similar to how you do in sortList.


res.push(obj);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Because obj is reused, each res.push(obj) just adds another reference to the same object rather than a distinct employee entry. After you move object creation inside the loop, res.push will correctly add separate objects for each employee.

}

return res;
};

getEmployees(list);
sortList(list);
Loading