jordansinn
jordansinn

Reputation: 33

Code does not seem to be updating global variable

For some reason doesItHit and doTheyHit always seem to execute, whilst dealDamage does nothing to the global variable.

var hitPercentage=Math.floor(Math.random() * 101) + 1
function doesItHit(){
    if(toHit * hitPercentage >= enemyEvasion){itHits = true}
    else("You miss.")
}

function doTheyHit(){
    if(enemyToHit * (Math.floor(Math.random() * 101) + 1) >= evasion){enemyToHit = true}}

function dealDamage(){enemyHealth= enemyHealth-lasers;}
function recieveDamage(){health= health - enemyLasers;}

function playerTurn(){
        doesItHit();
        if (itHits===true){
        dealDamage()
        console.log("You deal "+ lasers + " points of damage to the enemy.")
        lasersFired=false

        }    
}

Upvotes: 0

Views: 40

Answers (2)

Shoaib Chikate
Shoaib Chikate

Reputation: 8975

You didn't form correct expression so your dealDamge will never deal the damage due to wrong expression:

   enemyHealth= enemyHealth;-lasers;

You this:

   enemyHealth= enemyHealth-lasers;

Upvotes: 0

musefan
musefan

Reputation: 48415

It is because your code doesn't actually change anything. You have an extra semi-colon within dealDamage that you don't want.

Try this:

function dealDamage(){
    enemyHealth = enemyHealth - lasers;
}

(Assuming you want to subtract the value of lasers from enemyHealth)

On a side note, your recieveDamage function won't do anything either, but it is not clear as to what you intend for that so I cannot offer any suggestions.

Upvotes: 5

Related Questions