Dragon Slayer questions


#1

Hi i need some help with the dragon slayer lesson. I am using notepad++ and running it in chrome.

When I run it i get 3 possible outcomes. 1. 'the dragon eats me', 2. i do under 4 dmg and 'it says how much dmg i have done' and 3. i do more then 4 'it says how much dmg i have done and that i am victorious'.

On the 3rd last line of my code i have said else '(totalDamage < 4)' with the expectation that if i do under 4 dmg the dragon would still eat me. But it still just says how much dmg I am doing.

Can someone pls. explain what i am doing wrong?

/*
<html>

<head>

<title></title>

</head>

<body>
<p id = "text"></p>
<p id = "text2"></p>
<script>
var slaying=true;
var youHit=Math.floor(Math.random()*2);
var damageThisRound=Math.floor(Math.random()*5+1);
totalDamage=0

while(slaying) {
	if (youHit){
		document.getElementById("text").innerHTML=("You hit the dragon and did " + damageThisRound + " damage!"); 
		totalDamage += damageThisRound;
		
		 if (totalDamage >=4) {
		 document.getElementById("text2").innerHTML=("The Dragon is dead. You are victorious!")
		 slaying = false;
		 } else{youHit = math.floor(Math.random()*2);}
		}
		else (totalDamage < 4){
		document.getElementById("text2").innerHTML=("The Dragon eats you...");
		slaying = false;
		
		
		}
}
</script>
</body>

</html>
*/


#3

do you even get output? There is no trigger for the js code


#4

yes. im not using the codecady console. it prints on a blank chrom page...hmmm


#5

because nothing is triggering the js code? There also seems to be errors in your code, let me check

js code needs a trigger (document load, some event)


#6

here is an improved version of your code, next time, remember that else can't have a condition since it is everything else


#7

thankx, i see what you have done in jsbin. Oddly when copied notepad++ it prints out the whole code in chrome. (perhaps i have copied something wrong)

on that note in jsbin i do get this output:

You hit the dragon and did 5 damage!

The Dragon eats you...

which shouldnt be...

i am very noobish, will study what you have given me and hopefully go from there. thanks again.


#8

i removed the script tags, maybe you should insert those back in? I can't access the code in the bin, the code is gone


#9

must be something with notepad++ but i inserted your input and it works much better, i can have several results on one page. thanks.

I am still getting :

You hit the dragon and did 1 damage!

The Dragon is dead. You are victorious!

or i do 5 dmg and lose


#10

the problem with innerHTML is that if you hit multiply times, only the last one is displayed, given innerHTML will overwrite.

that is a bit weird, go fiddle with it, i helped you a bit further with it, if you need after a while need more help post an updated version of your code


#11

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