-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay_2[2].html
49 lines (44 loc) · 1.54 KB
/
Day_2[2].html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html>
<head>
<title>Simple HTML Template</title>
</head>
<body style="background-color: brown;">
<h1>Simple HTML Template</h1>
<p>
This is a basic HTML template with an input field and a button.
</p>
<label for="userInput">Enter something:</label>
<input type="text" id="userInput" placeholder="Type here...">
<button id="submitButton">Submit</button>
<div id="output"></div>
<!-- JavaScript code can be added here to handle user interactions -->
<script>
/*
Write a JavaScript function that calculates the factorial of a given number.
Use a loop to perform the factorial calculation within the function.
When the user enters a number and clicks the button, the function
should display the factorial result on the webpage.
*/
function calculateFactorial(){
//get user input from the field
const userInput = document.getElementById('userInput').value;
//convert the string to a number
const number = parseInt(userInput);
console.log(number);
let factorial = 1;
let i = 2;
while (i <= number){
factorial=factorial * i;
console.log('oh wow');
i++;
}
const outputDiv = document.getElementById('output');
outputDiv.textContent = factorial
return factorial;
}
const submitButton = document.getElementById('submitButton')
submitButton.addEventListener('click',calculateFactorial);
</script>
</body>
</html>