(1) Factorial problem.
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title></title>
<script>
function jiecheng(){ var n=jie.value ;//用于外部输入数字 var s=1; var a=n; for(var i=1;i<=a;i++){ s=s*n;//连加运算 n=n-1; } return s;//返回最终结果 }</script>
<body>
<p> Please enter a positive integer:</p>
<input type= "text" id= "Jie" value= "" >
<button onclick= "Jie.value=jiecheng ()" > Calculation </button>
</body>
(2) A random sentence of English, the first letter of each word capitalization problem
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title></title>
<script>
function Paiban () {
var a =cha.value;//defining variables storing English sentences
var b = new Array ();//define an array to hold the segmented word element
b = A.split ("");//Split string
for (var i = 0; i < b.length; i++) {
B[i]=b[i].slice (0,1). toLocaleUpperCase () +b[i].slice (1);//intercept the first capitalization, then intercept all the remaining and the former splicing, and finally restore the overall
}
return b;
}</script>
<body>
<input type= "text" id= "Cha" value= "" >
<button onclick= "Cha.value=paiban ()" > Typesetting </button>
</body>
(3) Find the longest word in an English sentence and output its length and word
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title></title>
<script>
function Chaci () {
var a =cha.value;//defines variables to accept data
var b = new Array ();//define array to receive array elements from the split string
b = A.split ("");//Split string
var C = 0;//Defines the variable to accept the length of the array element
var d = "";//define string to accept the word with the largest length
for (var i = 0; i < b.length; i++) {
if (B[i].length > C) {
c = b[i].length;//The word length to C from the first, only words with a length greater than C (the first length of the word) will overwrite the original length of C and D
d = b[i];
}
}
Return c+d;//returns results
}</script>
<body>
<input type= "text" id= "Cha" value= "" >
<button onclick= "Cha.value=chaci ()" > Find </button>
</body>
(4) repeats a string n times, and if n is negative, the result is empty.
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title></title>
<script>
var a= "";
var b=0;
Function Fu (A, b) {
var c= "";
if (a==null)
{
return 0;
}
else {
if (b>0)
{
for (Var i=0;i<b;i++)
{
C=c+a;
}
return C;
}
else {
C= "";
return C;
}
}
}</script>
<body>
<p> Please enter duplicate data:</p>
<input type= "text" id= "num" value= "" ><br>
<p> Please enter the number of repetitions:</p>
<input type= "text" id= "CI" value= "" ><br>
<button onclick= "Num.value=fu (num.value,ci.value)" > Repeat </button>
</body>
If there is a wrong place, please correct me!
Simple basic algorithm exercises on JS factorial, initial capitalization, longest word calculation, repetition of speaking times, etc.