Give yourself 10 attemps to slay the dragon


#1

At last I am here not to ask for help, but rather brag. I rewrote the code to run 10 times and calculate me vs. dragon vs. "tie" points. I am now curious if I could have done it more elegantly.


Take 10 attemps to slay the dragon and find out how successful you were.


var slaying = true;
var Ihit = 0;
var damageSeverity = 0 ;
var totalDamage = 0;

var counter=0;
var dragonPoints = 0
var myPoints = 0;
var tiePoints = 0;

while (slaying && counter < 10) {
    console.log("Attempt to slay #"+(counter+1)+"\n");
    Ihit = Math.floor(Math.random()*2);
    damageSeverity = Math.floor(Math.random()*5 + 1);
1
    if (Ihit) {
        console.log ("Yeeezz! You hit the dragon with damage " + damageSeverity);
        totalDamage += damageSeverity;
        if (totalDamage >=4){
            console.log("I slew the dragon with total damage "+totalDamage);
            myPoints++;
        } else {
            Ihit = Math.floor(Math.random()*2);
            tiePoints++;
        }
    } else {
        console.log("Oops! Dragon hit you!");
        dragonPoints++;
    }
    counter++;
    totalDamage = 0;
}

console.log ("dragon: "+dragonPoints + " vs. me: "+myPoints + " tiePoints: "+ tiePoints);


#2

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