From 072166e93c20913de8d803bdcd3918cfc999c27d Mon Sep 17 00:00:00 2001 From: Marko Date: Sun, 25 Oct 2026 16:37:32 +0100 Subject: [PATCH 1/3] solution --- README.md | 6 +++--- src/scripts/main.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 294f077fb..3d71e8003 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ 1. Replace `` with your Github username in the link - - [DEMO LINK](https://.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; @@ -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 diff --git a/src/scripts/main.js b/src/scripts/main.js index a765fdb1d..b67ef1c7b 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,3 +1,38 @@ 'use strict'; // write code here + +const ul = document.querySelector('ul'); +const lists = document.querySelectorAll('li'); + +// console.log({...list[0].dataset}); + +function sortList(list) { + return [...list].sort((a, b) => { + return b.salary - a.salary; + }); +} + +function getEmployees(list) { + return [...list].map((item) => { + return { + ...item.dataset, + salary: +item.dataset.salary.slice(1).replaceAll(',', ''), + name: item.textContent, + }; + }); +} + +ul.innerHTML = ''; + +sortList(getEmployees(lists)).forEach((item) => { + const li = document.createElement('li'); + + li.setAttribute('data-position', item.position); + li.setAttribute('data-salary', `$${item.salary.toLocaleString('en-US')}`); + li.setAttribute('data-age', item.age); + + li.textContent = item.name; + + ul.append(li); +}); From 1d837a859b98131556380c4c70dae19fe71a10ba Mon Sep 17 00:00:00 2001 From: Marko Date: Tue, 27 Oct 2026 16:02:19 +0100 Subject: [PATCH 2/3] fixed error --- src/scripts/main.js | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/scripts/main.js b/src/scripts/main.js index b67ef1c7b..8ff5dd739 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -8,9 +8,23 @@ const lists = document.querySelectorAll('li'); // console.log({...list[0].dataset}); function sortList(list) { - return [...list].sort((a, b) => { - return b.salary - a.salary; - }); + const dataSort = getEmployees(list); + + return [...dataSort] + .sort((a, b) => { + return b.salary - a.salary; + }) + .map((item) => { + return ` +
  • + ${item.name} +
  • + `; + }); } function getEmployees(list) { @@ -25,14 +39,6 @@ function getEmployees(list) { ul.innerHTML = ''; -sortList(getEmployees(lists)).forEach((item) => { - const li = document.createElement('li'); - - li.setAttribute('data-position', item.position); - li.setAttribute('data-salary', `$${item.salary.toLocaleString('en-US')}`); - li.setAttribute('data-age', item.age); - - li.textContent = item.name; - - ul.append(li); +sortList(lists).forEach((item) => { + ul.innerHTML += item; }); From 7efa89ad99478ac1b9c36bbf78f26d88aeaa887d Mon Sep 17 00:00:00 2001 From: Marko Date: Tue, 27 Oct 2026 17:53:01 +0100 Subject: [PATCH 3/3] fixed error --- src/scripts/main.js | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/src/scripts/main.js b/src/scripts/main.js index 8ff5dd739..47bd2e947 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -5,26 +5,19 @@ const ul = document.querySelector('ul'); const lists = document.querySelectorAll('li'); -// console.log({...list[0].dataset}); - function sortList(list) { - const dataSort = getEmployees(list); - - return [...dataSort] - .sort((a, b) => { - return b.salary - a.salary; - }) - .map((item) => { - return ` -
  • - ${item.name} -
  • - `; - }); + 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) { @@ -37,8 +30,10 @@ function getEmployees(list) { }); } -ul.innerHTML = ''; +function conversionToANumber(number) { + return +number.slice(1).replaceAll(',', ''); +} + +getEmployees(lists); -sortList(lists).forEach((item) => { - ul.innerHTML += item; -}); +sortList(lists);