#5 not looping


It looks like my code was acceptable. However while running this several times I've notice whenever I dealt damage below 4 it stops(turns false) and not run the youHit command again under the second set of else. What am I doing wrong?

var slaying = true;

var youHit = Math.floor(Math.random() * 2);

var damageThisRound = Math.floor(Math.random()*5 + 1);

var totalDamage = 0; 

while (slaying) {
    if (youHit) {
        console.log("You hit the sunuvabiatch with " +damageThisRound+" damage!");
        totalDamage += damageThisRound;
            if (totalDamage >=4) {
                console.log("You killed that damn foo! Claim the treasure and bootaay!!");
                slaying = false;
            else {
                youHit = Math.floor(Math.random() * 2);
    else {
        console.log("The muthafiretrucka burnt you to a toast and ate you! Fuuark!");
    slaying = false;



Are you sure it is just when the damage is below 4?

It seems that you have a second slaying = false outside of any if ... else code blocks which means that once the statements above the second slaying = false statement have run, it will always run the second slaying = false statement afterwards and therefore always run your while loop only one time since slaying will become false and the while loop only continues if the condition is true.

Try removing the second slaying = false code and see if that fixes your issue.


Ok I see where your getting at. It works now! I didnt realize it was outside of the 1st else function when it was actually within the while function. Thanks icodez!


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