【发布时间】:2014-01-31 20:41:13
【问题描述】:
好的,所以我需要创建一个 javascript 程序来查找第一个 n 平方数的总和(因此是标题)。除了公式,我几乎都完成了
<hmtl>
<body>
<center>
<p1>Sum of First n Whole Numbers</p>
<p1>Please enter a value into both boxes<br>
Press the button and the computer will determine the sum of all the numbers leading up to yours</p>
<script>
function sum() {
a = parseFloat(document.getElementById('boxone').value);
alert("The sum is "+);
}
</script>
Value 1: <input type="text" id="boxone" value="0">
<input type="submit" onclick="sum()" value="Summarize">
</center>
</body>
</html>
我知道公式是k^2=(n(n+1)(2n+1))/6,但我不知道如何将其放入代码中
【问题讨论】:
-
闻起来像家庭作业。你试过什么?
-
这只是简单的算术,他们不是在课堂上教过你怎么写吗?
-
将脚本放在
</body>之前的末尾,只需将a替换为n,您只需将k^2=替换为Math.sqrt并将其放在后面+... 明白了吗? -
我整个学期都在远程学习,我唯一遇到的问题是 n(n+1)。当我把它放进去时,代码就停止了。
-
您需要
n*(n+1)*(2*n+1)/6。一般来说,你总是需要明确乘法符号。
标签: javascript formula