Skip to content
Open
Show file tree
Hide file tree
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
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://MarcelloTar.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
36 changes: 36 additions & 0 deletions src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
'use strict';

// write code here

const ul = document.querySelector('ul');
const lists = document.querySelectorAll('li');

function sortList(list) {

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 sortList function should take a DOM list (li elements) as parameter and sort them by extracting salary from data attributes, not receive an already-processed array. Per requirements: 'takes a list property, and sorts list items in descending order by salary from data attribute'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

sortList should receive a DOM list (like ul element) and sort li elements directly by accessing dataset.salary, not call getEmployees() to get an array. The task requires sorting DOM elements, not creating new HTML strings.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Missing required standalone helper function to convert salary string to number. The task explicitly requires a helper function for this conversion that sortList should use.

const items = [...list].sort((a, b) => {
const ASalary = conversionToANumber(a.dataset.salary);
const BSalary = conversionToANumber(b.dataset.salary);

return BSalary - ASalary;
});

ul.innerHTML = '';

items.forEach((item) => {
ul.append(item);
});
}

function getEmployees(list) {
return [...list].map((item) => {
return {
...item.dataset,
salary: +item.dataset.salary.slice(1).replaceAll(',', ''),
name: item.textContent,
Comment on lines +23 to +28

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 name property should come from item.dataset.name, not item.textContent. Using textContent would include all text content from nested elements within the <li>, not just the name.

};
});
Comment on lines +23 to +30

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 getEmployees function spreads all dataset properties which may include extra fields. The schema should only contain { name, position, salary, age }. Also, the function should take a DOM list parameter, not an array.

}

function conversionToANumber(number) {
return +number.slice(1).replaceAll(',', '');
}

getEmployees(lists);

sortList(lists);
Loading