Skip to content

Basic operators, maths #314

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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
11 changes: 5 additions & 6 deletions 1-js/02-first-steps/08-operators/1-increment-order/solution.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

The answer is:
La respuesta es:

- `a = 2`
- `b = 2`
Expand All @@ -9,10 +9,9 @@ The answer is:
```js run no-beautify
let a = 1, b = 1;

alert( ++a ); // 2, prefix form returns the new value
alert( b++ ); // 1, postfix form returns the old value
alert( ++a ); // 2, la forma de prefijo devuelve el nuevo valor
alert( b++ ); // 1, la forma de sufijo devuelve el antiguo valor

alert( a ); // 2, incremented once
alert( b ); // 2, incremented once
alert( a ); // 2, incrementado una vez
alert( b ); // 2, incrementado una vez
```

4 changes: 2 additions & 2 deletions 1-js/02-first-steps/08-operators/1-increment-order/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# The postfix and prefix forms
# Las formas sufijo y prefijo

What are the final values of all variables `a`, `b`, `c` and `d` after the code below?
¿Cuáles son los valores finales de todas las variables `a`, `b`, `c` y `d` después del código a continuación?

```js
let a = 1, b = 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
The answer is:
La respuesta es:

- `a = 4` (multiplied by 2)
- `x = 5` (calculated as 1 + 4)
- `a = 4` (multiplicado por 2)
- `x = 5` (calculado como 1 + 4)

4 changes: 2 additions & 2 deletions 1-js/02-first-steps/08-operators/2-assignment-result/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 3

---

# Assignment result
# Resultado de asignación

What are the values of `a` and `x` after the code below?
¿Cuáles son los valores de 'a' y 'x' después del código a continuación?

```js
let a = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ undefined + 1 = NaN // (6)
" \t \n" - 2 = -2 // (7)
```

1. The addition with a string `"" + 1` converts `1` to a string: `"" + 1 = "1"`, and then we have `"1" + 0`, the same rule is applied.
2. The subtraction `-` (like most math operations) only works with numbers, it converts an empty string `""` to `0`.
3. The addition with a string appends the number `5` to the string.
4. The subtraction always converts to numbers, so it makes `" -9 "` a number `-9` (ignoring spaces around it).
5. `null` becomes `0` after the numeric conversion.
6. `undefined` becomes `NaN` after the numeric conversion.
7. Space characters, are trimmed off string start and end when a string is converted to a number. Here the whole string consists of space characters, such as `\t`, `\n` and a "regular" space between them. So, similarly to an empty string, it becomes `0`.
1. La suma con una cadena `"" + 1` convierte `1` a un string: `"" + 1 = "1"`, y luego tenemos `"1" + 0`, la misma regla se aplica.
2. La resta `-` (como la mayoría de las operaciones matemáticas) sólo funciona con números, convierte una cadena vacía `""` a `0`.
3. La suma con una cadena concatena el número `5` a la cadena.
4. La resta siempre convierte a números, por lo tanto hace de `" -9 "` un número `-9` (ignorando los espacios que lo rodean).
5. `null` se convierte en `0` después de la conversión numérica.
6. `undefined` se convierte en `NaN` después de la conversión numérica.
7. Los caracteres de espacio se recortan al inicio y al final de la cadena cuando una cadena se convierte en un número. Aquí toda la cadena consiste en caracteres de espacio, tales como `\t`, `\n` y un espacio "común" entre ellos. Por lo tanto, pasa lo mismo que a una cadena vacía, se convierte en `0`.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ importance: 5

---

# Conversiones de Tipos
# Conversiones de tipos

¿Cuáles son los resultados de estas expresiones?

Expand All @@ -24,4 +24,4 @@ undefined + 1
" \t \n" - 2
```

Piensa cuidadosamente, luego escribe los resultados y compáralos con la respuesta.
Piensa bien, anótalos y luego compara con la respuesta.
24 changes: 12 additions & 12 deletions 1-js/02-first-steps/08-operators/4-fix-prompt/solution.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
The reason is that prompt returns user input as a string.
La razón es que la captura devuelve la entrada del usuario como una cadena.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
La razón es que la captura devuelve la entrada del usuario como una cadena.
La razón es que la captura devuelve lo que ingresa el usuario como una cadena.

Don makumi me convenció que ingresso es mas cool
estoy de acuerdo
aunque también te cambié el armado...


So variables have values `"1"` and `"2"` respectively.
Entonces las variables tienen valores `"1"` y `"2"` respectivamente.

```js run
let a = "1"; // prompt("First number?", 1);
let b = "2"; // prompt("Second number?", 2);
let a = "1"; // prompt("Primer número?", 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let a = "1"; // prompt("Primer número?", 1);
let a = "1"; // prompt("¿Primer número?", 1);

No me gusta la pregunta pero eso no es importante. PEro la apertura para cumplir con la RAE.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let a = "1"; // prompt("Primer número?", 1);
let a = "1"; // prompt("Primer número", 1);

Yo opino que va sin signo interrogativo pues no es una pregunta la que se hace, sino una solicitud

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

¿No pedís con una pregunta?

o así:

if (
+prompt( "Dame todo tu dinero", 10000 ) < 5 ;
kickHim();
...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

De hecho no... Leelo con tono de pregunta. @joaquinelio

let b = "2"; // prompt("Segundo número?", 2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let b = "2"; // prompt("Segundo número?", 2);
let b = "2"; // prompt("¿Segundo número?", 2);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let b = "2"; // prompt("Segundo número?", 2);
let b = "2"; // prompt("Segundo número", 2);

Misma puerca pero revolcada...


alert(a + b); // 12
```

What we should to is to convert strings to numbers before `+`. For example, using `Number()` or prepending them with `+`.
Lo que debemos hacer es convertir las cadenas de texto a números antes `+`. Por ejemplo, utilizando `Number()` o anteponiendo `+`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Lo que debemos hacer es convertir las cadenas de texto a números antes `+`. Por ejemplo, utilizando `Number()` o anteponiendo `+`.
Lo que debemos hacer es convertir las cadenas de texto a números antes del `+`. Por ejemplo, utilizando `Number()` o anteponiendo `+`.

hubiera aclarado binario + anteponiendo unario +
mal ilya
mentira, distrae.

o ante "+"


For example, right before `prompt`:
Por ejemplo, justo antes de `prompt`:

```js run
let a = +prompt("First number?", 1);
let b = +prompt("Second number?", 2);
let a = +prompt("Primer número?", 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let a = +prompt("Primer número?", 1);
let a = +prompt("¿Primer número?", 1);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let a = +prompt("Primer número?", 1);
let a = +prompt("Primer número", 1);

let b = +prompt("Segundo número?", 2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let b = +prompt("Segundo número?", 2);
let b = +prompt("¿Segundo número?", 2);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let b = +prompt("Segundo número?", 2);
let b = +prompt("Segundo número", 2);

Same...


alert(a + b); // 3
```

Or in the `alert`:
O en el `alert`:

```js run
let a = prompt("First number?", 1);
let b = prompt("Second number?", 2);
let a = prompt("Primer número?", 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let a = prompt("Primer número?", 1);
let a = prompt("¿Primer número?", 1);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let a = prompt("Primer número?", 1);
let a = prompt("Primer número", 1);

let b = prompt("Segundo número?", 2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let b = prompt("Segundo número?", 2);
let b = prompt("¿Segundo número?", 2);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let b = prompt("Segundo número?", 2);
let b = prompt("Segundo número", 2);

Same...


alert(+a + +b); // 3
```

Using both unary and binary `+` in the latest code. Looks funny, doesn't it?
Usar ambos unario y binario `+` en el último ejemplo, se ve raro no?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Usar ambos unario y binario `+` en el último ejemplo, se ve raro no?
Usar ambos `+`, el unario y el binario en el último ejemplo, se ve raro, ¿no?

mejor armada creo
aisla compl con , ,
ah, y apertura ¿

, ¿se ve raro o no? puaj no.

12 changes: 6 additions & 6 deletions 1-js/02-first-steps/08-operators/4-fix-prompt/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ importance: 5

---

# Fix the addition
# Corregir la adición

Here's a code that asks the user for two numbers and shows their sum.
Aquí hay un código que le pide al usuario dos números y muestra su suma.

It works incorrectly. The output in the example below is `12` (for default prompt values).
Funciona incorrectamente. El resultado en el ejemplo a continuación es `12` (para valores de captura predeterminados).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Funciona incorrectamente. El resultado en el ejemplo a continuación es `12` (para valores de captura predeterminados).
Funciona incorrectamente. El resultado en el ejemplo a continuación es `12` (para los valores de captura predeterminados).


Why? Fix it. The result should be `3`.
¿Por qué? Arreglalo. El resultado debería ser `3`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
¿Por qué? Arreglalo. El resultado debería ser `3`.
¿Por qué? Arréglalo. El resultado debería ser `3`.


```js run
let a = prompt("First number?", 1);
let b = prompt("Second number?", 2);
let a = prompt("Primer número?", 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let a = prompt("Primer número?", 1);
let a = prompt("¿Primer número?", 1);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let a = prompt("Primer número?", 1);
let a = prompt("Primer número", 1);

Otro!?!?!?

let b = prompt("Segundo número?", 2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let b = prompt("Segundo número?", 2);
let b = prompt("¿Segundo número?", 2);

otro??
UFA para qué me meti a corregir esta gilada... jja

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let b = prompt("Segundo número?", 2);
let b = prompt("Segundo número", 2);

Y otro..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

¿HAss VISTO?
Si hubiera sabido que eran tantoss, no me habría metido a "arreglarlos" ni valia la pena


alert(a + b); // 12
```
Loading