I need to create a simple form with a button that calculates a number into a factorial. The calculation has to be done from the button and the answer needs to pop up in an alert box.
Embarisngly I've tried to figure this out for hours but I can not get the button to work and the workaround is to hit enter but then the answer does not display.
<script>
var n = document.getElementById('x1').value;
function fac(N) {
if (N > 1) return n * fac(N - 1);
else return N;
}
alert("Answer is "+ fac(N));
</script>
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>factorial</title>
</head>
<body>
<h2>Generate Your Factorial!</h2>
<form>
<label>Enter Number: </label><br>
<input type="text" id="x1"><br>
<input type="button" value= "Generate" onclick="fac(N)">
</form>
</body>
</html>