From 79ef067f5fa3b7f9e4eec676f91d49a0eebcaa2d Mon Sep 17 00:00:00 2001 From: Anna Pryimak Date: Sun, 21 Jun 2026 20:38:51 +0300 Subject: [PATCH 1/2] add task solution --- README.md | 6 +++--- src/scripts/main.js | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 294f077fb..784b032ce 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://PrimaSB.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..c219c0852 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,3 +1,34 @@ 'use strict'; -// write code here +const listEmployees = document.querySelector('ul'); + +function sortList(list) { + const listElements = list.querySelectorAll('li'); + const elementsArray = [...listElements]; + const sorted = [...elementsArray].sort((a, b) => { + const salaryA = Number(a.dataset.salary.replace(/\D/g, '')); + const salaryB = Number(b.dataset.salary.replace(/\D/g, '')); + + return salaryB - salaryA; + }); + + sorted.forEach((element) => { + list.appendChild(element); + }); +} + +function getEmployees(list) { + const listElements = list.querySelectorAll('li'); + const elementsArray = [...listElements]; + const employees = elementsArray.map((person) => ({ + name: person.dataset.name, + position: person.dataset.position, + salary: Number(person.dataset.salary), + age: Number(person.dataset.age), + })); + + return employees; +} + +sortList(listEmployees); +getEmployees(listEmployees); From 5a72c97c9f86a6e772a7bbe66ea0decfa2b066c8 Mon Sep 17 00:00:00 2001 From: Anna Pryimak Date: Mon, 22 Jun 2026 01:09:25 +0300 Subject: [PATCH 2/2] fixed converting to number logic --- src/scripts/main.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/scripts/main.js b/src/scripts/main.js index c219c0852..4a2389ddc 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -2,12 +2,18 @@ const listEmployees = document.querySelector('ul'); +function toNumber(salary) { + const rawSalary = salary.replace(/\D/g, ''); + + return parseInt(rawSalary); +} + function sortList(list) { const listElements = list.querySelectorAll('li'); const elementsArray = [...listElements]; const sorted = [...elementsArray].sort((a, b) => { - const salaryA = Number(a.dataset.salary.replace(/\D/g, '')); - const salaryB = Number(b.dataset.salary.replace(/\D/g, '')); + const salaryA = toNumber(a.dataset.salary); + const salaryB = toNumber(b.dataset.salary); return salaryB - salaryA; }); @@ -23,8 +29,8 @@ function getEmployees(list) { const employees = elementsArray.map((person) => ({ name: person.dataset.name, position: person.dataset.position, - salary: Number(person.dataset.salary), - age: Number(person.dataset.age), + salary: toNumber(person.dataset.salary), + age: toNumber(person.dataset.age), })); return employees;