Dragon Slayer questions


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?





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

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;



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


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


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)


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


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.


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


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


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


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