#6 Welldone Dragon youHit issue


#1


https://www.codecademy.com/courses/javascript-beginner-en-mrTNH-6VIZ9/0/6?curriculum_id=506324b3a7dffd00020bf661


If my youHit is 0 it will not console.log a miss


var slaying = true;
var youHit = Math.floor(Math.random()*1);
console.log(youHit)
var damageThisRound = Math.floor(Math.random()*5+1);
var totalDamage = 0;

while(slaying)  //while(slaying = true)
{ 
    if (youHit = 1) { //if its true 
        totalDamage += damageThisRound //x = x + y
            if (totalDamage >= 4) {
                console.log("You successfuly hit")
                console.log("You begin to hit for", totalDamage)
                console.log("dragon slayed with", totalDamage, "damage")    
            } else {
                console.log("Unfortunately", totalDamage, //
                "Was not enough and you now perish")
            }
        slaying = false;
    } else {
        if (youHit = 0) {  //doesnt work
            console.log("You miss completely and then perish")
            slaying = false;
        }
    }
}


#2

First of all, set var youHit = Math.floor(Math.random() * 2); If you don't put '* 2', it will always evaluate to 0, instead of 0 or 1.

Then, be careful writing the console.log() statements. If you want to add more than one element to console.log(), you need to use ' + ', like this: console.log("dragon slayed with" + totalDamage + "damage"); - also, don't forget the semicolon at the end of each statement.

slaying should evaluate to false only if totalDamage < 4, so you need to move it up a bit.

Lastly, you don't need another if statement at the end, because youHit is either 1 or 0.

Hope this helps! Pay attention to the syntax!


#3

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