Changed the code a bit...LMK if it's OK


#1

Have the code so that it keeps on slaying when you push the button. Very similiar to the tutorial.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script>
var slaying = true;
var totalDmg = 0;
var num = 0;
var theDmg = 0;

var yourHit = function() {
	num = Math.floor( Math.random()*2 );
	return num;
}

var roundDmg = function() {
	theDmg = Math.floor( Math.random()*5 );
	return theDmg;
}

var swordSwing = function(){
	do {
		console.log("You swing...");
			if (yourHit()) {
					var currentDmg = roundDmg();
					console.log("You hit for " + currentDmg + " pts");	
					totalDmg += currentDmg;
					console.log(totalDmg);
			} else {
				console.log("You missed");
			}
	} while (totalDmg < 10)
	console.log("You have slain the dragon!");
	//totalDmg = 0;
}
</script>
</head>

<body>
<form>
<button id="swing" onclick="swordSwing()">Start Slaying</button>
</form>
</body>
</html>

#2

@wlcc,

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

</head>

<body>
 <div>
   <button id="swing">Start Slaying</button>
   <!-- <button id="swing" onclick="swordSwing()">Start Slaying</button> -->
 </div>
 <div>
    <ul class="result"></ul>
 </div>
</body>
<script>
var main = function() {
alert("main is executed");

var yourHit = function() {
	num = Math.floor( Math.random()*2 );
	return num;
}

var roundDmg = function() {
	theDmg = Math.floor( Math.random()*5 );
	return theDmg;
}

var displayText = function( theText ) {
			$('<li>').text(theText).appendTo(".result")
}
var swordSwing = function(){
	var doText1,ifText1, ifText2, elseText,slainText1;
var slaying = true;
var totalDmg = 0;
var num = 0;
var theDmg = 0;
	$('li').remove();
	do {
		//console.log("You swing...");
		doText1 = "You swing...";
		displayText(doText1);
			if (yourHit()) {
					var currentDmg = roundDmg();
					//console.log("You hit for " + currentDmg + " pts");
					ifText1 = "You hit for " + currentDmg + " pts";
					displayText(ifText1);
					totalDmg += currentDmg;
					//console.log(totalDmg);
					ifText2 = "Total Damage done: "+totalDmg;
					displayText(ifText2);
			} else {
				//console.log("You missed");
				elseText1 = "You missed";
				displayText(elseText1);
			}
	} while (totalDmg < 10)
	//console.log("You have slain the dragon!");
	slainText1 ="You have slain the dragon!";
	totalDmg = 0;
}
$('#swing').click( function() {
  swordSwing();
  });

};
$(document).ready( main );
</script>
</html>