|
| 1 | +const generateBtn = document.getElementById("generate"); |
| 2 | +const topRow = document.getElementById("top-row"); |
| 3 | +const bottomRow = document.getElementById("bottom-row"); |
| 4 | + |
| 5 | +// Click event |
| 6 | +generateBtn.addEventListener("click", function () { |
| 7 | + clearTable(); |
| 8 | + |
| 9 | + for (let num = 1; num <= 10; num++) { |
| 10 | + const table = generateTable(num); |
| 11 | + |
| 12 | + if (num <= 5) { |
| 13 | + topRow.appendChild(table); |
| 14 | + } else { |
| 15 | + bottomRow.appendChild(table); |
| 16 | + } |
| 17 | + } |
| 18 | +}); |
| 19 | + |
| 20 | +// generate row |
| 21 | +const generateRow = ({ num, rowNo }) => { |
| 22 | + const tableRow = document.createElement("tr"); |
| 23 | + const cells = []; |
| 24 | + const rowData = [num, " x ", rowNo, " = ", num * rowNo]; |
| 25 | + |
| 26 | + rowData.forEach((data) => { |
| 27 | + const cell = document.createElement("td"); |
| 28 | + cell.innerText = data; |
| 29 | + cells.push(cell); |
| 30 | + }); |
| 31 | + |
| 32 | + tableRow.append(...cells); |
| 33 | + return tableRow; |
| 34 | +}; |
| 35 | + |
| 36 | +// generate table |
| 37 | +function generateTable(number) { |
| 38 | + const table = document.createElement("table"); |
| 39 | + table.className = "table-auto border border-slate-700 w-full"; |
| 40 | + const tableBody = document.createElement("tbody"); |
| 41 | + for (let rowNum = 1; rowNum <= 10; rowNum++) { |
| 42 | + const tRow = generateRow({ num: number, rowNo: rowNum }); |
| 43 | + tableBody.appendChild(tRow); |
| 44 | + } |
| 45 | + table.appendChild(tableBody); |
| 46 | + return table; |
| 47 | +} |
| 48 | + |
| 49 | +// clearing table after one operation |
| 50 | +function clearTable() { |
| 51 | + topRow.innerText = ""; |
| 52 | + bottomRow.innerText = ""; |
| 53 | +} |
0 commit comments