The First IF statement, dragon never wins


#1



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

In the help at the side of the text it suggests that if youHit is 1 (true) then write a congratulatory message. However i understood this to mean that you write IF youHit = 1.
I understand that i am now wrong and just "youHit" should be in the brackets.

HOWEVER the code i wrote on the right allowed me to go on to the next lesson BUT it never gave me an outcome where the dragon won. I was just wondering why?

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 hit the dragon!");
    } else {
        console.log("The dragon got you mate");
    }
    slaying = false;
}


#2

youHit = 1 assigns 1 to youHit and the statement as a whole has a value of 1 (what you assigned) so no matter what youHit originally was now it is 1 as well as the condition. If you think treating 1 as true and 0 is false is a little fishy than you can use == or === which compare values or values and types but do not use the assignment operator =.


#3

it took my ages but i just realised i only used one =.

Thanks.


#4