From 67615cc641845eb87f8926050a89b6e4f530c1f6 Mon Sep 17 00:00:00 2001 From: Vitalii Date: Sun, 21 Jun 2026 12:18:53 +0300 Subject: [PATCH] solution --- README.md | 6 +++--- src/scripts/main.js | 37 ++++++++++++++++++++++++++++++++++++- src/styles/main.scss | 17 ++++++++++------- 3 files changed, 49 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 294f077fb..dd886631d 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://Tysiachnyk.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..d33b88ebe 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,3 +1,38 @@ 'use strict'; -// write code here +const employeeListElement = document.querySelector('ul'); + +function sortList(list) { + const items = [...list.querySelectorAll('li')]; + + items.sort( + (empl1, empl2) => + parseSalary(empl2.dataset.salary) - parseSalary(empl1.dataset.salary), + ); + + items.forEach((item) => list.appendChild(item)); +} + +function getEmployees(employeeList) { + const items = [...employeeList.querySelectorAll('li')]; + + return items.map((item) => ({ + name: item.textContent.trim(), + position: item.dataset.position, + salary: parseSalary(item.dataset.salary), + age: Number(item.dataset.age), + })); +} + +function parseSalary(salaryStr) { + if (!salaryStr) { + return 0; + } + + const normalizedSalary = Number(salaryStr.slice(1).split(',').join('')); + + return Number.isNaN(normalizedSalary) ? 0 : normalizedSalary; +} + +sortList(employeeListElement); +getEmployees(employeeListElement); diff --git a/src/styles/main.scss b/src/styles/main.scss index e7de1fcf7..42e50663c 100644 --- a/src/styles/main.scss +++ b/src/styles/main.scss @@ -1,13 +1,16 @@ body { - font-family: monospace; - font-size: 24px; - background-color: blanchedalmond; - margin: 0; display: flex; - justify-content: center; + flex-direction: column; align-items: center; + justify-content: center; + min-height: 100vh; - flex-direction: column; + margin: 0; + + font-family: monospace; + font-size: 24px; + + background-color: blanchedalmond; } h1 { @@ -19,6 +22,6 @@ ul { } li:hover { - font-weight: 700; cursor: default; + font-weight: 700; }