summaryrefslogtreecommitdiff
path: root/Lab04/fact/script.js
diff options
context:
space:
mode:
authorSaumit Dinesan <justsaumit@protonmail.com>2023-09-13 19:08:54 +0530
committerSaumit Dinesan <justsaumit@protonmail.com>2023-09-13 19:08:54 +0530
commit08dc058cfe1184281fef89c0c484b9e8a6b7a6b7 (patch)
tree834158194e039ef4a76a30752241089f06c87be7 /Lab04/fact/script.js
parentf8a237e363b020e876f553520329046cf813da8e (diff)
Lab04: HTML Form Validation + Factorial
Diffstat (limited to 'Lab04/fact/script.js')
-rw-r--r--Lab04/fact/script.js23
1 files changed, 23 insertions, 0 deletions
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.";
+ }
+ });
+});