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

// write code here
// конвертуємо зарплату у число
function getSalaryNumber(el) {
return Number(el.dataset.salary.replace(/[$,]/g, '')); // прибираємо $ і коми
}

// сортування списку у спадному порядку
function sortList(list) {
const items = Array.from(list.children);

items.sort((a, b) => getSalaryNumber(b) - getSalaryNumber(a));
items.forEach((item) => list.appendChild(item)); // оновлюємо DOM
}

// отримання масиву об'єктів співробітників
function getEmployees(list) {
return Array.from(list.children).map((item) => ({
name: item.textContent.trim(), // ім'я з тексту li
position: item.dataset.position,
salary: Number(item.dataset.salary.replace(/[$,]/g, '')),

Choose a reason for hiding this comment

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

You've correctly converted the salary string to a number here. However, you have already created a helper function getSalaryNumber for this exact purpose. To avoid code duplication and make your code more maintainable, consider reusing your helper function here.

age: Number(item.dataset.age),
}));
}

const employeeList = document.querySelector('ul');

if (employeeList) {
sortList(employeeList); // сортуємо список у DOM

const employeesArray = getEmployees(employeeList); // масив об'єктів

Check failure on line 31 in src/scripts/main.js

View workflow job for this annotation

GitHub Actions / build (20.x)

'employeesArray' is assigned a value but never used

Choose a reason for hiding this comment

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

The employeesArray variable is assigned a value but is never used. It's good practice to remove unused variables to keep the code clean.



}
Loading