Witam, muszę stworzyć program do liczenia średnich w JS. Mam jakiś błąd przy zliczaniu wszystkich wag, którego nie potrafię odkryć. Oceny sumują się prawidłowo. Proszę o pomoc w wyjaśnieniu błędu oraz pozdrawiam
<html>
<head>
<script
src="http://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<script
src="http://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
crossorigin="anonymous"></script>
<script>
var zm =0;
var c = 0;
var o = 0;
function Licz() {
var ocenki = new Array;
$('.ocen').each(function(){
ocenki.push($(this).val());
});
var wagi = new Array;
$('.wag').each(function(){
wagi.push($(this).val());
});
for (j=0; j<wagi.length; j++)
{
// document.write(wagi[j]);
zm += wagi[j];
}
for (a=0; a<ocenki.length; a++)
{
ocenki[a]*= wagi[a];
}
for (a=0; a<ocenki.length; a++)
{
o += ocenki[a];
}
document.write(o/zm);
}
function funk() {
$('.ocen').focus(function(){
if($( this ).attr( "value" ) == "ocena" )
$(this).attr( "value","" );
});
$('.wag').focus(function(){
if($( this ).attr( "value" ) == "waga" )
$(this).attr( "value","" );
});
}
</script>
</head>
<body>
<div style="float:left" class="divi">
<input onclick="funk()" class="ocen" type="text" value="ocena">
<input onclick="funk()" class="wag" type="text" value="waga">
</div>
<div>
<button onclick="c++" id="but" >Dodaj przyciski</button>
<button onclick="Licz()">Licz</button>
</div>
<br>
<script>
window.addEventListener("click", funk);
window.addEventListener("load", funk);
var klon = $('.divi').html();
$('#but').click(function(){
$('.divi').prepend(klon + "<br>");
}); // koniec click
</script>
</body>
</html>