Dragon Slayer #8) Says I pass but does not show damage output


#1

Just would like to know how to fix my code to put the damage output on the console log and space out the answer because right now I get on the console log "The dragon ate you because you only did0 damage! false" or when you win I get on the console log "You slew the dragon with0damage! You win! false" Thanks!


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 slew the dragon with" + totalDamage + "damage!");
totalDamage += damageThisRound;
 if (totalDamage >= 4) {
 console.log("You win!");
 slaying = false;
} else {
youHit = Math.floor(Math.random() * 2);
 }
} else { 
console.log("The dragon ate you because you only did" + totalDamage + " damage!");
}
slaying = false;
}


#2

Try "The dragon ate you because you only did ". Notice the extra space between did and the ".


#3

I am afraid I have tried that and it does not work. Even still, the spacing is a problem, but my real problem is why does the damage output not come up on the screen...


#4

Look at your console.log on line 7 in your above code. That should come after totalDamage += damageThisRound;. Otherwise, totalDamage will always be set to the original value of 0.


#5

Try this :

console.log("You slew the dragon with" + " " + "totalDamage + "damage!")


#6

Ok! Now we are talking! Thank you! Is there a technical reason why it did that? Now to get that spacing figured out :joy:


#7

I am afraid not biff75 Apparently, that is actually wrong because I get back a syntax error


#8

Your console logs should look like this:

console.log("You slew the dragon with " + totalDamage + " damage!");

and

console.log("The dragon ate you because you only did " + totalDamage + " damage!")


#9

The code passes the test but I get back an error message saying "Cannot read property 'log' of undefined....so idk what happened when i copied and pasted it in


#10

console.log("You slew the dragon with" + " " + totalDamage + " " + "damage!")

I meant this, sorry


#11

What did you copy/paste?


#12

While this works biff75 for passing it does not change the spacing effect...
Also, @itschrisburton i copied and pasted your to console.logs right into mine. However, it gives me that error message I stated earlier


#13

Please repost your code so I can take a look.


#14

Gladly! On the first console log I have kept biff75's version in there

var slaying = true ;
var youHit = Math.floor(Math.random() * 2) ;
var damageThisRound = Math.floor(Math.random()*5 + 1) ;
var totalDamage = 0 ;
totalDamage += damageThisRound;
while (slaying) {
if (youHit) {
console.logconsole.log("You slew the dragon with" + " " + totalDamage + " " + "damage!")
if (totalDamage >= 4) {
console.log("You win!");
slaying = false;
} else {
youHit = Math.floor(Math.random() * 2);
}
} else {
console.log("The dragon ate you because you only did" + totalDamage + "damage!");
}
slaying = false;
}


#15

Look at line 8 of your above code:

console.logconsole.log("You slew the dragon with" + " " + totalDamage + " " + "damage!")

console.log is in there twice.


#16

well that will explain the copy and paste error. However, my secondary problem of spacing still remains. Also apparently now my dragon now wins even when you do 5 damage and you can slay the dragon with two damage so something has happened also with that... Sorry if this is seeming hopeless or pointless


#17

This is why I suggested to add spaces inside the quotes. It's easier to read.

You can do either this (more readable):

console.log("The dragon ate you because you only did " + totalDamage + " damage!");

or:

console.log("The dragon ate you because you only did" + " " + totalDamage + " " + "damage!");


#18

I understand this point. And I agree, it is easier to read in the code, however, I am trying to get what shows up on the console log to be spaced out more. I have tried this method several times with no prevail on getting the desired result.


#19

You're just making simple mistakes which trip everyone up at some point. It's all good.

Look at line 5 of your recent post.

Line 5: totalDamage += damageThisRound;

Should that be there or above console.log("You slew the dragon with" + " " + totalDamage + " " + "damage!")? You got this!


#20

The problem with moving the

is that when put under the console.log I get no damage output. However, when put above I do.