【发布时间】:2018-10-12 14:35:55
【问题描述】:
得到了数字列和平方列,但是我不知道如何处理立方/第四/第五/第六列。
我知道我需要使用至少 2 个嵌套的 for 循环来填充行和列。而且我知道 x^3 应该是 x * x * x ,而 x^4 应该是 x * x * x * x 等等。
<HTML>
<HEAD>
<TITLE>Table of Powers</TITLE>
</HEAD>
<BODY>
<SCRIPT type="text/javascript">
document.writeln("<TABLE BORDER = '1' WIDTH = '100%'>");
document.writeln("<TR><TH WIDTH = '100'><B>x</b></TH>");
document.writeln("<TH><B>x^2</B></TH>");
document.writeln("<TH><B>x^3</B></TH>");
document.writeln("<TH><B>x^4</B></TH>");
document.writeln("<TH><B>x^5</B></TH>");
document.writeln("<TH><B>x^6</B></TH></TR>");
for (var count = 1; count <=10; count++)
{
document.writeln("<TR><TD>" + count + "</TD><TD>" + square(count));
function square(x)
{
return x*x;
}
}
document.writeln("</TABLE>");
</SCRIPT>
</BODY>
</HTML>
输出应该是这样的:
x x^2 x^3 x^4 x^5 x^6
1 1 1 1 1 1
2 4 8 16 32 64
3 9 27 81 243 729
4 16 64 256 1,024 4,096
5 25 125 625 3,125 15,625
6 36 216 1,296 7,776 46,656
7 49 343 2,401 16,807 117,649
8 64 512 4,096 32,768 262,144
9 81 729 6,561 59,049 531,441
10 100 1,000 10,000 100,000 1,000,000
【问题讨论】:
-
提示:你可能想看看数学库link的
pow函数。
标签: javascript for-loop html-table nested