Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[004] Add number table generator #90

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
32 changes: 32 additions & 0 deletions 004-number-table/bongodev/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Multiplication Table Generator</title>
<!-- Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>
</head>

<body class="bg-gray-100 h-screen flex items-center justify-center">
<div class="bg-white p-6 rounded-lg shadow-lg text-center">
<h1 class="text-2xl font-bold mb-4">Multiplication Table Generator</h1>

<!-- Input field for the number -->
<input type="number" id="input-number" placeholder="Enter a number" class="border p-2 rounded w-full" value="1" />

<!-- Button to generate the multiplication table -->
<button id="generate" class="mt-4 bg-blue-500 text-white py-2 px-4 rounded">Generate Table</button>

<!-- Display area for the multiplication table -->
<div id="table-container" class="mt-4">
<table class="table-auto border border-slate-700 w-full">
<tbody id="table-body">
</tbody>
</table>
</div>
</div>

<!-- Include JavaScript -->
<script src="multiplication-table.js"></script>
</body>
43 changes: 43 additions & 0 deletions 004-number-table/bongodev/multiplication-table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const numberInput = document.getElementById('input-number');
const generateBtn = document.getElementById('generate');
const tableBody = document.getElementById('table-body');

generateBtn.addEventListener('click', function () {
const num = parseInt(numberInput.value);

cleanTable();
generateTable(num);
});

function cleanTable() {
tableBody.innerHTML = '';
}

function generateTable(num) {
for (let rowNo = 1; rowNo <= 10; rowNo++) {
const tableRow = generateRow(num, rowNo);
tableBody.appendChild(tableRow);
}
}

function generateRow(num, rowNo) {
const cells = [];
for (let i = 1; i <= 5; i++) {
const cell = document.createElement('td');
cells.push(cell);
}

cells[0].innerText = num;
cells[1].innerText = ' x ';
cells[2].innerText = rowNo;
cells[3].innerText = ' = ';
cells[4].innerText = num * rowNo;

const tableRow = document.createElement('tr');

for (let i = 0; i < cells.length; i++) {
tableRow.appendChild(cells[i]);
}

return tableRow;
}
33 changes: 33 additions & 0 deletions 004-number-table/sumiya-yasmin/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Multiplication Table Generator</title>
<!-- Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>
</head>

<body class="bg-gray-100 h-screen flex items-center justify-center">
<div class="bg-white p-6 rounded-lg shadow-lg text-center">
<h1 class="text-2xl font-bold mb-4">Multiplication Table Generator</h1>

<!-- Input field for the number -->
<input type="number" id="input-number" placeholder="Enter a number" class="border p-2 rounded w-full" />

<!-- Button to generate the multiplication table -->
<button id="generate" class="mt-4 bg-green-500 text-white py-2 px-4 rounded">Generate Table</button>
<!-- Display area for the multiplication table -->
<div id="table-container" class="mt-4">
<p id="error-text"></p>
<p id="title"></p>
<table class="table-auto border border-slate-700 w-full">
<tbody id="table-body">
</tbody>
</table>
</div>
<button id="reset" class="mt-4 bg-blue-500 text-white py-2 px-4 rounded">Reset</button>
</div>
<!-- Include JavaScript -->
<script src="multiplication-table.js"></script>
</body>
75 changes: 75 additions & 0 deletions 004-number-table/sumiya-yasmin/multiplication-table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const inputNumber = document.getElementById("input-number");
const generateButton = document.getElementById("generate");
const tableBody = document.getElementById("table-body");
const errorText = document.getElementById("error-text");
const titleText = document.getElementById("title");
const resetButton = document.getElementById("reset");
const ERROR_CLASS = "border-red-500";
const ERROR_TEXT = "text-red-600";

function resetErrorStyles() {
titleText.classList.add("hidden");
errorText.classList.add("hidden");
inputNumber.classList.remove(ERROR_CLASS);
}
function isValidInput() {
if (!inputNumber.value) {
return false;
}
return true;
}
generateButton.addEventListener("click", function () {
if (!isValidInput()) {
cleanTable();
inputNumber.classList.add(ERROR_CLASS);
errorText.classList.remove("hidden");
titleText.classList.add("hidden");
errorText.classList.add(ERROR_TEXT);
document.getElementById("error-text");
errorText.innerText = "Your input is empty. Enter a valid Number";
return;
}
const num = parseInt(inputNumber.value);
cleanTable();
resetErrorStyles();
titleText.classList.remove("hidden");
titleText.classList.add("text-bold", "text-black", "bg-green-200", "mb-2");
titleText.innerText = `Multiplication Table of ${inputNumber.value}`;
generateTable(num);
});

function cleanTable() {
tableBody.innerHTML = "";
}
function generateTable(num) {
for (let mult = 1; mult <= 10; mult++) {
const tableRow = generateRow(num, mult);
tableBody.appendChild(tableRow);
tableBody.classList.add("bg-gray-100");
}
}

function generateRow(num, mult) {
const cells = [];
for (let i = 1; i <= 5; i++) {
const cell = document.createElement("td");
cells.push(cell);
}

cells[0].innerText = num;
cells[1].innerText = " x ";
cells[2].innerText = mult;
cells[3].innerText = "=";
cells[4].innerText = num * mult;

const tableRow = document.createElement("tr");
for (let i = 0; i < cells.length; i++) {
tableRow.appendChild(cells[i]);
}
return tableRow;
}
resetButton.addEventListener("click", function () {
inputNumber.value = "";
tableBody.innerHTML = "";
resetErrorStyles();
});