【问题标题】:JavaScript table of powers (nested for loop)JavaScript 权力表(嵌套 for 循环)
【发布时间】: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

【问题讨论】:

  • 提示:你可能想看看数学库linkpow函数。

标签: javascript for-loop html-table nested


【解决方案1】:

你为什么不用Math.pow function

类似的,

for (var base = 1;base <=10;base++)
{document.writeln("<TR>");
for (var count = 1; count <=6; count++)
{
    document.writeln( "<TD>"  + Math.pow(base,count)+"</TD>");

}
document.writeln("</TR>");
}

【讨论】:

  • 即使我这样做了,我仍然不确定如何使用嵌套的 for 循环填充表格的行和列。
【解决方案2】:

    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) + "</TD>");
        document.writeln("<TD>" + cubed(count)) + "</TD>";
        document.writeln("<TD>" + fourth(count)) + "</TD>";
        document.writeln("<TD>" + five(count)) + "</TD>";
        document.writeln("<TD>" + six(count)) + "</TD>";
        function square(x) {
            return x * x;
        }
        function cubed(x) {
            return x * x *x;
        }
        function fourth(x) {
            return x * x * x * x;
        }
        function five(x) {
            return x * x * x *x * x;
        }
        function six(x) {
            return x * x * x * x * x *x;
        }
    }
    document.writeln("</TABLE>");
</script>

您总是可以用更简洁的循环替换幂函数,但这应该会提供您需要的输出。

【讨论】:

  • 你不能将函数声明放在循环体中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-13
  • 1970-01-01
  • 2017-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多