Quadratic Equation Solver
Results will be displayed here.
Root (x₁): ${resultData.roots[0]}
`; resultsDiv.innerHTML += `Root (x₂): ${resultData.roots[1]}
`; } solutionData = resultData; // Store for PDF downloadBtn.style.display = 'inline-block'; // Show download button } // --- Generate and Download PDF --- function downloadPDF() { if (!solutionData) return; const doc = new jsPDF(); const { a, b, c, discriminant, rootType, roots } = solutionData; // --- Get Colors from CSS Variables --- const styles = getComputedStyle(document.querySelector('.qes-solver-container')); const primaryColor = styles.getPropertyValue('--qes-primary-color').trim(); const textColor = styles.getPropertyValue('--qes-text-color').trim(); const resultsBg = styles.getPropertyValue('--qes-results-bg').trim(); // const errorColor = styles.getPropertyValue('--qes-error-color').trim(); // Not needed directly for success PDF // --- PDF Content --- const lineHeight = 7; const margin = 15; let currentY = 20; // Title doc.setFontSize(18); doc.setTextColor(primaryColor); doc.text("Quadratic Equation Solver Results", doc.internal.pageSize.getWidth() / 2, currentY, { align: 'center' }); currentY += lineHeight * 2; // Input Equation doc.setFontSize(12); doc.setTextColor(textColor); doc.setFont("helvetica", "bold"); doc.text("Input Equation:", margin, currentY); doc.setFont("courier", "normal"); // Use monospace for equation // Construct equation string carefully for PDF display let equationString = `${formatNumber(a)}x^2`; equationString += (b >= 0 ? ` + ${formatNumber(b)}` : ` - ${formatNumber(Math.abs(b))}`) + "x"; equationString += (c >= 0 ? ` + ${formatNumber(c)}` : ` - ${formatNumber(Math.abs(c))}`) + " = 0"; doc.text(equationString, margin + 40, currentY); currentY += lineHeight * 1.5; doc.setFont("helvetica", "normal"); // Back to normal font // Draw separator line doc.setDrawColor(primaryColor); doc.setLineWidth(0.5); doc.line(margin, currentY, doc.internal.pageSize.getWidth() - margin, currentY); currentY += lineHeight * 1.5; // Results Section doc.setFont("helvetica", "bold"); doc.text("Discriminant (Δ):", margin, currentY); doc.setFont("helvetica", "normal"); doc.text(formatNumber(discriminant), margin + 40, currentY); currentY += lineHeight; doc.setFont("helvetica", "bold"); doc.text("Root Type:", margin, currentY); doc.setFont("helvetica", "normal"); doc.text(rootType, margin + 40, currentY); currentY += lineHeight; doc.setFont("helvetica", "bold"); doc.text("Roots:", margin, currentY); doc.setFont("helvetica", "normal"); currentY += lineHeight; // Add space before listing roots doc.setFont("courier", "normal"); if (roots.length === 1) { doc.text(`x = ${roots[0]}`, margin + 10, currentY); } else if (roots.length === 2) { doc.text(`x1 = ${roots[0]}`, margin + 10, currentY); currentY += lineHeight; doc.text(`x2 = ${roots[1]}`, margin + 10, currentY); } doc.setFont("helvetica", "normal"); // --- Save PDF --- doc.save('quadratic_solution.pdf'); } // --- Event Listeners --- solveBtn.addEventListener('click', () => { const inputs = getInputs(); if (inputs) { const { a, b, c } = inputs; const result = solveQuadratic(a, b, c); displayResults(result); } }); downloadBtn.addEventListener('click', downloadPDF); // Add input listeners to clear errors when user types again [inputA, inputB, inputC].forEach(input => { input.addEventListener('input', () => { // Simple reset - clear border and result message input.style.borderColor = ''; if (resultsDiv.querySelector('.qes-error')) { resultsDiv.innerHTML = 'Enter coefficients and click "Solve Equation".
'; downloadBtn.style.display = 'none'; solutionData = null; } }); }); // --- Initial Setup --- resultsDiv.innerHTML = 'Enter coefficients (a, b, c) and click "Solve Equation".
'; downloadBtn.style.display = 'none'; // Ensure download button is hidden initially })(); // IIFE