Skip to content

Logical operators #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
35a6c72
Traducción de Logical Operators
Sjesc May 16, 2019
f1ab3cc
Fix small details
Sjesc May 16, 2019
85b9fcd
Translate string
Sjesc May 16, 2019
865f4e7
fixes
tscandalitta May 18, 2019
ec93c16
fix 2
tscandalitta May 20, 2019
b109d94
fix 3
tscandalitta May 20, 2019
335ce5f
fix 4
tscandalitta May 20, 2019
0bac3ae
fix 5
tscandalitta May 20, 2019
b7f27dc
Update solution.md
tscandalitta May 20, 2019
6d0f0a6
Update task.md
tscandalitta May 20, 2019
885c3fe
Update solution.md
tscandalitta May 20, 2019
e306bae
Update task.md
tscandalitta May 20, 2019
05c8fa6
Update solution.md
tscandalitta May 20, 2019
1a7bb8a
Update task.md
tscandalitta May 20, 2019
682cda5
Update task.md
tscandalitta May 20, 2019
a2fac86
Update solution.md
tscandalitta May 20, 2019
7f940f1
Update task.md
tscandalitta May 20, 2019
36c1b7d
Update solution.md
tscandalitta May 20, 2019
32b4744
Update task.md
tscandalitta May 20, 2019
4b8c0a6
Update solution.md
tscandalitta May 20, 2019
9c17870
Update task.md
tscandalitta May 20, 2019
847d5f5
Update solution.md
tscandalitta May 20, 2019
96a65ec
Update solution.md
tscandalitta May 20, 2019
4809cbe
Update task.md
tscandalitta May 20, 2019
f2e80f4
Update task.md
tscandalitta May 20, 2019
7fa3dd2
Update solution.md
tscandalitta May 20, 2019
1adc84c
Update solution.md
tscandalitta May 20, 2019
c7cb13e
Update solution.md
tscandalitta May 20, 2019
0a5f045
Update solution.md
tscandalitta May 20, 2019
28c2314
Update article.md
tscandalitta May 20, 2019
ded4ce0
Update task.md
tscandalitta May 20, 2019
a97d693
Update solution.md
tscandalitta May 20, 2019
e13ff70
Update solution.md
tscandalitta May 20, 2019
bd691f5
Update task.md
tscandalitta May 20, 2019
1345509
Update solution.md
tscandalitta May 20, 2019
927cb20
Update task.md
tscandalitta May 20, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The answer is `2`, that's the first truthy value.
La respuesta es `2`, ese es el primer valor verdadero.

```js run
alert( null || 2 || undefined );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# What's the result of OR?
# ¿Cuál es el resultado de OR?

What is the code below going to output?
¿Cuál será la salida del siguiente código?

```js
alert( null || 2 || undefined );
Expand Down
12 changes: 6 additions & 6 deletions 1-js/02-first-steps/11-logical-operators/2-alert-or/solution.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
The answer: first `1`, then `2`.
La repuesta: primero `1`, después `2`.

```js run
alert( alert(1) || 2 || alert(3) );
```

The call to `alert` does not return a value. Or, in other words, it returns `undefined`.
La llamada a `alert` no retorna un valor. O, en otras palabras, retorna `undefined`.

1. The first OR `||` evaluates it's left operand `alert(1)`. That shows the first message with `1`.
2. The `alert` returns `undefined`, so OR goes on to the second operand searching for a truthy value.
3. The second operand `2` is truthy, so the execution is halted, `2` is returned and then shown by the outer alert.
1. El primer OR `||` evalua el operando de la izquierda `alert(1)`. Eso muestra el primer mensaje con `1`.
2. El `alert` retorna `undefined`, por lo que OR se dirige al segundo operando buscando un valor verdadero.
3. El segundo operando `2` es un valor verdadero, por lo que se detiene la ejecución, se retorna `2` y es mostrado por el alert exterior.

There will be no `3`, because the evaluation does not reach `alert(3)`.
No habrá `3` debido a que la evaluación no alcanza a `alert(3)`.
4 changes: 2 additions & 2 deletions 1-js/02-first-steps/11-logical-operators/2-alert-or/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 3

---

# What's the result of OR'ed alerts?
# ¿Cuál es el resultado de las alertas aplicadas al operador OR?

What will the code below output?
¿Cuál será la salida del siguiente código?

```js
alert( alert(1) || 2 || alert(3) );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The answer: `null`, because it's the first falsy value from the list.
La respuesta: `null`, porque es el primer valor falso de la lista.

```js run
alert( 1 && null && 2 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# What is the result of AND?
# ¿Cuál es el resultado de AND?

What is this code going to show?
¿Cuál será la salida del siguiente código?

```js
alert( 1 && null && 2 );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
The answer: `1`, and then `undefined`.
La respuesta: `1` y después `undefined`.

```js run
alert( alert(1) && alert(2) );
```

The call to `alert` returns `undefined` (it just shows a message, so there's no meaningful return).

Because of that, `&&` evaluates the left operand (outputs `1`), and immediately stops, because `undefined` is a falsy value. And `&&` looks for a falsy value and returns it, so it's done.
La llamada a `alert` retorna `undefined` (solo muestra un mensaje, así que no hay un valor que retornar relevante)

Debido a ello, `&&` evalua el operando de la izquierda (imprime `1`) e inmediatamente se detiene porque `undefined` es un valor falso. Como `&&` busca un valor falso y lo retorna, terminamos.
4 changes: 2 additions & 2 deletions 1-js/02-first-steps/11-logical-operators/4-alert-and/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 3

---

# What is the result of AND'ed alerts?
# ¿Cuál es el resultado de las alertas aplicadas al operador AND?

What will this code show?
¿Cuál será la salida del siguiente código?

```js
alert( alert(1) && alert(2) );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
The answer: `3`.
La respuesta: `3`.

```js run
alert( null || 2 && 3 || 4 );
```

The precedence of AND `&&` is higher than `||`, so it executes first.
La precedencia de AND `&&` es mayor que la de `||`, así que se ejecuta primero.

The result of `2 && 3 = 3`, so the expression becomes:
El resultado de `2 && 3 = 3`, por lo que la expresión se convierte en:

```
null || 3 || 4
```

Now the result is the first truthy value: `3`.
Ahora el resultado será el primer valor verdadero: `3`.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# The result of OR AND OR
# El resultado de OR AND OR

What will the result be?
¿Cuál será el resultado?

```js
alert( null || 2 && 3 || 4 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ importance: 3

---

# Check the range between
# Comprueba el rango por dentro

Write an "if" condition to check that `age` is between `14` and `90` inclusively.
Escribe una condición "if" para comprobar que `age`(edad) está entre `14` y `90` inclusivamente.

"Inclusively" means that `age` can reach the edges `14` or `90`.
"Inclusivamente" significa que `age` puede llegar a ser uno de los extremos, `14` o `90`.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
The first variant:
La primer variante:

```js
if (!(age >= 14 && age <= 90))
```

The second variant:
La segunda variante:

```js
if (age < 14 || age > 90)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ importance: 3

---

# Check the range outside
# Comprueba el rango por fuera

Write an `if` condition to check that `age` is NOT between 14 and 90 inclusively.
Escribe una condición `if` para comprobar que `age` NO está entre 14 y 90 inclusivemente.

Create two variants: the first one using NOT `!`, the second one -- without it.
Crea dos variantes: la primera usando NOT `!`, y la segunda -- sin usarlo.
24 changes: 12 additions & 12 deletions 1-js/02-first-steps/11-logical-operators/8-if-question/solution.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
The answer: the first and the third will execute.
La respuesta: el primero y el tercero serán ejecutados.

Details:
Detalles:

```js run
// Runs.
// The result of -1 || 0 = -1, truthy
if (-1 || 0) alert( 'first' );
// Corre.
// El resultado de -1 || 0 = -1, valor verdadero
if (-1 || 0) alert( "primero" );

// Doesn't run
// -1 && 0 = 0, falsy
if (-1 && 0) alert( 'second' );
// No corre.
// -1 && 0 = 0, valor falso
if (-1 && 0) alert( "segundo" );

// Executes
// Operator && has a higher precedence than ||
// so -1 && 1 executes first, giving us the chain:
// Se ejecuta
// El operador && tiene mayor precedencia que ||
// Así que -1 && 1 se ejecuta primero, dándonos la cadena:
// null || -1 && 1 -> null || 1 -> 1
if (null || -1 && 1) alert( 'third' );
if (null || -1 && 1) alert( "tercero" );
```

12 changes: 6 additions & 6 deletions 1-js/02-first-steps/11-logical-operators/8-if-question/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ importance: 5

---

# A question about "if"
# Un pregunta acerca de "if"

Which of these `alert`s are going to execute?
¿Cuáles de estos `alert`s va a ejecutarse?

What will the results of the expressions be inside `if(...)`?
¿Cuáles serán los resultados de las expresiones dentro de `if(...)`?

```js
if (-1 || 0) alert( 'first' );
if (-1 && 0) alert( 'second' );
if (null || -1 && 1) alert( 'third' );
if (-1 || 0) alert( "primero" );
if (-1 && 0) alert( "segundo" );
if (null || -1 && 1) alert( "tercero" );
```

32 changes: 16 additions & 16 deletions 1-js/02-first-steps/11-logical-operators/9-check-login/solution.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@


```js run demo
let userName = prompt("Who's there?", '');
let userName = prompt("Quién está ahí?", "");

if (userName == 'Admin') {
if (userName == "Admin") {

let pass = prompt("Contraseña?", "");

let pass = prompt('Password?', '');

if (pass == 'TheMaster') {
alert( 'Welcome!' );
} else if (pass == '' || pass == null) {
alert( 'Canceled.' );
} else {
alert( 'Wrong password' );
}

} else if (userName == '' || userName == null) {
alert( 'Canceled' );
if (pass == "TheMaster") {
alert( "Bienvenido!" );
} else if (pass == "" || pass == null) {
alert( "Cancelado." );
} else {
alert( "Contraseña incorrecta" );
}

} else if (userName == "" || userName == null) {
alert( "Canceledo" );
} else {
alert( "I don't know you" );
alert( "No te conozco" );
}
```

Note the vertical indents inside the `if` blocks. They are technically not required, but make the code more readable.
Nota las sangrías verticales dentro de los bloques `if`. Técnicamente no son necesarias, pero facilitan la lectura del código.
20 changes: 10 additions & 10 deletions 1-js/02-first-steps/11-logical-operators/9-check-login/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ importance: 3

---

# Check the login
# Comprueba el inicio de sesión

Write the code which asks for a login with `prompt`.
Escribe un código que pregunte por el inicio de sesión con `propmt`.

If the visitor enters `"Admin"`, then `prompt` for a password, if the input is an empty line or `key:Esc` -- show "Canceled.", if it's another string -- then show "I don't know you".
Si el visitante ingresa `"Admin"`, entonces `prompt`(pregunta) por una contraseña, si la entrada es una linea vacía o `key:Esc` -- muestra "Cancelado.", si es otra cadena de texto -- entonces muestra "No te conozco".

The password is checked as follows:
La contraseña se comprueba de la siguiente manera:

- If it equals "TheMaster", then show "Welcome!",
- Another string -- show "Wrong password",
- For an empty string or cancelled input, show "Canceled."
- Si es igual a "TheMaster", entonces muestra "Bienvenido!",
- Si es otra cadena de texto -- muetra "Contraseña incorrecta",
- Para una cadena de texto vacía o una entrada cancelada, muestra "Cancelado."

The schema:
El esquema:

![](ifelse_task.png)

Please use nested `if` blocks. Mind the overall readability of the code.
Por favor usa bloques anidados de `if`. Piensa en la legibilidad general del código.

Hint: passing an empty input to a prompt returns an empty string `''`. Pressing `key:ESC` during a prompt returns `null`.
Pista: si se le pasa una entrada vacía a un prompt, retorna una cadena de texto vacía `''`. Presionando `key:ESC` durante un prompt retorna `null`.

[demo]
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy