Skip to content

Commit 129b06f

Browse files
yet-yuvikmtusher97
authored andcommitted
yet-yuvi: Add number table 1-10
1 parent 59b73f1 commit 129b06f

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Multiplication Table Generator</title>
7+
<!-- Tailwind CSS -->
8+
<script src="https://cdn.tailwindcss.com"></script>
9+
</head>
10+
11+
<body class="bg-gray-100">
12+
<div class="bg-white p-6 rounded-lg shadow-lg text-center">
13+
<h1 class="text-2xl font-bold">Multiplication Table Generator</h1>
14+
15+
<!-- Button to generate the multiplication table -->
16+
<button id="generate" class="mt-4 bg-blue-500 text-white py-2 px-4 rounded">Generate Table</button>
17+
18+
<!-- Display area for the multiplication table -->
19+
<div id="table-container" class="flex flex-col gap-4 py-2">
20+
<div id="top-row" class="flex gap-4"></div>
21+
<div id="bottom-row" class="flex gap-4"></div>
22+
</div>
23+
</div>
24+
25+
<!-- Include JavaScript -->
26+
<script src="script.js"></script>
27+
</body>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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

Comments
 (0)