summaryrefslogtreecommitdiff
path: root/Lab04/fact/script.js
diff options
context:
space:
mode:
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.";
+ }
+ });
+});