Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
8 changes: 4 additions & 4 deletions 1-js/05-data-types/02-number/1-sum-interface/solution.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@


```js run demo
let a = +prompt("The first number?", "");
let b = +prompt("The second number?", "");
let a = +prompt("¿El primer número?", "");
let b = +prompt("¿El segundo número?", "");

alert( a + b );
```

Note the unary plus `+` before `prompt`. It immediately converts the value to a number.
Toma nota de el más unario `+` antes del `prompt`. Este convierte inmediatamente el valor a `number`.
Comment thread
joaquinelio marked this conversation as resolved.
Outdated

Otherwise, `a` and `b` would be string their sum would be their concatenation, that is: `"1" + "2" = "12"`.
De otra manera `a` and `b` serían `string`, y la suma, su concatenación: `"1" + "2" = "12"`.
6 changes: 3 additions & 3 deletions 1-js/05-data-types/02-number/1-sum-interface/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ importance: 5

---

# Sum numbers from the visitor
# Suma números para el visitante
Comment thread
joaquinelio marked this conversation as resolved.
Outdated

Create a script that prompts the visitor to enter two numbers and then shows their sum.
Crea un script que pida al visitante que ingrese dos números y muestre su suma.

[demo]

P.S. There is a gotcha with types.
P.D. Hay una triquiñuela con los tipos.
Comment thread
joaquinelio marked this conversation as resolved.
Outdated
18 changes: 9 additions & 9 deletions 1-js/05-data-types/02-number/2-why-rounded-down/solution.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
Internally the decimal fraction `6.35` is an endless binary. As always in such cases, it is stored with a precision loss.
Internamente, la fracción decimal `6.35` resulta en binario sin fin. Como siempre en estos casos, es almacenado con pérdida de precisión.

Let's see:
Veamos:

```js run
alert( 6.35.toFixed(20) ); // 6.34999999999999964473
```

The precision loss can cause both increase and decrease of a number. In this particular case the number becomes a tiny bit less, that's why it rounded down.
La pérdida de precisión puede causar que el número incremente o decremente. En este caso particular el número se vuelve ligeramente menor, por ello es redondeado hacia abajo.

And what's for `1.35`?
¿Y qué pasa con `1.35`?

```js run
alert( 1.35.toFixed(20) ); // 1.35000000000000008882
```

Here the precision loss made the number a little bit greater, so it rounded up.
Aquí la pérdida de precisión hace el número algo mayor, por ello redondea hacia arriba.

**How can we fix the problem with `6.35` if we want it to be rounded the right way?**
**¿Cómo podemos arreglar el problema con `6.35` si queremos redondearlo de manera correcta?**

We should bring it closer to an integer prior to rounding:
Debemos llevarlo más cerca de un entero antes del redondeo:

```js run
alert( (6.35 * 10).toFixed(20) ); // 63.50000000000000000000
```

Note that `63.5` has no precision loss at all. That's because the decimal part `0.5` is actually `1/2`. Fractions divided by powers of `2` are exactly represented in the binary system, now we can round it:
observa que `63.5` no tiene pérdida de precisión en absoluto. Esto es porque la parte decimal `0.5` es realmente `1/2`. Fracciones divididas por potencias de `2` son representadas exactamente en el sistema binario, ahora podemos redondearlo:
Comment thread
joaquinelio marked this conversation as resolved.
Outdated


```js run
alert( Math.round(6.35 * 10) / 10); // 6.35 -> 63.5 -> 64(rounded) -> 6.4
alert( Math.round(6.35 * 10) / 10); // 6.35 -> 63.5 -> 64(redondeado) -> 6.4
```

10 changes: 5 additions & 5 deletions 1-js/05-data-types/02-number/2-why-rounded-down/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ importance: 4

---

# Why 6.35.toFixed(1) == 6.3?
# ¿Por qué 6.35.toFixed(1) == 6.3?

According to the documentation `Math.round` and `toFixed` both round to the nearest number: `0..4` lead down while `5..9` lead up.
Según la documentación `Math.round` y `toFixed` redondean al número más cercano: `0..4` hacia abajo mientras `5..9` hacia arriba.

For instance:
Por ejemplo:

```js run
alert( 1.35.toFixed(1) ); // 1.4
```

In the similar example below, why is `6.35` rounded to `6.3`, not `6.4`?
En el ejemplo similar que sigue, ¿por qué `6.35` es redondeado a `6.3`, y no a `6.4`?

```js run
alert( 6.35.toFixed(1) ); // 6.3
```

How to round `6.35` the right way?
¿Cómo redondear `6.35` de manera correcta?

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function readNumber() {
let num;

do {
num = prompt("Enter a number please?", 0);
num = prompt("Ingrese un número por favor:", 0);
} while ( !isFinite(num) );

if (num === null || num === '') return null;
Expand All @@ -15,9 +15,9 @@ function readNumber() {
alert(`Read: ${readNumber()}`);
```

The solution is a little bit more intricate that it could be because we need to handle `null`/empty lines.
La solución es un poco más intrincada de lo que podría porque necesitamos manejar `null` y líneas vacías.
Comment thread
joaquinelio marked this conversation as resolved.
Outdated

So we actually accept the input until it is a "regular number". Both `null` (cancel) and empty line also fit that condition, because in numeric form they are `0`.
Entonces pedimos ingresos hasta que sea un "número regular". También `null` (cancel) y las líneas vacías encajan en esa condición porque un su forma numérica estos son `0`.
Comment thread
joaquinelio marked this conversation as resolved.
Outdated

After we stopped, we need to treat `null` and empty line specially (return `null`), because converting them to a number would return `0`.
Una vez detenido el ingreso, necesitamos tratar especialmente los casos `null` y línea vacía (return `null`), porque al convertirlos devolverían `0`.

8 changes: 4 additions & 4 deletions 1-js/05-data-types/02-number/3-repeat-until-number/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ importance: 5

---

# Repeat until the input is a number
# Repetir hasta que lo ingresado sea un número

Create a function `readNumber` which prompts for a number until the visitor enters a valid numeric value.
Crea una función `readNumber` que pida un número hasta que el visitante ingrese un valor numérico válido.

The resulting value must be returned as a number.
El valor resultante debe ser devuelto como number.

The visitor can also stop the process by entering an empty line or pressing "CANCEL". In that case, the function should return `null`.
El visitante puede también detener el proceso ingresando una linea vacía o presionando "CANCEL". En tal caso la función debe devolver `null`.

[demo]

10 changes: 5 additions & 5 deletions 1-js/05-data-types/02-number/4-endless-loop-error/solution.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
That's because `i` would never equal `10`.
Es porque `i` nunca sería igual a `10`.

Run it to see the *real* values of `i`:
Ejecuta esto para ver los valores *reales* de `i`:

```js run
let i = 0;
Expand All @@ -10,8 +10,8 @@ while (i < 11) {
}
```

None of them is exactly `10`.
Ninguno de ellos es exactamente `10`.

Such things happen because of the precision losses when adding fractions like `0.2`.
Tales cosas suceden por las pérdidas de precisión cuando sumamos decimales como `0.2`.

Conclusion: evade equality checks when working with decimal fractions.
Conclusión: evita chequeos de igualdad al trabajar con números decimales.
4 changes: 2 additions & 2 deletions 1-js/05-data-types/02-number/4-endless-loop-error/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 4

---

# An occasional infinite loop
# Un bucle infinito ocasional

This loop is infinite. It never ends. Why?
Este bucle es infinito. Nunca termina, ¿por qué?

```js
let i = 0;
Expand Down
10 changes: 5 additions & 5 deletions 1-js/05-data-types/02-number/8-random-min-max/solution.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
We need to "map" all values from the interval 0..1 into values from `min` to `max`.
Necesitamos hacer un "mapeo" de todos los valores del intervalo 0..1 a valores desde `min` a `max`.

That can be done in two stages:
Esto puede hacerse en dos pasos:

1. If we multiply a random number from 0..1 by `max-min`, then the interval of possible values increases `0..1` to `0..max-min`.
2. Now if we add `min`, the possible interval becomes from `min` to `max`.
1. Si multiplicamos el número aleatorio 0..1 por `max-min`, entonces el intervalo de valores posibles va de `0..1` a `0..max-min`.
2. Ahora si sumamos `min`, el intervalo posible se vuelve desde `min` a `max`.

The function:
La función:

```js run
function random(min, max) {
Expand Down
8 changes: 4 additions & 4 deletions 1-js/05-data-types/02-number/8-random-min-max/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ importance: 2

---

# A random number from min to max
# Un número aleatorio entre min y max

The built-in function `Math.random()` creates a random value from `0` to `1` (not including `1`).
La función incorporada `Math.random()` crea un valor aleatorio entre `0` y `1` (no incluyendo `1`).

Write the function `random(min, max)` to generate a random floating-point number from `min` to `max` (not including `max`).
Escribe una función `random(min, max)` para generar un número de punto flotante entre `min` y `max` (no incluyendo `max`).

Examples of its work:
Ejemplos de su funcionamiento:

```js
alert( random(1, 5) ); // 1.2345623452
Expand Down
34 changes: 17 additions & 17 deletions 1-js/05-data-types/02-number/9-random-int-min-max/solution.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The simple but wrong solution
# La solución simple pero equivocada

The simplest, but wrong solution would be to generate a value from `min` to `max` and round it:
La solución más simple, pero equivocada, sería generar un valor entre `min` y `max` y redondearlo:

```js run
function randomInteger(min, max) {
Expand All @@ -11,23 +11,23 @@ function randomInteger(min, max) {
alert( randomInteger(1, 3) );
```

The function works, but it is incorrect. The probability to get edge values `min` and `max` is two times less than any other.
La función trabaja, pero es incorrecta. La probabilidad de obtener los valores extremos `min` y `max` es la mitad de la de los demás.
Comment thread
joaquinelio marked this conversation as resolved.
Outdated

If you run the example above many times, you would easily see that `2` appears the most often.
Si ejecutas el ejemplo que sigue muchas veces, fácilmente verás que `2` aparece más a menudo.

That happens because `Math.round()` gets random numbers from the interval `1..3` and rounds them as follows:
Esto ocurre porque `Math.round()` obtiene los números del intervalo `1..3` y los redondea como sigue:

```js no-beautify
values from 1 ... to 1.4999999999 become 1
values from 1.5 ... to 2.4999999999 become 2
values from 2.5 ... to 2.9999999999 become 3
valores desde 1 ... hasta 1.4999999999 se vuelven 1
valores desde 1.5 ... hasta 2.4999999999 se vuelven 2
valores desde 2.5 ... hasta 2.9999999999 se vuelven 3
```

Now we can clearly see that `1` gets twice less values than `2`. And the same with `3`.
Ahora podemos ver claramente que `1` obtiene la mitad de valores que `2`. Y lo mismo con `3`.

# The correct solution
# La solución correcta

There are many correct solutions to the task. One of them is to adjust interval borders. To ensure the same intervals, we can generate values from `0.5 to 3.5`, thus adding the required probabilities to the edges:
Hay muchas soluciones correctas para la tarea. una es ajustar los bordes del intervalo. Para asegurarse los mismos intervalos, podemos generar valores entre `0.5 a 3.5`, así sumando las probabilidades requeridas a los extremos:

```js run
*!*
Expand All @@ -41,7 +41,7 @@ function randomInteger(min, max) {
alert( randomInteger(1, 3) );
```

An alternative way could be to use `Math.floor` for a random number from `min` to `max+1`:
Una alternativa es el uso de `Math.floor` para un número aleatorio entre `min` y `max+1`:

```js run
*!*
Expand All @@ -55,12 +55,12 @@ function randomInteger(min, max) {
alert( randomInteger(1, 3) );
```

Now all intervals are mapped this way:
Ahora todos los intervalos son mapeados de esta forma:

```js no-beautify
values from 1 ... to 1.9999999999 become 1
values from 2 ... to 2.9999999999 become 2
values from 3 ... to 3.9999999999 become 3
valores desde 1 ... hasta 1.9999999999 se vuelven 1
valores desde 2 ... hasta 2.9999999999 se vuelven 2
valores desde 3 ... hasta 3.9999999999 se vuelven 3
```

All intervals have the same length, making the final distribution uniform.
Todos los intervalos tienen el mismo largo, haciendo la distribución final uniforme.
10 changes: 5 additions & 5 deletions 1-js/05-data-types/02-number/9-random-int-min-max/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ importance: 2

---

# A random integer from min to max
# Un entero aleatorio entre min y max

Create a function `randomInteger(min, max)` that generates a random *integer* number from `min` to `max` including both `min` and `max` as possible values.
Crea una función `randomInteger(min, max)` que genere un número *integer* aleatorio entre `min` y `max` incluyendo ambos, `min` y `max`, como valores posibles.
Comment thread
joaquinelio marked this conversation as resolved.
Outdated

Any number from the interval `min..max` must appear with the same probability.
Todo número del intervalo `min..max` debe aparecer con la misma probabilidad.


Examples of its work:
Ejemplos de funcionamiento:

```js
alert( randomInteger(1, 5) ); // 1
alert( randomInteger(1, 5) ); // 3
alert( randomInteger(1, 5) ); // 5
```

You can use the solution of the [previous task](info:task/random-min-max) as the base.
Puedes usar la solución de la [tarea previa](info:task/random-min-max) como base.
Loading