Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- /* # configure event listeners # */
- // select all buttons
- // first button -> table generation
- // second button -> buy furniture
- const table = document.querySelector('table.table tbody');
- const [input, output] = Array.from(document.querySelectorAll('textarea'));
- //console.log(input, output);
- const [generateBtn, buyBtn] = Array.from(document.querySelectorAll('button', buy));
- //console.log(generateBtn, buyBtn);
- generateBtn.addEventListener('click', generate);
- buyBtn.addEventListener('click', buy);
- function generate(e) {
- const data = JSON.parse(input.value);
- //console.log(data);
- for (let item of data) {
- const row = document.createElement('tr');
- const imgCell = document.createElement('td');
- const nameCell = document.createElement('td');
- const priceCell = document.createElement('td');
- const decFactorCell = document.createElement('td');
- const imgTd = document.createElement('td');
- const checkCell = document.createElement('td');
- const img = document.createElement('img');
- img.src = item.img;
- console.log(img);
- imgCell.appendChild(img);
- const nameP = document.createElement('p');
- nameP.textContent = item.name;
- nameCell.appendChild(nameP);
- const decP = document.createElement('p');
- decP.textContent = item.decFactor;
- console.log(decP);
- decFactorCell.appendChild(decP);
- const priceP = document.createElement('p');
- priceP.textContent = item.price;
- priceCell.appendChild(priceP);
- const check = document.createElement('input');
- check.type = 'checkbox';
- checkCell.appendChild(check);
- row.appendChild(imgCell);
- row.appendChild(nameCell);
- row.appendChild(priceCell);
- row.appendChild(decFactorCell);
- row.appendChild(checkCell);
- table.appendChild(row);
- }
- }
- /* # table generation */
- // read input JSON and parse it
- // for every array element, create table row
- /* # buy furniture */
- function buy(e) {
- const furniture = Array
- .from(document.querySelector('input[type="checkbox"]:checked'))
- .map(b => b.parentElement.parentElement)
- .map(r => ({
- name: r.children[1].textContent,
- price: Number(r.children[2].textContent),
- decFactor: Number(r.children[3].textContent)
- }));
- let total = 0;
- let decFactor = 0;
- for (let item of furniture) {
- names.push(item.name);
- total += item.price;
- decFactor += item.decFactor;
- }
- const result = `Bought furniture: ${names.join(', ')}
- Total price: ${total.toFixed(2)}
- Average decoration factor: ${decFactor / furniture.length}`;
- output.value = result;
- }
- // select all checkboxes
- // filter only checked checkboxes
- // repeat for every selected checkbox
- // -- select parent row
- // -- read item information
- // display output
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement