Home > Web Front-end > JS Tutorial > body text

How to write the multiplication table using JS

亚连
Release: 2018-06-13 16:25:03
Original
2941 people have browsed it

This article mainly introduces the ninety-nine multiplication written in JS based on the for statement, involving the for statement loop output combined with the table layout to implement the ninety-nine multiplication function. Friends in need can refer to it

The example in this article describes the multiplication table written in JS based on the for statement. Share it with everyone for your reference, the details are as follows:

The function of js is very powerful, so now we use the for loop in js to output an original multiplication table;

Let’s take a look at the running effect first:

The core code is as follows:

<script type="text/javascript">
 document.write(&#39;<table border="1" bgcolor="#ccc" width="80%" cellpadding="0" cellspacing="0">&#39;);
 for(var i=1;i<=9;i++){
 document.write(&#39;<tr>&#39;);
  for(var j=1;j<=i;j++){
  document.write(&#39;<td>&#39;+i+&#39;x&#39;+j+&#39;=&#39;+(i*j)+&#39;</td>&#39;);
  }
 document.write(&#39;</tr>&#39;);
 }
 document.write(&#39;</table>&#39;);
</script>
Copy after login

You can try to go through the loop. Let’s multiply the two numbers of the nine-nine multiplication table. The number is set to two variables, namely i and j;

When i=1, the condition of the for loop is met, the loop statement is executed, and a (the row attribute in the table tag) is output first, Then execute another for loop in tr;

Let j=1, j<=i, at this time i=1, which meets the loop condition, execute the loop statement, and output 1*1=1; after j The value of j becomes 2; let's look at j<=i again, that is, 2<=1 is wrong, so the loop is terminated;

Our first pass through the loop outputs one line, and the content in the line It is 1*1=1; then we use i to execute the loop and let i=2, and also perform the loop according to the above;

When i reaches 9 and then goes down to 10, which does not meet the conditions, After terminating the entire loop, we output an original multiplication table;

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Using fullpage.js to implement scrolling

The problem of failure to install Electron using npm

How to implement callbacks using Promise in WeChat applet?

Concept and usage of command mode in JS (detailed tutorial)

Use selenium to capture Taobao data information

How to implement map grid using Baidu Map

The above is the detailed content of How to write the multiplication table using JS. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template