logo image

How to write Factorial program with jQuery + javascript + html

216


Demo

Code

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
	$("#btn").click(function(){
		var number = $("#num").val();
		var output = 1;

		if(number <= 0){
			 output = 1;
		}else{
			for(i=1; i<=number; i++){
				output = output*i;
			}
		}
		$("#result").html(output);
});
});
</script>
</head>
<body>
Enter a number:
<input type = "text" name = "value" id = "num"></input>
<button id= "btn"> Submit </button>
<p id = "result"> </p>
</body>
</html>