Que el programa decida
Hasta ahora tu código hacía siempre lo mismo. Con if empieza a elegir.
Making the program decide
So far your code always did the same thing. With if it starts choosing.
if significa "si" en inglés. Se lee literal: si esto es
cierto, ejecuta lo que está entre llaves.
Entre paréntesis va la pregunta. Entre llaves { } va lo que pasa cuando la
respuesta es sí.
if reads literally: if this is true, run what is between the
braces.
Between the brackets goes the question. Between the braces { } goes what
happens when the answer is yes.
Fíjate que escribí === con tres signos, no uno. Esto confunde a todo el
mundo al principio:
=con uno — guarda un valor.vidas = 0pone cero en la caja.===con tres — compara.vidas === 0pregunta si vale cero.
Si por error escribes if (vidas = 0), no estás preguntando: estás
cambiando las vidas a cero en medio de la pregunta. Es un error clásico y difícil de
ver, porque no da mensaje rojo, simplemente el programa se comporta raro.
Notice I wrote === with three signs, not one. This confuses everyone at first:
=with one — stores a value.lives = 0puts zero in the box.===with three — compares.lives === 0asks whether it is zero.
If you mistakenly write if (lives = 0), you are not asking: you are
setting lives to zero inside the question. A classic bug and a hard one to spot,
because there is no red message — the program just behaves strangely.
=== | |
!== | |
> | |
< | |
>= | |
<= |
else significa "si no". Es el plan B: se ejecuta cuando la condición
del if no se cumple.
Siempre pasa uno de los dos. Nunca los dos, nunca ninguno.
else means "otherwise". It is plan B: it runs when the
if condition is not met.
One of the two always happens. Never both, never neither.
Cuando hay varias opciones, se encadenan con else if — "si no, y si en
cambio…".
Ojo si vienes de Python: allá esto se escribe elif, todo
junto. En JavaScript son dos palabras separadas: else if.
Copiar elif acá da error.
Se leen en orden, de arriba hacia abajo, y en cuanto uno se cumple, los demás ni se miran.
When there are several options, you chain them with else if — "otherwise,
if instead…".
Coming from Python? There this is written elif, one word. In
JavaScript it is two separate words: else if. Copying
elif here gives an error.
They are read in order, top to bottom, and as soon as one matches, the rest are not even looked at.
A veces necesitas preguntar dos cosas a la vez:
&&es Y — se tienen que cumplir las dos.||es O — basta con que se cumpla una.!es NO — invierte la respuesta.
La barra | se escribe con AltGr más la tecla que está sobre el
Enter, o Alt + 124.
Sometimes you need to ask two things at once:
&&is AND — both must be true.||is OR — one is enough.!is NOT — it flips the answer.
Ese true es un tipo de dato nuevo: el
booleano. Solo puede valer true (verdadero) o false
(falso). Se usa para todo lo que sea sí o no: ¿está vivo? ¿tiene escudo? ¿ya pasó el nivel?
That true is a new data type: the
boolean. It can only be true or false. Used for
anything yes-or-no: is it alive? does it have a shield? has the level been cleared?
Esto es, simplificado, lo que hace Vital Signs cada vez que tocas la pantalla:
This is, simplified, what Vital Signs does every time you tap the screen:
if, else y else if — que en Python sería
elif. Los comparadores. Que = guarda y === pregunta.
Combinar condiciones con && y ||. Y los booleanos.
Te falta una sola idea para poder hacer cualquier cosa: repetir.
if, else and else if — which in Python would be
elif. The comparators. That = stores and === asks.
Combining conditions with && and ||. And booleans.
One idea left before you can build anything: repeating.