const ATTACK_VALUE = 10;
const STRONG_ATTACK_VALUE = 17;
const MONSTER_ATTACK_VALUE = 14;
function attackHandler( typeOfAttack ) {
const damageOne = typeOfAttack
const damageTwo = dealMonsterDamage(damageOne)
currentMonsterHealth -= damageTwo;
playerDamage = dealPlayerDamage(MONSTER_ATTACK_VALUE);
currentPlayerHealth -= playerDamage;
if ( currentMonsterHealth <= 0 && currentPlayerHealth > 0 ) {
alert('You won!');
} else if ( currentPlayerHealth <= 0 && currentMonsterHealth > 0 ) {
alert('You lost!');
} else if ( currentPlayerHealth <= 0 && currentMonsterHealth <= 0 ) {
alert('You have a draw');
}
}
attackBtn.addEventListener( 'click', attackHandler( ATTACK_VALUE ) );
strongAttackBtn.addEventListener( 'click', attackHandler( STRONG_ATTACK_VALUE ) );
The other code
function dealMonsterDamage(damage) {
const dealtDamage = Math.random() * damage;
monsterHealthBar.value = +monsterHealthBar.value - dealtDamage;
return dealtDamage;
}
I just started learning to code and have been following a tutorial, thought I would experiment, and I was wondering why does this not work. It executes immediatly and the buttons afterward does not work