得到了数字列和平方列,但我无法弄清楚如何进行立方/第四/第五/第六列。
我知道我需要使用至少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
答案 0 :(得分:0)
为什么不使用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>");
}
答案 1 :(得分:0)
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>
您总是可以用更简洁的循环替换电源功能,但这应该提供您需要的输出。