4. The first if statement


#1

I got this exercise right, but I want to ask one thing. If I put

while (slaying){ if (youHit === 1) {console.log ("You hit the dragon!");}

It is as much right, as if I would put just like the example says, without (youHit === 1), just (youHit) :

while (slaying){ if (youHit) {console.log ("You hit the dragon!");}

How does the code know, that youHit is automatically 1? (True)?

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


#2

1 is considered true, 0 is considered false. (all positive numbers are considered true)


#3

But how does the code know automatically that youHit equals 1?


#4

it doesn't. But it doesn't matter. youHit is a random generated number which is then floored, which means we have two possible outcomes: 0 and 1. 1 is considered true, so the if statement gets executed, 0 is considered false, so the if statement doesn't get executed. We don't care if the number equals 1, we just want a 50% change of a hit


#5

Why do we

?
O doesn't mean "we don't hit the dragon"?
Why don't we type if (youHit===0) or if (youHit===1)?
The console will type "You hit the dragon...." no matter what youHit is. I don't get this. But what if youHit is 0? If 0 means we dont hit the dragon, how come it still prints "You hit the dragon...." ?

Please help, thanks.


#6

there is a 50% change you hit the dragon (if youHit is 1) and not hit the dragon (youHit is 0)

It shouldn't, then something is wrong with your code i would guess. Please make a new topic with code + error message + question