From 5c3674c457d06a90049d5267610743c0bdc955f1 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 8 Sep 2026 15:20:46 +0200 Subject: [PATCH 1/6] added task solution --- README.md | 6 +++--- src/scripts/main.js | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 294f077fb..06eb2257f 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://Marfo0.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..a954a480b 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,3 +1,18 @@ 'use strict'; +function sortList() { +const list = document.querySelector('ul'); +const workers = [...document.querySelectorAll('li[data-salary]')]; + +workers.sort((a, b) => { + const salaryA = Number(a.dataset.salary.slice(1).split(',').join('')); + const salaryB = Number(b.dataset.salary.slice(1).split(',').join('')); + + return salaryB - salaryA; +}); + +workers.forEach((worker) => { + list.append(worker); +}); +} // write code here From f18fad0813f9f82b53a024c0311a2c27a01740d9 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 8 Sep 2026 15:27:34 +0200 Subject: [PATCH 2/6] added 2nd function --- src/scripts/main.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/scripts/main.js b/src/scripts/main.js index a954a480b..51a1e5a5c 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -15,4 +15,19 @@ workers.forEach((worker) => { }); } +function getEmployees() { + const workers = [...document.querySelectorAll('li[data-salary]')]; + + return workers.map((worker) => { + return { + name: worker.textContent.trim(), + position: worker.dataset.position, + salary: Number( + worker.dataset.salary.slice(1).split(',').join('') + ), + age: Number(worker.dataset.age), + }; + }); +} + // write code here From 8fac8827c4f1874183fa70cca7e894b6093dc4c1 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 8 Sep 2026 15:30:09 +0200 Subject: [PATCH 3/6] new task solution --- src/scripts/main.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/scripts/main.js b/src/scripts/main.js index 51a1e5a5c..e75d44f69 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -30,4 +30,7 @@ function getEmployees() { }); } +sortList(); +getEmployees(); + // write code here From 5332b42fbbda6fb2e6b7c14f79e223b49ce0ee30 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 8 Sep 2026 15:38:22 +0200 Subject: [PATCH 4/6] added helper function --- src/scripts/main.js | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/scripts/main.js b/src/scripts/main.js index e75d44f69..beb9b75d2 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,13 +1,15 @@ 'use strict'; -function sortList() { -const list = document.querySelector('ul'); -const workers = [...document.querySelectorAll('li[data-salary]')]; +function getSalary(worker) { + return Number + (worker.dataset.salary.slice(1).split(',').join('') + ); +} -workers.sort((a, b) => { - const salaryA = Number(a.dataset.salary.slice(1).split(',').join('')); - const salaryB = Number(b.dataset.salary.slice(1).split(',').join('')); +function sortList(list) { +const workers = [...list.querySelectorAll('li[data-salary]')]; - return salaryB - salaryA; +workers.sort((a, b) => { + return getSalary(b) - getSalary(a); }); workers.forEach((worker) => { @@ -22,15 +24,18 @@ function getEmployees() { return { name: worker.textContent.trim(), position: worker.dataset.position, - salary: Number( - worker.dataset.salary.slice(1).split(',').join('') - ), + salary: getSalary(worker), age: Number(worker.dataset.age), }; }); } -sortList(); -getEmployees(); +const list = document.querySelector('ul'); + +sortList(list); + +const employees = getEmployees(list); + +console.log(employees); // write code here From 65da09e75478f583caa2f8fe074385a086af9fdb Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 8 Sep 2026 15:39:52 +0200 Subject: [PATCH 5/6] added newest task solution --- src/scripts/main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/scripts/main.js b/src/scripts/main.js index beb9b75d2..a1eafc7fc 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -17,8 +17,8 @@ workers.forEach((worker) => { }); } -function getEmployees() { - const workers = [...document.querySelectorAll('li[data-salary]')]; +function getEmployees(list) { + const workers = [...list.querySelectorAll('li[data-salary]')]; return workers.map((worker) => { return { From e82963c5fcff7c14bec3c6417f04c759c68b8243 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 8 Sep 2026 15:44:29 +0200 Subject: [PATCH 6/6] added new task solution --- src/scripts/main.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/scripts/main.js b/src/scripts/main.js index a1eafc7fc..22ac8b5ef 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -30,12 +30,10 @@ function getEmployees(list) { }); } -const list = document.querySelector('ul'); +const employeesList = document.querySelector('ul'); -sortList(list); +sortList(employeesList); +getEmployees(employeesList); -const employees = getEmployees(list); - -console.log(employees); // write code here