diff --git a/src/index.html b/src/index.html index f0f81a237..34db4a256 100644 --- a/src/index.html +++ b/src/index.html @@ -93,6 +93,9 @@

List of employees

Colleen Hurst - + diff --git a/src/scripts/main.js b/src/scripts/main.js index a765fdb1d..17e49d680 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,3 +1,32 @@ 'use strict'; -// write code here +function parseSalary(salaryStr) { + return Number(salaryStr.replace('$', '').replaceAll(',', '')); +} + +function sortList(list) { + const items = [...list.querySelectorAll('li')]; + + items.sort( + (a, b) => parseSalary(b.dataset.salary) - parseSalary(a.dataset.salary), + ); + items.forEach((item) => list.appendChild(item)); +} + +function getEmployees(list) { + const items = [...list.querySelectorAll('li')]; + + return items.map((item) => ({ + name: item.textContent.trim(), + position: item.dataset.position, + salary: parseSalary(item.dataset.salary), + age: Number(item.dataset.age), + })); +} + +const employeeList = document.querySelector('ul'); + +sortList(employeeList); +getEmployees(employeeList); + +export { parseSalary, sortList, getEmployees };