Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 和数组不工作_Javascript_Arrays - Fatal编程技术网

Javascript 和数组不工作

Javascript 和数组不工作,javascript,arrays,Javascript,Arrays,我试图对一个动态数组求和。但由于某些原因,这些数字并不累加,只是显示在彼此旁边。我试着做1+2+3+4+5。但是我的输出结果是012345而不是15 var userInput = -1; var totalinputs = []; var sum = 0; var j = 0; While(userInput != 999) { userInput = window.prompt("Enter a test score, or 999 to end the program");

我试图对一个动态数组求和。但由于某些原因,这些数字并不累加,只是显示在彼此旁边。我试着做1+2+3+4+5。但是我的输出结果是012345而不是15

var userInput = -1;
var totalinputs = [];
var sum = 0;
var j = 0;

While(userInput != 999)
{
    userInput = window.prompt("Enter a test score, or 999 to end the program");

    if(userInput <= 100 && userInput >= 0)
    {
        totalInputs[j] = userInput;
        j++;
    }
}

$("lastScore").value = totalInputs[j-1];

for(var i = 0; i < totalInputs.length; i++)
{
    sum += totalInputs[i];
}

$("sumScores").value = sum;
替换为以下内容

for(var i = 0; i < totalInputs.length; i++)
{
    sum += +totalInputs[i]; //this coerces string to number.
}
for(变量i=0;i
a=“1”
可以通过编写a=+a在javascript中转换为
a=1
,这种类型的强制是一种JS特性

提示框中的用户输入是字符串。因此,您将面临此问题。

替换为以下内容

for(var i = 0; i < totalInputs.length; i++)
{
    sum += +totalInputs[i]; //this coerces string to number.
}
for(变量i=0;i
a=“1”
可以通过编写a=+a在javascript中转换为
a=1
,这种类型的强制是一种JS特性


提示框中的用户输入是字符串。因此,您面临着这个问题。

首先,您的脚本中有很多拼写错误,要使其正常工作非常困难

好的,让我们开始:当您提示某些内容时,浏览器将结果存储为字符串。如果将字符串索引0与字符串索引1求和,将得到两者的串联,而不是求和(如您所期望的)。要解决此问题,可以使用parseInt函数

var userInput = -1;
var totalInputs = [];
var sum = 0;
var j = 0;

while(userInput != 999)
{
  userInput = window.prompt("Enter a test score, or 999 to end the program");

  if(userInput <= 100 && userInput >= 0)
  {
    totalInputs[j] = userInput;
    j++;
  }
}

$("lastScore").value = totalInputs[j-1];

for(var i = 0; i < totalInputs.length; i++)
{
  sum += parseInt(totalInputs[i]);
}

$("sumScores").value = sum;
为此:

userInput = parseInt(window.prompt("Enter a test score, or 999 to end the program"));
然后可以从for循环中删除parseInt。最后:

// sum += parseInt(totalInputs[i]);
sum += totalInputs[i];

首先,你的剧本有很多打字错误,要让它正常工作真的很困难

好的,让我们开始:当您提示某些内容时,浏览器将结果存储为字符串。如果将字符串索引0与字符串索引1求和,将得到两者的串联,而不是求和(如您所期望的)。要解决此问题,可以使用parseInt函数

var userInput = -1;
var totalInputs = [];
var sum = 0;
var j = 0;

while(userInput != 999)
{
  userInput = window.prompt("Enter a test score, or 999 to end the program");

  if(userInput <= 100 && userInput >= 0)
  {
    totalInputs[j] = userInput;
    j++;
  }
}

$("lastScore").value = totalInputs[j-1];

for(var i = 0; i < totalInputs.length; i++)
{
  sum += parseInt(totalInputs[i]);
}

$("sumScores").value = sum;
为此:

userInput = parseInt(window.prompt("Enter a test score, or 999 to end the program"));
然后可以从for循环中删除parseInt。最后:

// sum += parseInt(totalInputs[i]);
sum += totalInputs[i];

虽然你已经标记了一个作为答案,但我会尝试抛出我自己的版本。以上两个答案仅在某些情况下有效。这是修剪到虫子为什么?因为您不能信任用户的输入

@Luis的答案很好,但您是否尝试过输入字母数字,如
1bs64s
?解析后,您会发现返回数字1。您的用户可能没有预料到这一点,但如果您同意的话,我想“我就到此为止”,但从用户的角度来看,这可能会很奇怪,因为您允许添加字母数字

下面是为什么它发生的描述,如果你是古玩

如果遇到的字符不是 指定的基数,则忽略该基数及其所有后续字符和 返回解析到该点的整数值。parseInt截断 将数字转换为整数值。允许使用前导空格和尾随空格

好吧,在你读过之后,你可能会说,这很简单,让我们把基数设为10来解决这个问题。不,没有。由于程序接受0-100之间的数字范围,程序员的程序仍然会接受它

@Pbd的答案也有问题,并遭受上述同样的信念。他只是简单地将字符串强制为数字,而没有适当的验证

// this condition without properly validation returns true. Allowing `1e1` to store in array.
if("1e1" <= 100 && "1e1" >= 0) // is true!
可能有很多版本可以保护您的安全,但IMHO将修剪用户的输入,强制字符串进行编号,并验证输入是否可转换为整数

userInput = window.prompt("Enter a test score, or 999 to end the program").trim(); // or userInput = userInput.trim();

var convertedInput = +userInput;
var isNumber = convertedInput == userInput; // check equality "1" == 1
if(userInput && isNumber && convertedInput >= 0 && convertedInput <= 100)
    totalInputs[j] = convertedInput;
或者使用ES6中引入的,它更简单:

var sum = totalInputs.reduce((pv, cv) => pv+cv, 0);
归功于“混沌”。我没有检查减少跨浏览器性能


Javascript很有趣!:)

虽然你已经把一个标记为答案,但我会尝试抛出我自己的版本。以上两个答案仅在某些情况下有效。这是修剪到虫子为什么?因为您不能信任用户的输入

@Luis的答案很好,但您是否尝试过输入字母数字,如
1bs64s
?解析后,您会发现返回数字1。您的用户可能没有预料到这一点,但如果您同意的话,我想“我就到此为止”,但从用户的角度来看,这可能会很奇怪,因为您允许添加字母数字

下面是为什么它发生的描述,如果你是古玩

如果遇到的字符不是 指定的基数,则忽略该基数及其所有后续字符和 返回解析到该点的整数值。parseInt截断 将数字转换为整数值。允许使用前导空格和尾随空格

好吧,在你读过之后,你可能会说,这很简单,让我们把基数设为10来解决这个问题。不,没有。由于程序接受0-100之间的数字范围,程序员的程序仍然会接受它

@Pbd的答案也有问题,并遭受上述同样的信念。他只是简单地将字符串强制为数字,而没有适当的验证

// this condition without properly validation returns true. Allowing `1e1` to store in array.
if("1e1" <= 100 && "1e1" >= 0) // is true!
可能有很多版本可以保护您的安全,但IMHO将修剪用户的输入,强制字符串进行编号,并验证输入是否可转换为整数

userInput = window.prompt("Enter a test score, or 999 to end the program").trim(); // or userInput = userInput.trim();

var convertedInput = +userInput;
var isNumber = convertedInput == userInput; // check equality "1" == 1
if(userInput && isNumber && convertedInput >= 0 && convertedInput <= 100)
    totalInputs[j] = convertedInput;
或者使用ES6中引入的,它更简单:

var sum = totalInputs.reduce((pv, cv) => pv+cv, 0);
归功于“混沌”。我没有检查减少跨浏览器性能


Javascript很有趣!:)

您可以尝试
sum+=Number(totalInputs[i])
,很可能是因为它们被读取为字符串而不是整数。只需将它们放在一个
parseInt
中,例如
parseInt(userInput)
我在总输入周围添加了数字,它就工作了!非常感谢。它们被读取为字符串,
+
用于连接两个字符串。这就是为什么它会显示
012345
,因为它是
“012345”
您可以尝试
sum+=Number(totalInputs[i])
,很可能是因为它们被读取为字符串而不是整数。只需将它们放在一个
parseInt
中,例如
parseInt(userInput)
我在总输入周围添加了数字,它就工作了!非常感谢。它们被读取为字符串,
+
用于连接两个字符串。这就是为什么它显示
012345