I am currently developing a text-based adventure game where the output in the console log is determined by specific input. The goal is for a message to appear if a goblin inflicts enough attack damage to reduce the player's defense to below 0, stating "The Goblin did" x amount of "damage!". Below are the variables defined at the beginning of the code:
//player stats
var atk= 1;
var def= 1;
var hp= 10;
var mp= 0;
var block= 1;
var magic= 0;
//goblin stats
var gobAtk= [3,4,5];
var gobDef= 1;
var gobHp= 5;
var gobMagDef= 0;
var rand= Math.floor(Math.random()* gobAtk.length);
Further down in the code lies an if/else condition that executes only when the goblin's hp (var gobHp) has not been depleted to zero after the player attacks it earlier in the program. Here is the snippet:
else {
def-=rand;
if(def<0) {
hp-=Math.abs(rand);
console.log("The Goblin did"+ " "+ Math.abs(rand)+ " "+ "Damage!");
if(hp<=0) {
console.log("The Goblin defeated you! You died");
console.log("Game Over.");
}
}
else {
console.log("The Goblin did 0 damage!");
}
}
Despite executing the code multiple times, the console always displays "The Goblin did 0 damage!" regardless of whether the goblin's attack reduces the player's defense below 0. Ideally, the message should accurately reflect the amount of damage inflicted if the player's defense drops below 0.