Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- let tableBody = document.getElementsByTagName('tbody')[0];
- let inputSubmit = document.getElementById('input').querySelector('input[type="submit"');
- let checkBoxes = document.querySelectorAll('input[type="checkbox"]');
- for (let checkBox of checkBoxes) {
- checkBox.disabled = false;
- }
- inputSubmit.addEventListener('click', function (e) {
- e.preventDefault();
- generateFurniture();
- })
- function generateFurniture() {
- let input = document.getElementById('input').querySelector('textarea').value;
- let inputArray = JSON.parse(input);
- for (let object of inputArray) {
- let image = object.img;
- let newImage = document.createElement('img');
- newImage.src = image;
- let name = object.name;
- let newName = document.createElement('p');
- newName.textContent = name;
- let price = object.price;
- let newPrice = document.createElement('p');
- newPrice.textContent = price;
- let decFactor = object.decFactor;
- let newDecFactor = document.createElement('p');
- newDecFactor.textContent = decFactor;
- let newRow = document.createElement('tr');
- let imageCell = document.createElement('td');
- imageCell.appendChild(newImage);
- newRow.appendChild(imageCell);
- let nameCell = document.createElement('td');
- nameCell.appendChild(newName);
- newRow.appendChild(nameCell);
- let priceCell = document.createElement('td');
- priceCell.appendChild(newPrice);
- newRow.appendChild(priceCell);
- let decFactorCell = document.createElement('td');
- decFactorCell.appendChild(newDecFactor);
- newRow.appendChild(decFactorCell);
- let chechBoxCell = document.createElement('td');
- let newCheckInput = document.createElement('input');
- newCheckInput.type = 'checkbox';
- newCheckInput.disabled = false;
- chechBoxCell.appendChild(newCheckInput);
- newRow.appendChild(chechBoxCell);
- tableBody.appendChild(newRow);
- }
- }
- let shopSubmit = document.getElementById('shop').querySelector('input[type="submit"');
- shopSubmit.addEventListener('click', function (e) {
- e.preventDefault();
- generateOutput();
- })
- function generateOutput() {
- let tableRows = Array.from(document.querySelectorAll('tbody tr'));
- let boughtFurniture = [];
- let totalPrice = 0;
- let totalDecFactor = 0;
- for (let row of tableRows) {
- let checkBox = row.querySelector('td:last-child input[type="checkbox"]')
- if (checkBox.checked) {
- totalPrice += Number(row.querySelector('td:nth-of-type(3) p').textContent);
- totalDecFactor += Number(row.querySelector('td:nth-of-type(4) p').textContent);
- boughtFurniture.push(row.querySelector('td:nth-of-type(2) p').textContent);
- }
- }
- let avgDecFactor = (totalDecFactor / boughtFurniture.length);
- let shopTextArea = document.getElementById('shop').querySelector('textarea');
- let result = `Bought furniture: ${boughtFurniture.join(', ')}\nTotal price: ${totalPrice}\nAverage decoration factor: ${avgDecFactor}`
- shopTextArea.value = result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement