lundi 31 août 2015

Javascript Math.pow() returning Uncaught TypeError: Cannot read property '0' of undefined

I can't seem to figure out why this:

Populations[0] = Math.round(Math.pow((Populations[0] * Math.E), (popGrowth * 5)));
Populations[1] = Math.round(Math.pow((Populations[1] * Math.E), (popGrowth * 5)));
Populations[2] = Math.round(Math.pow((Populations[2] * Math.E), (popGrowth * 5)));

returns this:

Uncaught TypeError: Cannot read property '0' of undefined

JSFiddle:http://ift.tt/1MYsAut

window.addEventListener('load', function () {
    document.getElementById("buttonEndTurn").addEventListener('click', endTurn());
});
var Populations = [100, 100, 100];
var popGrowth = [0.1797];
var resourceGold = [1000, 1000, 1000];
var resourceWood = [500, 500, 500];
var minerPercent1 = 0.5;
var minerPercent2 = 0.75;
var minerPercent3 = 0.1;
var Miners = [0, 0, 0];
var Loggers = [0, 0, 0];
var endTurn = function (Populations,popGrowth,resourceGold,resourceWood,minerPercent1,minerPercent2,minerPercent3,Minners,Loggers) {
    popGrowth += Math.random;

    Populations[0] = Math.round(Math.pow((Populations[0] * Math.E), (popGrowth * 5)));
    Populations[1] = Math.round(Math.pow((Populations[1] * Math.E), (popGrowth * 5)));
    Populations[2] = Math.round(Math.pow((Populations[2] * Math.E), (popGrowth * 5)));
        
 for (var m = 0; m < 3;m++) {console.log(Populations[m])};
    
    Miners = [Math.round(Populations[0] * minerPercent1),
    Math.round(Populations[1] * minerPercent2),
    Math.round(Populations[2] * minerPercent3)];

    Loggers = [Populations[0] - Miners[0],
    Populations[1] - Miners[1],
    Populations[2] - Miners[2]];
    for (var i = 0; i < 3; i++) {
        console.log(Miners[i]);
    }
    for (var j = 0; j < 3; j++) {
        console.log(Loggers[j]);
    }

    resourceGold[0] += Miners[0] * 100;
    resourceGold[1] += Miners[1] * 100;
    resourceGold[2] += Miners[2] * 100;

    resourceWood[0] += Loggers[0] * 100;
    resourceWood[1] += Loggers[1] * 100;
    resourceWood[2] += Loggers[2] * 100;

    for (var k = 0; k < 3; k++) {
        console.log(resourceGold[k]);
    }

    for (var l = 0; l < 3; l++) {
        console.log(resourceWood[l]);
    }
};
<script src="http://ift.tt/1oMJErh"></script>
<button id="buttonEndTurn">End Turn</button>
<canvas id="hexCanvas" width="800" height="600" style="width:800px;height:600px" />


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire