Enter any number on the page and click the button to calculate the factorial ., Page factorial
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> </title>
<Script type = "text/javascript">
// Use a function to calculate any number factorial.
// Requirement: enter any number on the page and click the button to calculate the factorial.
Function $ (id) {return document. getElementById (id );}
// Use $ to replace document. getElementById () to reduce the code repetition rate.
Window. onload = function ()
{
ONum = $ ('num ');
OButt = $ ('butt ');
OBox = $ ('box ');
Function jie (n ){
// Calculate the factorial function and use the Recursive Method
If (n = 1)
{
Return 1;
}
Return n = n * jie (n-1 );
// Recursive formula n! = N * (n-1 )!
}
OButt. onclick = function (){
// Click to output the factorial event
NNum = Number (oNum. value );
OBox. innerHTML = jie (nNum );
}
}
</Script>
</Head>
<Body>
<Input id = "num" type = "text"/>
<Input id = "butt" type = "button" value = "Calculate factorial"/>
<Div id = "box"> </div>
</Body>
</Html>
Bytes