Factorial Calculator
Calculate factorial values with ease
ⓘ
Enter a non-negative integer (up to 170)
Result:
The factorial result will appear here
Calculation History:
No calculations yet
"; return; } calculationHistory.slice().reverse().forEach((item, index) => { const historyItem = document.createElement("div"); historyItem.className = "history-item"; // For large results, truncate them let displayResult = item.result; if (displayResult.length > 20) { displayResult = displayResult.substring(0, 10) + "..." + displayResult.substring(displayResult.length - 10); } historyItem.innerHTML = ` ${item.number}! = ${displayResult} ${item.time} `; historyContainer.appendChild(historyItem); }); } // Event listener for calculate button calculateBtn.addEventListener('click', function() { const inputValue = parseFloat(numberInput.value); if (isNaN(inputValue)) { errorDisplay.textContent = "Please enter a valid number."; return; } try { const result = calculateFactorial(inputValue); updateResult(inputValue, result); } catch (error) { errorDisplay.textContent = error.message; } }); // Event listener for reset button resetBtn.addEventListener('click', function() { numberInput.value = ""; resultDisplay.textContent = "The factorial result will appear here"; formulaDisplay.textContent = ""; errorDisplay.textContent = ""; }); // Event listener for Enter key numberInput.addEventListener('keypress', function(e) { if (e.key === 'Enter') { calculateBtn.click(); } }); // Event listener for download button downloadBtn.addEventListener('click', function() { // Check if there's a result to download if (resultDisplay.textContent === "The factorial result will appear here") { errorDisplay.textContent = "Calculate a factorial first before downloading."; return; } // Create a new jsPDF instance const { jsPDF } = window.jspdf; const doc = new jsPDF(); // Set background colors similar to the calculator doc.setFillColor(106, 17, 203); // #6a11cb doc.rect(0, 0, 210, 297, 'F'); doc.setFillColor(37, 117, 252); // #2575fc doc.rect(0, 100, 210, 197, 'F'); // Set text color to white doc.setTextColor(255, 255, 255); // Add title doc.setFontSize(24); doc.text('Factorial Calculator Results', 105, 20, { align: 'center' }); // Add date and time doc.setFontSize(12); doc.text(`Generated on: ${new Date().toLocaleString()}`, 105, 30, { align: 'center' }); // Add result section doc.setFillColor(255, 255, 255, 0.2); doc.roundedRect(20, 40, 170, 40, 3, 3, 'F'); doc.setFontSize(16); doc.text('Current Calculation:', 30, 55); // Handle large results by breaking them across multiple lines if needed const currentResult = resultDisplay.textContent; doc.setFontSize(14); if (currentResult.length > 60) { const chunks = []; for (let i = 0; i < currentResult.length; i += 60) { chunks.push(currentResult.substring(i, i + 60)); } chunks.forEach((chunk, index) => { doc.text(chunk, 30, 65 + (index * 10)); }); } else { doc.text(currentResult, 30, 65); } // Add history section doc.setFontSize(16); doc.text('Calculation History:', 30, 100); // Add history items calculationHistory.slice().reverse().forEach((item, index) => { const yPos = 115 + (index * 15); // Create a background for each history item doc.setFillColor(255, 255, 255, 0.2); doc.roundedRect(20, yPos - 10, 170, 15, 3, 3, 'F'); // For large results, truncate them let displayResult = item.result; if (displayResult.length > 40) { displayResult = displayResult.substring(0, 20) + "..." + displayResult.substring(displayResult.length - 20); } const historyText = `${item.number}! = ${displayResult}`; doc.setFontSize(12); doc.text(historyText, 30, yPos); doc.text(item.time, 160, yPos, { align: 'right' }); }); // Add footer doc.setFontSize(10); doc.text('© Factorial Calculator Tool', 105, 280, { align: 'center' }); // Save the PDF doc.save('factorial-calculation-results.pdf'); }); });