Changed around line 1
+ const formulas = [
+ {
+ name: "Area of Rectangle",
+ inputs: ["length", "width"],
+ calculate: (values) => values[0] * values[1]
+ },
+ {
+ name: "Area of Circle",
+ inputs: ["radius"],
+ calculate: (values) => Math.PI * values[0] * values[0]
+ },
+ {
+ name: "Pythagorean Theorem",
+ inputs: ["a", "b"],
+ calculate: (values) => Math.sqrt(values[0] * values[0] + values[1] * values[1])
+ }
+ ];
+
+ function createTableRows() {
+ const tbody = document.querySelector('#formulaTable tbody');
+
+ formulas.forEach((formula, index) => {
+ const tr = document.createElement('tr');
+
+ // Formula name cell
+ const nameCell = document.createElement('td');
+ nameCell.textContent = formula.name;
+
+ // Inputs cell
+ const inputsCell = document.createElement('td');
+ const inputsDiv = document.createElement('div');
+ formula.inputs.forEach((input, i) => {
+ const inputElement = document.createElement('input');
+ inputElement.type = 'number';
+ inputElement.placeholder = input;
+ inputElement.dataset.row = index;
+ inputElement.dataset.index = i;
+ inputElement.addEventListener('input', calculateResult);
+ inputsDiv.appendChild(inputElement);
+ });
+ inputsCell.appendChild(inputsDiv);
+
+ // Result cell
+ const resultCell = document.createElement('td');
+ resultCell.className = 'result';
+ resultCell.id = `result-${index}`;
+
+ tr.appendChild(nameCell);
+ tr.appendChild(inputsCell);
+ tr.appendChild(resultCell);
+ tbody.appendChild(tr);
+ });
+ }
+
+ function calculateResult(event) {
+ const row = event.target.dataset.row;
+ const formula = formulas[row];
+ const inputs = document.querySelectorAll(`input[data-row="${row}"]`);
+ const values = Array.from(inputs).map(input => parseFloat(input.value) || 0);
+
+ const result = formula.calculate(values);
+ const resultCell = document.getElementById(`result-${row}`);
+ resultCell.textContent = isNaN(result) ? '' : result.toFixed(2);
+ }
+
+ document.addEventListener('DOMContentLoaded', createTableRows);