简体   繁体   English

计算Javascript中的平方和

[英]Calculating the Sum of Squares in Javascript

I am having issues calculating the sum of squares in javascript. 我在计算javascript中的平方和时遇到问题。 I get lost when I define the for the loop and the variable index. 当我为循环和变量索引定义时,我迷路了。 Can anyone help me understand what i am doing wrong? 谁能帮助我了解我在做什么错?

var sums = [];
var sumsqs= Number(prompt("Please enter a number to sum the square"));
for (var index = 0; index < sumsqs.length; index++) {
var total = total + sumsqs[index] * sumsqs[index];

document.write("<h1>The sum of squares is " + total + ".</h1>");

Thank you, Viv 谢谢,Viv

If you're looking to convert, say, a comma-separated list of numbers to an array of numbers, that is not 例如,如果您想将逗号分隔的数字列表转换为数字数组,那不是

Number(prompt("Please enter a number to sum the square"))

(which will attempt to convert the entire string to a number), but rather (它将尝试将整个字符串转换为数字),而是

prompt("Please enter a number to sum the square").split(",").map(Number)

You also can't declare and start to use total at the same time; 您也不能同时声明并开始使用total you'll get NaN. 您会得到NaN。 Declare and initialize it before the loop: 在循环之前声明并初始化它:

var total = 0;

for (var index = 0; index < sumsqs.length; index++) {
    total += sumsqs[index] * sumsqs[index];
}
var inputNumber = Number(prompt("Please enter a number to sum the square"));
document.write("<h1>The sum of squares is " +
   ((inputNumber * (inputNumber + 1) * (2 * inputNumber + 1))/6) + ".</h1>");

It uses the formula mentioned here to calculate the result. 它使用此处提到的公式来计算结果。 http://library.thinkquest.org/20991/gather/formula/data/209.html http://library.thinkquest.org/20991/gather/formula/data/209.html

Thanks for everyone feedback. 感谢大家的反馈。 I just saw it a little too late. 我只是觉得有点晚了。 Here's what I did to complete the assignment. 这是我完成作业的工作。 I was able to figure it out with help of web console. 我能够在Web控制台的帮助下解决问题。

Thanks again! 再次感谢!

var sumsqs = [];
    var sumsq = Number(prompt("Please enter a number to calculate the sum the square or -1 to stop"));
    while (sumsq != -1) {
    sumsqs.push(sumsq);
      sumsq = Number(prompt("Please enter a number to calculate the sum the square or -1 to stop"));
    }

    var total_sumsqs = 0;
    for (var index = 0; index < sumsqs.length; index++) {
    total_sumsqs = total_sumsqs + sumsqs[index] * sumsqs[index];
    }

    document.write("<h3>The sum of squares is " + total_sumsqs + ".</h3>");

You can calculate the sum of squares of a comma-separated list using the following expression: 您可以使用以下表达式计算逗号分隔列表的平方和:

Math.hypot(...prompt().split`,`)**2

Explanation: 说明:

...prompt().split`,` accepts user input and creates an array from the input using , as the delimiter. ...prompt().split`,`接受用户输入,并创建使用从输入的阵列,作为分隔符。 Then the elements of the result array are converted to arguments for Math.hypot() using the spread operator . 然后,使用散布运算符将结果数组的元素转换为Math.hypot()参数。

Math.hypot() returns the square root of the sum of squares of its arguments, so if you raise the result to the power of 2 using the exponentiation operator , you can obtain the sum of squares. Math.hypot()返回其参数平方和的平方根,因此,如果使用幂运算符将结果提高为2 ,则可以获得平方和。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM