How to implement 99 multiplication table in javascript

青灯夜游
Release: 2022-02-23 18:24:44
Original
20463 people have browsed it

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

How to implement 99 multiplication table in javascript

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

How to implement 99 multiplication table in javascript

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("
"); }
Copy after login

Output result:

How to implement 99 multiplication table in javascript

We can also put the 99 multiplication table as shown in the beginning picture Output into a table:

document.write(""); for (var i = 1; i <= 9; i++) { //外层循环控制行 document.write(""); for (var j = 1; j <= i; j++) //内层循环控制列 { document.write(""); } //换行,控制每行的输出几个表达式 document.write(""); } document.write("
" + j + "*" + i + "=" + j * i + "
");
Copy after login

Then add css style to modify it:

table { width: 600px; border-collapse: separate; } table td { border: #000 1px solid; text-align: center; }
Copy after login

Look at the output:

How to implement 99 multiplication table in javascript

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!