General Doubts; Semicolon


#1

I am very new to programming and only know what I have been able to pick up from this course.

When do I use a semicolon? I understand it is at the end of a statement... so what is the end of a statement?

also... Specifically to Dragon Slayer:
I came up with this

if (youHit >= 1) {
console.log("congratulatory message. Dragon is hit! You did" + " " + damageThisRound + " " + "damage to the dragon")
totalDamage = totalDamage += damageThisRound

whereas the hint states:

if (youHit >= 1) {
console.log("congratulatory message. Dragon is hit! You did" + " " + damageThisRound + " " + "damage to the dragon")
totalDamage += damageThisRound

What is the difference between adding >= 1 and not? "totalDamage = totalDamage += damageThisRound" and "totalDamage += damageThisRound"?

Thanks in advance to taking the time to answer my query.


#2

When talking about int values, everything different (or greater, I don't remember very well) than 0 will return true (a boolean value). So:

  • if (youHit => 1) will return true if youHit is equal or greater than 1.
  • if (youHit) will return true if that variable value is true (for numbers will be what I said before, for strings will be other condition, etc).

The difference is in the syntax (quantity of code you have to write) and in the "correct thing to do" (you should do the first one if Codecademy lets you, because is the good way for numbers).


#3

Thank you. What do you mean by "(you should do the first one if Codecademy lets you, because is the good way for numbers)."?

It did let me do:

if (youHit >= 1)

it is the same as:

if (youHit => 1)

Right?


#4

Yeah, but if you accidentally change the type of youHit (to a string like "aksda", for example), you should get unexpected behaviour doing if (youHit >= 1) (In this case, you cannot compare a str with an int, so it'll throw an exception).