Javascript traditional for loop instances

Source: Internet
Author: User

In the past, the classic for loop instance in the learning program design was the 9-9 multiplication table. Now we use JavaScript to rewrite the Code:

Code
For (I = 1; I <= 9; I ++ ){
For (j = 1; j <= 9; j ++ ){
Console. Log (I + '*' + J + '=' + I * j );
}
}

Another example is to output the * matrix graph on the screen. Now we use JavaScript to display the 9*9 matrix graph on the webpage:

Code
VaR getidname = function (idname ){
This. ELEM = Document. getelementbyid (idname );
Return this. ELEM;
};

VaR testcase = {
Drawrectangle: function (x, y ){
For (I = 1; I <= x; I ++ ){
For (j = 1; j <= y; j ++ ){
ELEM = getidname ('elem1 ');
ELEM. innerhtml + = '*';
}
ELEM. innerhtml + = '<br/> ';
}
ELEM. innerhtml + = '<br/> ';
},
Run: function (){
This. drawrectangle (9, 9 );
}
}
Window. onload = function (){
Testcase. Run ();
}

HTML code:

Code
<Div id = "elem1"> </div>

The HTML code here is just a container for loading data.

The webpage displays the following results:

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

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.