How to print the correct value of a random array inside an If / Else Statement in my text adventure?

I am trying to make a text adventure that will output the correct console log expression based on specific input. I want that if the goblin does enough damage from attacks so that the player’s defense is less than 0, then the console magazine will print out "The goblin did" x amount of "damage!". Here is my list of variables at the top 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);

At the bottom of my code, a specific if / else condition is called until the goblin hp (var gobHp) drops to zero after the player attacks the goblin in the previous section of the code. Here it is:

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!");
        }
}

, , " 0 !" 0. def 0, .

+4
1

, :

function getRandomArbitrary(min, max) {
   return Math.random() * (max - min) + min;
}
var rand = getRandomArbitrary(3,5)

:

() , ,

+2

Source: https://habr.com/ru/post/1614071/


All Articles