Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.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_Html_Arrays_Function - Fatal编程技术网

Javascript 如何求推式数组的和

Javascript 如何求推式数组的和,javascript,html,arrays,function,Javascript,Html,Arrays,Function,有人能帮我把推送的数字求和到数组中吗?我需要在第20轮后显示总和。我按下由玩家输入的分数点,所有的分数需要在20轮后相乘才能看到结果 var round=1; var评分1=[]; 函数roundNr(){ round++; document.getElementById(“p”).innerHTML=round; } 函数scoreCntr(){ var scorePoint1=document.getElementById('score1')。值; 得分1.推(得分点1); 如果(轮数>2

有人能帮我把推送的数字求和到数组中吗?我需要在第20轮后显示总和。我按下由玩家输入的分数点,所有的分数需要在20轮后相乘才能看到结果

var round=1;
var评分1=[];
函数roundNr(){
round++;
document.getElementById(“p”).innerHTML=round;
}
函数scoreCntr(){
var scorePoint1=document.getElementById('score1')。值;
得分1.推(得分点1);
如果(轮数>20){
document.getElementById(“score”).innerHTML=score1;
}
}

提交
圆形的

1


您缺少
score1.forEach(val=>sum+=parseInt(val))if
条件内的code>。还请注意,当您按下该值时,它是字符串类型,因此在添加它们之前,需要使用
parseInt()
获取整数值

var round=1;
var评分1=[];
函数roundNr(){
round++;
document.getElementById(“p”).innerHTML=round;
}
函数scoreCntr(){
var scorePoint1=document.getElementById('score1')。值;
得分1.推(得分点1);
如果(轮数>20){
var总和=0;
分数1.forEach(val=>sum+=parseInt(val));
document.getElementById(“score”).innerHTML=sum;
}
}

提交
圆形的

1


您有一个字符串数组,不是数字,而是:
score1.push(+scorePoint1)。。。innerHTML=score1.reduce((a,b)=>a+b,0)非常感谢!我是javascript的学生,所以我不知道它。但无论如何,谢谢你提供的信息。