проблем със задача 7.Furniture от DOM-Manipulations-Exercise
Здравейте,
дали някой би разбрал, защо получавам този отговор от системата:
Unexpected error: The table rows is incorrect: expected 1 to equal 2
това е което съм написал:
function solve() {
let textAreas = Array.from(document.querySelectorAll('textarea'));
let inputArea = textAreas[0];
let infoArea = textAreas[1];
let tableBody = document.querySelector('tbody');
let btns = Array.from(document.querySelectorAll('button'));
let generateBtn = btns[0];
let buyBtn = btns[1];
generateBtn.addEventListener('click', function (e) {
let objects = JSON.parse(inputArea.value);
for (let obj of objects) {
let img = obj.img;
let name = obj.name;
let price = obj.price;
let decFactor = obj.decFactor;
let tr = createDomElement('tr', [
createDomElement('td', [createDomElement('img', '', { src: `${img}` })]),
createDomElement('td', [createDomElement('p', name)]),
createDomElement('td', [createDomElement('p', price)]),
createDomElement('td', [createDomElement('p', decFactor)]),
createDomElement('td', [createDomElement('input', '', { type: 'checkbox' })])
]);
tableBody.appendChild(tr);
}
})
buyBtn.addEventListener('click', function (e) {
let checkboxCheked = Array.from(document.querySelectorAll('input[type=checkbox]')).filter(el => el.checked == true);
if (checkboxCheked.length == 0) { return; }
let products = [];
for (let el of checkboxCheked) {
let currentTr = el.parentElement.parentElement;
let currentP = currentTr.querySelectorAll('p');
let name = currentP[0].innerText;
let price = currentP[1].innerText;
let factor = currentP[2].innerText;
products.push({name, price, factor});
}
let bodthProducts = products.map(el => el.name).join(', ');
let totalPrice = products.reduce((acc, curr) => {
acc += Number(curr.price);
return acc;
}, 0);
let averageFactor = products.reduce((acc, curr) => {
acc += Number(curr.factor);
return acc;
}, 0) / products.length;
let res = [`Bought furniture: ${bodthProducts}`, `Total price: ${totalPrice.toFixed(2)}`, `Average decoration factor: ${averageFactor}`];
infoArea.value = res.join('\n');
console.log(document.querySelectorAll('tbody tr').length)
})
function createDomElement(type, node, attributes) {
let el = document.createElement(type);
if (attributes !== undefined) {
Object.assign(el, attributes);
}
if (Array.isArray(node)) {
node.forEach(e => appendElement(e));
} else {
appendElement(node);
}
function appendElement(currentNode) {
if (typeof currentNode === 'string') {
el.innerText = currentNode;
return;
}
el.appendChild(currentNode);
} // end appendElement;
return el;
} // end createDomElement;
}
Благодаря!