Value sum of an array?

1

I can not display the sum of each line on the screen, values are being added together with the other lines

<!DOCTYPE html>
<html lang="">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <link rel="stylesheet" href="">
</head>

<body>
    <script>
        var somatoria = [];
        var soma = 0;

        for(x = 0; x<7; x++){
            for (y = 0; y<5; y++){
                somatoria[y]= Math.floor(Math.random()*100);
                soma += somatoria[y]; 
            }
            document.write(somatoria+" = "+soma+ "<br>");
        }
    </script>
</body>

</html>
    
asked by anonymous 17.06.2018 / 01:44

1 answer

2

Declare the variable var soma = 0; at the beginning of the first for to zero each line:

var somatoria = [];

for(x = 0; x<7; x++){
   var soma = 0;
   for (y = 0; y<5; y++){
      somatoria[y]= Math.floor(Math.random()*100);
      soma += somatoria[y]; 
   }
   document.write(somatoria+" = "+soma+ "<br>");
}
    
17.06.2018 / 01:49