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:
*********
*********
*********
*********
*********
*********
*********
*********
*********