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

// write code here

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

function convertToNumber(value) {
if (!isNaN(Number(value.replace('$', '').replaceAll(',', '')))) {
return Number(value.replace('$', '').replaceAll(',', ''));
}
}

function sortList(list) {
const sorted = [...list.querySelectorAll('li')].sort((a, b) => {
return (
convertToNumber(b.dataset.salary) - convertToNumber(a.dataset.salary)
);
});

sorted.forEach((employee) => list.append(employee));
}

function getEmployees(list) {
const employees = [...list.querySelectorAll('li')].map((employee) => {
Comment on lines +23 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.

The function takes list as a parameter but uses listOfEmployees directly instead. This defeats the purpose of the parameter. Change listOfEmployees.querySelectorAll('li') to list.querySelectorAll('li').

return {
name: employee.innerText,
position: employee.dataset.position,
salary: convertToNumber(employee.dataset.salary),
age: employee.dataset.age,
};
});

return employees;
}

sortList(listOfEmployees);
getEmployees(listOfEmployees);
Loading