In JS, you can implement the 99 multiplication table by nesting two levels of for loops. The syntax format is "for(var i=1;i
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Javascript implements the 99 multiplication table
First of all, let’s look at the 99 multiplication table
We can get The rules for drawing a chart:
There are a total of 9 rows and 9 columns, and there are several expressions in each row.
In line i, the expression starts from i*1 and ends at i*i. There are a total of i expressions (we can achieve this effect through a loop).
Therefore, a double loop is needed to control the output. The outer loop controls the number of rows i (i is minimum 1 and the maximum is 9), and the inner loop controls column j (j is minimum 1, the maximum is equal to i).
Implementation code:
for(var i = 1; i <= 9; i++){ //外层循环控制行 for(var j = 1; j <= i; j++) //内层循环控制列 { document.write(j+"*"+i+"="+j*i+" "); } document.write(""); }
Output result:
We can also put the 99 multiplication table as shown in the beginning picture Output into a table:
document.write("
" + j + "*" + i + "=" + j * i + " | "); } //换行,控制每行的输出几个表达式 document.write("
Then add css style to modify it:
table { width: 600px; border-collapse: separate; } table td { border: #000 1px solid; text-align: center; }
Look at the output:
[Recommended learning:javascript advanced tutorial]
The above is the detailed content of How to implement 99 multiplication table in javascript. For more information, please follow other related articles on the PHP Chinese website!