From 08dc058cfe1184281fef89c0c484b9e8a6b7a6b7 Mon Sep 17 00:00:00 2001 From: Saumit Dinesan Date: Wed, 13 Sep 2023 19:08:54 +0530 Subject: Lab04: HTML Form Validation + Factorial --- Lab04/fact/script.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Lab04/fact/script.js (limited to 'Lab04/fact/script.js') diff --git a/Lab04/fact/script.js b/Lab04/fact/script.js new file mode 100644 index 0000000..a8f397e --- /dev/null +++ b/Lab04/fact/script.js @@ -0,0 +1,23 @@ +function factorial(n) { + if (n === 0) { + return 1; + } + return n * factorial(n - 1); +} + +document.addEventListener("DOMContentLoaded", function () { + const button = document.getElementById("submitbutton"); + const resultElement = document.getElementById("result"); + const numberInput = document.getElementById("numberInput"); + + button.addEventListener("click", function () { + const input = parseInt(numberInput.value); + + if (!isNaN(input) && input >= 0) { + const result = factorial(input); + resultElement.innerText = `The factorial of ${input} is equal to ${result}`; + } else { + resultElement.innerText = "Please enter a non-negative number."; + } + }); +}); -- cgit v1.2.3