Simple basic algorithm exercises on JS factorial, initial capitalization, longest word calculation, repetition of speaking times, etc.

Source: Internet
Author: User
Tags repetition

(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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.