From 711a92d53da8f0cb399f2a21581a1ce79a99fdf4 Mon Sep 17 00:00:00 2001 From: "stan0men@yahoo.com" Date: Thu, 4 Jun 2026 09:32:50 +0300 Subject: [PATCH] Solution --- README.md | 25 ++++++++++++++----------- src/scripts/main.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index a4241d05f..ae741054c 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,35 @@ 1. Replace `` with your Github username in the link - - [DEMO LINK](https://.github.io/js_task_generate_table_DOM/) + - [DEMO LINK](https://stan0men.github.io/js_task_generate_table_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; - - Run `npm run test:only -- -l` to run fast test with additional info in console ignoring linter. + - Run `npm run test` command to test your code; + - Run `npm run test:only -- -n` to run fast test ignoring linter; + - Run `npm run test:only -- -l` to run fast test with additional info in console ignoring linter. ### Task: Generate dashboard from JSON This task requires knowledge of how HTML table works. You can learn it here: - - [MDN HTML table basics](https://developer.mozilla.org/en-US/docs/Learn/HTML/Tables/Basics) + +- [MDN HTML table basics](https://developer.mozilla.org/en-US/docs/Learn/HTML/Tables/Basics) Okay, now we know what is a table, and can do some magic. In `main.js`, you already have imported file `people.json`. Variable `people` contains an array of people, you can check it by using `console.log`. Your task today is to convert this array to table rows. -Your layout for start: +Your layout for start: ![Preview](./src/images/preview.png) From the preview, you can see that table has 6 headers, but our data does not contain age and century. Yes, you need to calculate them by yourself. - + ##### Steps to do this challenge: -1) For each person from `people` array create table row with 6 table cells (name, gender, born, died, age, century) -2) Find a table with class `dashboard` in the document. -3) Append created row to table. -4) Done. + +1. For each person from `people` array create table row with 6 table cells (name, gender, born, died, age, century) +2. Find a table with class `dashboard` in the document. +3. Append created row to table. +4. Done. Hints: + - Age is `person.died - person.born` - Century:divide year of person's death by 100 `Math.ceil(person.died / 100)` diff --git a/src/scripts/main.js b/src/scripts/main.js index 7d4a5db04..0886bbea9 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -358,3 +358,37 @@ const people = [ console.log(people); // you can remove it // write your code here +const table = document.querySelector('table.dashboard'); + +if (table) { + people.forEach((person) => { + const tr = document.createElement('tr'); + + const nameTd = document.createElement('td'); + + nameTd.textContent = person.name; + + const genderTd = document.createElement('td'); + + genderTd.textContent = person.sex === 'm' ? 'Male' : 'Female'; + + const bornTd = document.createElement('td'); + + bornTd.textContent = person.born; + + const diedTd = document.createElement('td'); + + diedTd.textContent = person.died; + + const ageTd = document.createElement('td'); + + ageTd.textContent = person.died - person.born; + + const centuryTd = document.createElement('td'); + + centuryTd.textContent = Math.ceil(person.died / 100); + + tr.append(nameTd, genderTd, bornTd, diedTd, ageTd, centuryTd); + table.appendChild(tr); + }); +}