Skip to content

Commit

Permalink
added parse list DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
MartaChobaniuk committed Apr 18, 2024
1 parent a6f32f1 commit 951c2cb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
1. Replace `<your_account>` with your Github username in the link
- [DEMO LINK](https://<your_account>.github.io/js_task_parse_list_DOM/)
- [DEMO LINK](https://martachobaniuk.github.io/js_task_parse_list_DOM/)
2. Follow [this instructions](https://mate-academy.github.io/layout_task-guideline/)
- Run `npm run test` command to test your code;
- Run `npm run test:only -- -n` to run fast test ignoring linter;
Expand All @@ -10,14 +10,14 @@
Hey there! Can you parse data from the list and sort it based on data attributes?

Your task: Sort list by salary in descending order.
Get an array of employees. Write two functions:
Get an array of employees. Write two functions:
- first, which sorts the list by salary from data attributes
- second, which returns an array of objects, where objects are employees.

The schema for the employee:
```
{
name,
name,
position,
salary,
age
Expand Down
26 changes: 25 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
'use strict';

// write code here
const listOfEmployees = document.querySelector('ul');
const employees = document.querySelectorAll('li');
const newListOfEmployees = [...employees];

function getSalary(salaryString) {
return Number(salaryString.slice(1).replaceAll(',', ''));
}

function sortList() {
return newListOfEmployees
.sort((a, b) => getSalary(b.dataset.salary) - getSalary(a.dataset.salary))
.forEach((employee) => listOfEmployees.append(employee));
}

function getEmployees() {
return newListOfEmployees.map((employee) => ({
name: employee.innerText,
position: employee.dataset.position,
salary: employee.dataset.salary,
age: employee.dataset.age,
}));
}

sortList();
getEmployees();

0 comments on commit 951c2cb

Please sign in to comment.