Polynomial Expression Calculator
Polynomial Expression Calculator
<div class=”poly-calculator”> <h2>Polynomial Expression Calculator</h2> <div class=”poly-form”> <div class=”poly-input-group”> <label for=”poly-coeffs”>Polynomial Coefficients:</label> <div class=”poly-desc”>Enter coefficients separated by commas, from highest power to lowest (constant).</div> <div class=”poly-input-wrapper”> <span class=”poly-sign”>P(x)</span> <input type=”text” id=”poly-coeffs” placeholder=”e.g. 3, -2, 5 (for 3x² – 2x + 5)”> </div> </div> <div class=”poly-input-group”> <label for=”poly-x-val”>Value for x:</label> <div class=”poly-input-wrapper”> <span class=”poly-sign”>x =</span> <input type=”number” id=”poly-x-val” placeholder=”e.g. 2″ step=”any”> </div> </div> <div class=”poly-buttons-container”> <button id=”poly-calculate-btn” onclick=”calculatePolynomial()”>Calculate</button> <button id=”poly-reset-btn” onclick=”location.reload()”>Reset</button> </div> </div> <div id=”poly-result” class=”poly-result-container” style=”display:none;”> <div class=”poly-result-item”> <span class=”poly-result-label”>Function:</span> <span class=”poly-result-value math-text” id=”res-poly-func”></span> </div> <div class=”poly-result-item poly-final”> <span class=”poly-result-label”>Result P(<span id=”res-x-display”>x</span>):</span> <span class=”poly-result-value”><span id=”res-poly-val”>0</span></span> </div> </div> </div> <style> .poly-calculator { max-width: 480px; margin: 40px auto; padding: 30px 25px; background: #fff; border-radius: 10px; box-shadow: 0 4px 16px rgba(0,0,0,0.1); font-family: sans-serif; } .poly-calculator h2 { text-align: center; color: #4A70A9; margin-bottom: 24px; border-bottom: 2px solid #8FABD4; padding-bottom: 10px; } .poly-input-group { margin-bottom: 22px; } .poly-input-group label { display: block; margin-bottom: 5px; color: #333; font-weight: 600; } .poly-desc { font-size: 0.85em; color: #666; margin-bottom: 8px; font-style: italic; } .poly-input-wrapper { display: flex; align-items: center; } .poly-sign { color: #555; margin-right: 10px; font-size: 1.05em; font-weight: 600; min-width: 30px; text-align: right; } .poly-input-group input { width: 100%; padding: 12px 14px; border: 1px solid #ddd; border-radius: 6px; font-size: 15px; box-sizing: border-box; color: #222; background: #fafcff; } .poly-input-group input:focus { border-color: #4A70A9; outline: none; box-shadow: 0 0 0 2px rgba(74, 112, 169, 0.2); } .poly-buttons-container { display: flex; justify-content: center; gap: 16px; margin-top: 28px; } #poly-calculate-btn, #poly-reset-btn { padding: 12px 30px; border: none; border-radius: 6px; cursor: pointer; font-size: 16px; font-weight: 600; transition: background 0.25s; box-shadow: 0 2px 4px rgba(0,0,0,0.1); color: #fff; } #poly-calculate-btn { background: #4A70A9; } #poly-calculate-btn:hover { background: #365685; } #poly-reset-btn { background: #8FABD4; } #poly-reset-btn:hover { background: #4A70A9; } .poly-result-container { margin-top: 30px; padding: 20px; background: #f4f8fc; border-radius: 8px; border-left: 5px solid #4A70A9; } .poly-result-item { display: flex; justify-content: space-between; align-items: flex-start; padding: 10px 0; border-bottom: 1px solid #dbe4f0; } .poly-result-item:last-child { border-bottom: none; } .poly-result-label { color: #444; font-weight: 500; min-width: 80px; margin-top: 2px; } .poly-result-value { color: #4A70A9; font-weight: 700; text-align: right; word-break: break-word; } .math-text { font-family: ‘Times New Roman’, serif; font-style: italic; font-size: 1.1em; color: #333; } .poly-final .poly-result-label { font-weight: 700; color: #222; } .poly-final .poly-result-value { font-size: 1.2em; color: #2e558e; } </style> <script> function calculatePolynomial() { var coeffInput = document.getElementById(‘poly-coeffs’).value; var xValInput = document.getElementById(‘poly-x-val’).value; if (coeffInput.trim() === “” || xValInput.trim() === “”) { alert(“Please enter both coefficients and a value for x.”); return; } // Parse Coefficients var coeffs = coeffInput.split(‘,’).map(function(item) { return parseFloat(item.trim()); }); // Validate Coefficients for (var i = 0; i < coeffs.length; i++) { if (isNaN(coeffs[i])) { alert(“Invalid coefficient found. Please use numbers separated by commas.”); return; } } var x = parseFloat(xValInput); if (isNaN(x)) { alert(“Please enter a valid number for x.”); return; } // Evaluate Polynomial (Horner’s Method recommended, but using simple power loop for clarity) var result = 0; var degree = coeffs.length – 1; var funcStr = “”; for (var i = 0; i < coeffs.length; i++) { var power = degree – i; var c = coeffs[i]; // Calculation result += c * Math.pow(x, power); // Build String Representation for display if (c !== 0) { var sign = (c < 0) ? ” – ” : ” + “; if (i === 0 && c > 0) sign = “”; // No sign for first positive term if (i === 0 && c < 0) sign = “-“; var absC = Math.abs(c); var term = “”; // Coefficient display logic (don’t show 1 if there is a variable, unless it’s just a number) if (power === 0) { term = absC; } else { term = (absC !== 1) ? absC : “”; term += “x”; if (power > 1) term += “<sup>” + power + “</sup>”; } funcStr += sign + term; } } if (funcStr === “”) funcStr = “0”; document.getElementById(‘res-poly-func’).innerHTML = funcStr; document.getElementById(‘res-x-display’).textContent = x; document.getElementById(‘res-poly-val’).textContent = parseFloat(result.toFixed(4)); // Limit decimals document.getElementById(‘poly-result’).style.display = ‘block’; } </script>
