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
42 changes: 41 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
'use strict';

// write code here
const employeesList = document.querySelector('ul');

const parseSalary = (salaryStr) => {
if (!salaryStr) {
return 0;
}

return Number(salaryStr.replace(/[^0-9.]/g, '')) || 0;
Comment on lines +5 to +10

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This helper for converting salary strings to numbers fits the requirement to write a conversion helper, but it should be used inside a sortList(list) function that sorts actual li elements by their data-salary attribute (checklist items #11 and #13).

};

const sortList = (list) => {
const items = Array.from(list.querySelectorAll('li'));

items.sort((a, b) => {
const salaryA = parseSalary(a.dataset.salary);
const salaryB = parseSalary(b.dataset.salary);

return salaryB - salaryA;
});

items.forEach((item) => list.appendChild(item));
};

const getEmployees = (list) => {
const items = Array.from(list.querySelectorAll('li'));

return items.map((item) => {
const { position, salary, age } = item.dataset;
const employeeName = item.textContent.trim();

return {
name: employeeName,
position,
salary,
age,
};
});
};

sortList(employeesList);
getEmployees(employeesList);
Loading