Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- let bts = Array.from(document.querySelectorAll("button"));
- bts.forEach(b => b.addEventListener("click", clicked));
- let boughtField = document.querySelectorAll("textarea")[1];
- function clicked(ev) {
- if (ev.target.textContent == "Generate") {
- let inputField = document.querySelectorAll("textarea")[0];
- let parsed = JSON.parse(inputField.value);
- parsed.forEach(pr => {
- let { name, img, price, decFactor } = pr;
- let htmlStr = `<tr>
- <td><img src="${img}"></td>
- <td><p>${name}</p></td>
- <td><p>${price}</p></td>
- <td><p>${decFactor}</p></td>
- <td><input type="checkbox" /></td>
- </tr>`
- document.querySelector("tbody").innerHTML += htmlStr;
- });
- inputField.value = "";
- }
- if (ev.target.textContent == "Buy") {
- let boxes = document.querySelectorAll('input[type=checkbox]');
- let boughtProdName = [];
- let boughtProdPrice = [];
- let boughtProdFact = [];
- boxes.forEach(ch => {
- // console.log(ch.checked)
- if (ch.checked === true) {
- let rowChecked = ch.parentNode.parentNode;
- let data = rowChecked.querySelectorAll("p");
- boughtProdName.push(data[0].textContent);
- boughtProdPrice.push(Number(data[1].textContent));
- boughtProdFact.push(Number(data[2].textContent));
- }
- });
- let avg = boughtProdFact.reduce((a, b) => a + b,0) / boughtProdFact.length;
- let tot = boughtProdPrice.reduce((a, b) => a += b);
- let res = `Bought furniture: ${boughtProdName.join(", ")}\n`;
- res += `Total price: ${tot.toFixed(2)}\n`;
- res += `Average decoration factor: ${avg}`;
- boughtField.textContent = res;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement