Example of loop in js

Source: Internet
Author: User

 

Multiplication

// For (I = 1; I <= 9; I ++) {// control row

 

// For (j = 1; j <= I; j ++) {// Control

 

// Document. write (I + "*" + j + "=" + (I * j) + "& nbsp ");

//}

// Document. write ("<br/> ");

 

//}

/*

*

***

*****

 

*/

 

// For (I = 1; I <= 5; I ++) {// control row

 

// For (j = 1; j <= 2 * I-1; j ++) {// Control

 

// Document. write ("*");

//}

// Document. write ("<br/> ");

 

//}

 

 

 

/*

*

***

*****

*******

*********

***********

*************

***************

*****************

*******************

 

 

*/

// For (I = 1; I <= 10; I ++ ){

//// Print space

// For (t = 1; t <= 10-I; t ++ ){

 

// Document. write ("& nbsp ");

 

//}

// For (j = 1; j <= 2 * I-1; j ++ ){

 

 

// Document. write ("*");

 

 

//}

// Document. write ("<br/> ");

//}

 

 

// Print the diamond Graph

 

 

// For (I = 1; I <= 5; I ++ ){

//// Print space

// For (t = 1; t <= 5-i; t ++ ){

 

// Document. write ("& nbsp ");

 

//}

// For (j = 1; j <= 2 * I-1; j ++ ){

 

 

// Document. write ("*");

 

 

//}

// Document. write ("<br/> ");

//}

 

 

/*

Number of rows, number of stars, number of spaces

*/

For (I = 4; I> = 1; I --){

// For (t = 4; t> = I; t --)

For (t = 5-i; t> = 1; t --)

{

 

Document. write ("& nbsp ;");

 

}

For (j = 1; j <= 2 * I-1; j ++ ){

Document. write ("*");

 

 

}

Document. write ("<br/> ");

 

}

 

 

 

You can use the for loop to implement some related images.

 

For example, print the diamond:

*

***

*****

*******

*********

*******

*****

***

*

// Print the diamond Graph

 

 

For (I = 1; I <= 5; I ++ ){

// Print space

For (t = 1; t <= 5-i; t ++ ){

 

Document. write ("& nbsp ");

 

}

For (j = 1; j <= 2 * I-1; j ++ ){

 

 

Document. write ("*");

 

 

}

Document. write ("<br/> ");

}

 

 

/*

Number of rows, number of stars, number of spaces

*/

For (I = 4; I> = 1; I --){

// For (t = 4; t> = I; t --)

For (t = 5-i; t> = 1; t --)

{

 

Document. write ("& nbsp ;");

 

}

For (j = 1; j <= 2 * I-1; j ++ ){

Document. write ("*");

 

 

}

Document. write ("<br/> ");

 

}

 

 

 

 

/*

*

**

**

**

*****

*/

For (I = 1; I <= 5; I ++)

{

// Print space

For (t = 1; t <= 5-i; t ++)

{

Document. write ("& nbsp ;");

}

 

For (j = 1; j <= 2 * I-1; j ++)

{

// Print other rows except the last row, with spaces in the first and last columns.

If (I <5)

{

If (j = 1 | j = 2 * i-1)

Document. write ("*");

Else

Document. write ("& nbsp ;");

}

Else // last line, odd *, even space

{

If (j % 2! = 0)

Document. write ("*");

Else

Document. write ("& nbsp ;");

}

}

Document. write ("<br/> ");

}

 

 

 

 

<! DOCTYPE html PUBLIC "-// W3C // DTDXHTML 1.0 Transitional //" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>

<Title> triangle printing </title>

</Head>

 

<Body style = "text-align: center;">

<Script type = "text/javascript">

Var k = prompt ("Enter the number of printed rows :","");

For (var I = 1; I <= k; I ++)

{

For (varj = 0; j <I; j ++)

{

Document. write ("* & nbsp ;");

}

Document. write ("<br/> ");

}

</Script>

</Body>

</Html>

 

From Aweijun360

Related Article

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.