Always the same damage? 6/6


#1



https://www.codecademy.com/courses/javascript-beginner-en-mrTNH-6VIZ9/0/6?curriculum_id=506324b3a7dffd00020bf661


I did everything correctly, and I've gone a bit further, adjusting the hit rate and adding a critical hit system.
However, I notice that when I run the program, it always deals the same damage every time. How can I randomise the damage each time?


var slaying = true
var youHit = Math.floor(Math.random() * 12)
var dragonHits = Math.floor(Math.random())*100
var damageThisRound = Math.floor(Math.random() * 26 + 50)
var dragonAttackDamage = Math.floor(Math.random() * 61 + 120)
var dragonHealth = 500
var playerHealth = 1500;


while (slaying)
{
    if (youHit < 8)
    {
        if(youHit < 2)
        {
            damageThisRound *= 2
            console.log("You land a critical hit! You deal" + " " +                           damageThisRound + " " + "damage!")
            dragonHealth -= damageThisRound
            if (dragonHealth <= 0)
            {
                console.log("The dragon has fallen! You have saved the town of Madrigar from its wrath!");  
                slaying = false;
            }
            else
            {
                youHit = Math.floor(Math.random() * 12)
            }
        }
        else
        {
            console.log("You strike the dragon, dealing" + " " +                                 damageThisRound + " " + "damage!")
            dragonHealth -= damageThisRound
            if (dragonHealth <= 0)
            {
                console.log("The dragon has fallen! You have defeated it!");  
                slaying = false;
            }
            else
            {
                youHit = Math.floor(Math.random() * 12);
            };
        }
    }
    else
    {
        if (dragonHits >= 95)
        {
            console.log("You barely dodge the dragons claws!")
        }
        else
        {
            console.log("The dragon's claws thunder onto you, dealing" + " " + dragonAttackDamage + " " + "damage!")
            playerHealth -= dragonAttackDamage
            if (playerHealth <= 0)
            {
                console.log("The dragon slashes you with its claws one final time... you've lost!");
                slaying = false;
            }
            else
            {
                youHit = Math.floor(Math.random() * 12)
            }
        }
    }
};


#2

You'd put the code that randomises the damage at a place that is executed each time it needs to be re-rolled, right? So at the start of a turn?


#3

so i would put the youHit variable right after the while (slaying)?


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.