diff --git a/1-js/02-first-steps/12-while-for/1-loop-last-value/solution.md b/1-js/02-first-steps/12-while-for/1-loop-last-value/solution.md index 43ee4aad3..c6f783153 100644 --- a/1-js/02-first-steps/12-while-for/1-loop-last-value/solution.md +++ b/1-js/02-first-steps/12-while-for/1-loop-last-value/solution.md @@ -1,4 +1,4 @@ -The answer: `1`. +La respuesta: `1`. ```js run let i = 3; @@ -8,18 +8,18 @@ while (i) { } ``` -Every loop iteration decreases `i` by `1`. The check `while(i)` stops the loop when `i = 0`. +Cada iteración del bucle disminuye `i` en `1`. La comprobación `while(i)` detiene el bucle cuando `i = 0`. -Hence, the steps of the loop form the following sequence ("loop unrolled"): +Por consiguiente, los pasos del bucle forman la siguiente secuencia ("bucle desenrollado"). ```js let i = 3; -alert(i--); // shows 3, decreases i to 2 +alert(i--); // muestra 3, disminuye i a 2 -alert(i--) // shows 2, decreases i to 1 +alert(i--) // muestra 2, disminuye i a 1 -alert(i--) // shows 1, decreases i to 0 +alert(i--) // muestra 1, disminuye i a 0 -// done, while(i) check stops the loop +// listo, while(i) comprueba y detiene el bucle ``` diff --git a/1-js/02-first-steps/12-while-for/1-loop-last-value/task.md b/1-js/02-first-steps/12-while-for/1-loop-last-value/task.md index 3b847dfa2..b4ebf1c03 100644 --- a/1-js/02-first-steps/12-while-for/1-loop-last-value/task.md +++ b/1-js/02-first-steps/12-while-for/1-loop-last-value/task.md @@ -2,9 +2,9 @@ importance: 3 --- -# Last loop value +# Último valor del bucle -What is the last value alerted by this code? Why? +¿Cuál es el último valor alertado/mostrado por este código? ¿Por qué? ```js let i = 3; diff --git a/1-js/02-first-steps/12-while-for/2-which-value-while/solution.md b/1-js/02-first-steps/12-while-for/2-which-value-while/solution.md index 495359876..adc230e84 100644 --- a/1-js/02-first-steps/12-while-for/2-which-value-while/solution.md +++ b/1-js/02-first-steps/12-while-for/2-which-value-while/solution.md @@ -1,30 +1,30 @@ -The task demonstrates how postfix/prefix forms can lead to different results when used in comparisons. +La tarea demuestra cómo las formas de sufijo y prefijo pueden llevar a diferentes resultados cuando son usadas en comparaciones. -1. **From 1 to 4** +1. **Del 1 al 4** ```js run let i = 0; while (++i < 5) alert( i ); ``` - The first value is `i = 1`, because `++i` first increments `i` and then returns the new value. So the first comparison is `1 < 5` and the `alert` shows `1`. + El primer valor es `i = 1`, porque `++i` primero incrementa `i` y luego retorna el valor nuevo. Así que la primera comparación es `1 < 5` y el `alert` muestra `1`. - Then follow `2, 3, 4…` -- the values show up one after another. The comparison always uses the incremented value, because `++` is before the variable. + Entonces siguen `2, 3, 4…` -- los valores son mostrados uno tras otro. La comparación siempre usa el valor incrementado, porque `++` está antes de la variable. - Finally, `i = 4` is incremented to `5`, the comparison `while(5 < 5)` fails, and the loop stops. So `5` is not shown. -2. **From 1 to 5** + Finalmente, `i = 4` es incrementada a `5`, la comparación `while(5 < 5)` falla, y el bucle se detiene. Así que `5` no es mostrado. +2. **Del 1 al 5** ```js run let i = 0; while (i++ < 5) alert( i ); ``` - The first value is again `i = 1`. The postfix form of `i++` increments `i` and then returns the *old* value, so the comparison `i++ < 5` will use `i = 0` (contrary to `++i < 5`). + El primer valor es de nuevo `i = 1`. La forma del sufijo de `i++` incrementa `i` y luego retorna el valor *viejo*, así que la comparación `i++ < 5` usará `i = 0` (contrario a `++i < 5`). - But the `alert` call is separate. It's another statement which executes after the increment and the comparison. So it gets the current `i = 1`. + Pero la llamada a `alert` está separada. Es otra declaración, la cual se ejecuta luego del incremento y la comparación. Así que obtiene el `i = 1` actual. - Then follow `2, 3, 4…` + Luego siguen `2, 3, 4…` - Let's stop on `i = 4`. The prefix form `++i` would increment it and use `5` in the comparison. But here we have the postfix form `i++`. So it increments `i` to `5`, but returns the old value. Hence the comparison is actually `while(4 < 5)` -- true, and the control goes on to `alert`. + Detengámonos en `i = 4`. La forma del prefijo `++i` lo incrementaría y usaría `5` en la comparación. Pero aquí tenemos la forma del sufijo `i++`. Así que incrementa `i` a `5`, pero retorna el valor viejo. Por lo tanto, la comparación es en realidad `while(4 < 5)` -- verdadero, y el control sigue a `alert`. - The value `i = 5` is the last one, because on the next step `while(5 < 5)` is false. + El valor `i = 5` es el último, porque el siguiente paso `while(5 < 5)` es falso. diff --git a/1-js/02-first-steps/12-while-for/2-which-value-while/task.md b/1-js/02-first-steps/12-while-for/2-which-value-while/task.md index 298213237..2a9489eb3 100644 --- a/1-js/02-first-steps/12-while-for/2-which-value-while/task.md +++ b/1-js/02-first-steps/12-while-for/2-which-value-while/task.md @@ -2,19 +2,19 @@ importance: 4 --- -# Which values does the while loop show? +# ¿Qué valores serán mostrados por el bucle while? -For every loop iteration, write down which value it outputs and then compare it with the solution. +Para cada iteración del bucle, escribe qué valor será impreso y luego compáralo con la solución. -Both loops `alert` the same values, or not? +Ambos bucles ¿`alertan` los mismos valores? -1. The prefix form `++i`: +1. La forma de prefijo `++i`: ```js let i = 0; while (++i < 5) alert( i ); ``` -2. The postfix form `i++` +2. La forma de sufijo `i++` ```js let i = 0; diff --git a/1-js/02-first-steps/12-while-for/3-which-value-for/solution.md b/1-js/02-first-steps/12-while-for/3-which-value-for/solution.md index e2e28e75b..72950fd33 100644 --- a/1-js/02-first-steps/12-while-for/3-which-value-for/solution.md +++ b/1-js/02-first-steps/12-while-for/3-which-value-for/solution.md @@ -1,4 +1,4 @@ -**The answer: from `0` to `4` in both cases.** +**La respuesta: de `0 `a `4` en ambos casos.** ```js run for (let i = 0; i < 5; ++i) alert( i ); @@ -6,12 +6,12 @@ for (let i = 0; i < 5; ++i) alert( i ); for (let i = 0; i < 5; i++) alert( i ); ``` -That can be easily deducted from the algorithm of `for`: +Eso puede ser fácilmente deducido del algoritmo de `for`: -1. Execute once `i = 0` before everything (begin). -2. Check the condition `i < 5` -3. If `true` -- execute the loop body `alert(i)`, and then `i++` +1. Ejecutar `i = 0` una vez antes de todo (comienzo). +2. Comprobar la condición `i < 5`. +3. Si `true` -- ejecutar el cuerpo del bucle `alert(i)` y luego `i++`. -The increment `i++` is separated from the condition check (2). That's just another statement. +El incremento `i++` es separado de la comprobación de la condición (2). Es simplemente otra declaración. -The value returned by the increment is not used here, so there's no difference between `i++` and `++i`. +El valor retornado por el incremento no es usado aquí, así que no hay diferencia entre `i++` y `++i`. diff --git a/1-js/02-first-steps/12-while-for/3-which-value-for/task.md b/1-js/02-first-steps/12-while-for/3-which-value-for/task.md index bfefa63f5..b29f0b1e8 100644 --- a/1-js/02-first-steps/12-while-for/3-which-value-for/task.md +++ b/1-js/02-first-steps/12-while-for/3-which-value-for/task.md @@ -2,18 +2,18 @@ importance: 4 --- -# Which values get shown by the "for" loop? +# ¿Qué valores serán mostrados por el bucle "for"? -For each loop write down which values it is going to show. Then compare with the answer. +Para cada bucle, anota qué valores mostrará y luego compara las respuestas. -Both loops `alert` same values or not? +Ambos bucles ¿`alertan` los mismos valores? -1. The postfix form: +1. La forma del sufijo: ```js for (let i = 0; i < 5; i++) alert( i ); ``` -2. The prefix form: +2. La forma del prefijo: ```js for (let i = 0; i < 5; ++i) alert( i ); diff --git a/1-js/02-first-steps/12-while-for/4-for-even/solution.md b/1-js/02-first-steps/12-while-for/4-for-even/solution.md index e8e66bb47..8db6872d3 100644 --- a/1-js/02-first-steps/12-while-for/4-for-even/solution.md +++ b/1-js/02-first-steps/12-while-for/4-for-even/solution.md @@ -8,4 +8,4 @@ for (let i = 2; i <= 10; i++) { } ``` -We use the "modulo" operator `%` to get the remainder and check for the evenness here. +Usamos el operador "modulo" `%` para conseguir el resto y comprobar la paridad. diff --git a/1-js/02-first-steps/12-while-for/4-for-even/task.md b/1-js/02-first-steps/12-while-for/4-for-even/task.md index ff34e7e40..4117fb442 100644 --- a/1-js/02-first-steps/12-while-for/4-for-even/task.md +++ b/1-js/02-first-steps/12-while-for/4-for-even/task.md @@ -2,8 +2,8 @@ importance: 5 --- -# Output even numbers in the loop +# Muestra números pares en el bucle -Use the `for` loop to output even numbers from `2` to `10`. +Usa el bucle `for` para mostrar números pares del `2` al `10`. [demo] diff --git a/1-js/02-first-steps/12-while-for/5-replace-for-while/solution.md b/1-js/02-first-steps/12-while-for/5-replace-for-while/solution.md index 612cf559c..5579686ef 100644 --- a/1-js/02-first-steps/12-while-for/5-replace-for-while/solution.md +++ b/1-js/02-first-steps/12-while-for/5-replace-for-while/solution.md @@ -3,7 +3,7 @@ ```js run let i = 0; while (i < 3) { - alert( `number ${i}!` ); + alert( `número ${i}!` ); i++; } ``` diff --git a/1-js/02-first-steps/12-while-for/5-replace-for-while/task.md b/1-js/02-first-steps/12-while-for/5-replace-for-while/task.md index 0c69d9c2d..3da7d1b9d 100644 --- a/1-js/02-first-steps/12-while-for/5-replace-for-while/task.md +++ b/1-js/02-first-steps/12-while-for/5-replace-for-while/task.md @@ -2,13 +2,13 @@ importance: 5 --- -# Replace "for" with "while" +# Reemplaza "for" por "while" -Rewrite the code changing the `for` loop to `while` without altering its behavior (the output should stay same). +Reescribe el código cambiando el bucle `for` a `while` sin alterar su comportamiento (la salida debería ser la misma). ```js run for (let i = 0; i < 3; i++) { - alert( `number ${i}!` ); + alert( `número ${i}!` ); } ``` diff --git a/1-js/02-first-steps/12-while-for/6-repeat-until-correct/solution.md b/1-js/02-first-steps/12-while-for/6-repeat-until-correct/solution.md index 2e04a78c4..1450fee07 100644 --- a/1-js/02-first-steps/12-while-for/6-repeat-until-correct/solution.md +++ b/1-js/02-first-steps/12-while-for/6-repeat-until-correct/solution.md @@ -3,13 +3,13 @@ let num; do { - num = prompt("Enter a number greater than 100?", 0); + num = prompt("Ingresa un número mayor a 100", 0); } while (num <= 100 && num); ``` -The loop `do..while` repeats while both checks are truthy: +El bucle `do..while` se repite mientras ambas condiciones sean verdaderas: -1. The check for `num <= 100` -- that is, the entered value is still not greater than `100`. -2. The check `&& num` is false when `num` is `null` or a empty string. Then the `while` loop stops too. +1. La condición `num <= 100` -- eso es, el valor ingresado aún no es mayor que `100`. +2. La condición `&& num` -- es falsa cuando `num` es `null` o una cadena de texto vaciá. Entonces el bucle `while` se detiene. -P.S. If `num` is `null` then `num <= 100` is `true`, so without the 2nd check the loop wouldn't stop if the user clicks CANCEL. Both checks are required. +PD. Si `num` es `null` entonces `num <= 100` es `true`, así que sin la segunda condición el bucle no se detendría si el usuario hace click en CANCELAR. Ambas comprobaciones son requeridas. diff --git a/1-js/02-first-steps/12-while-for/6-repeat-until-correct/task.md b/1-js/02-first-steps/12-while-for/6-repeat-until-correct/task.md index 0788ee76e..b9a1e3e95 100644 --- a/1-js/02-first-steps/12-while-for/6-repeat-until-correct/task.md +++ b/1-js/02-first-steps/12-while-for/6-repeat-until-correct/task.md @@ -2,12 +2,12 @@ importance: 5 --- -# Repeat until the input is correct +# Repite hasta que la entrada sea correcta -Write a loop which prompts for a number greater than `100`. If the visitor enters another number -- ask them to input again. +Escribe un bucle que solicite un número mayor que `100`. Si el usuario ingresa otro número -- pídele que ingrese un valor de nuevo. -The loop must ask for a number until either the visitor enters a number greater than `100` or cancels the input/enters an empty line. +El bucle debe pedir un número hasta que el usuario ingrese un número mayor que `100` o bien cancele la entrada/ingrese una linea vacía. -Here we can assume that the visitor only inputs numbers. There's no need to implement a special handling for a non-numeric input in this task. +Aquí podemos asumir que el usuario solo ingresará números. No hay necesidad de implementar un manejo especial para entradas no numéricas en esta tarea. [demo] diff --git a/1-js/02-first-steps/12-while-for/7-list-primes/solution.md b/1-js/02-first-steps/12-while-for/7-list-primes/solution.md index 9ff0663d7..bb53d37fc 100644 --- a/1-js/02-first-steps/12-while-for/7-list-primes/solution.md +++ b/1-js/02-first-steps/12-while-for/7-list-primes/solution.md @@ -1,29 +1,28 @@ -There are many algorithms for this task. +Hay muchos algoritmos para esta tarea. -Let's use a nested loop: +Usemos un bucle anidado. ```js -For each i in the interval { - check if i has a divisor from 1..i - if yes => the value is not a prime - if no => the value is a prime, show it +Por cada i en el intervalo { + comprobar si i tiene un divisor en 1..i + si tiene => el valor no es un primo + si no => el valor es un primo, mostrarlo } ``` - -The code using a label: +El código usando una etiqueta: ```js run let n = 10; nextPrime: -for (let i = 2; i <= n; i++) { // for each i... +for (let i = 2; i <= n; i++) { // por cada i... - for (let j = 2; j < i; j++) { // look for a divisor.. - if (i % j == 0) continue nextPrime; // not a prime, go next i + for (let j = 2; j < i; j++) { // buscar un divisor.. + if (i % j == 0) continue nextPrime; // no es primo, ir al próximo i } - alert( i ); // a prime + alert( i ); // primo } ``` -There's a lot of space to opimize it. For instance, we could look for the divisors from `2` to square root of `i`. But anyway, if we want to be really efficient for large intervals, we need to change the approach and rely on advanced maths and complex algorithms like [Quadratic sieve](https://en.wikipedia.org/wiki/Quadratic_sieve), [General number field sieve](https://en.wikipedia.org/wiki/General_number_field_sieve) etc. +Hay mucho lugar para la mejora. Por ejemplo, podríamos buscar por divisores desde `2` hasta la raíz cuadrada de `i`. Pero de todas formas, si queremos ser realmente eficientes para intervalos grandes, necesitamos cambiar el enfoque y confiar en matemáticas avanzadas y algoritmos complejos como [Criba cuadrática](https://es.wikipedia.org/wiki/Criba_cuadr%C3%A1tica), [Criba general del cuerpo de números](https://es.wikipedia.org/wiki/Criba_general_del_cuerpo_de_n%C3%BAmeros) etc. diff --git a/1-js/02-first-steps/12-while-for/7-list-primes/task.md b/1-js/02-first-steps/12-while-for/7-list-primes/task.md index 6344b9f6f..7de302fa7 100644 --- a/1-js/02-first-steps/12-while-for/7-list-primes/task.md +++ b/1-js/02-first-steps/12-while-for/7-list-primes/task.md @@ -2,16 +2,16 @@ importance: 3 --- -# Output prime numbers +# Muestra números primos -An integer number greater than `1` is called a [prime](https://en.wikipedia.org/wiki/Prime_number) if it cannot be divided without a remainder by anything except `1` and itself. +Un número entero mayor que `1` es llamado [primo](https://es.wikipedia.org/wiki/N%C3%BAmero_primo) si no puede ser dividido sin un resto por ningún número excepto `1` y él mismo. -In other words, `n > 1` is a prime if it can't be evenly divided by anything except `1` and `n`. +En otras palabras, `n > 1` es un primo si no puede ser divido exactamente por ningún número excepto `1` y `n`. -For example, `5` is a prime, because it cannot be divided without a remainder by `2`, `3` and `4`. +Por ejemplo, `5` es un primo, porque no puede ser divido exactamente por `2`, `3` y `4`. -**Write the code which outputs prime numbers in the interval from `2` to `n`.** +**Escribe el código que muestre números primos en el intervalo de `2` a `n`.** -For `n = 10` the result will be `2,3,5,7`. +Para `n = 10` el resultado será `2, 3, 5, 7`. -P.S. The code should work for any `n`, not be hard-tuned for any fixed value. +PD. El código debería funcionar para cualquier `n`, no debe estar programado para valores fijos. diff --git a/1-js/02-first-steps/12-while-for/article.md b/1-js/02-first-steps/12-while-for/article.md index 992c21af6..159c91f2f 100644 --- a/1-js/02-first-steps/12-while-for/article.md +++ b/1-js/02-first-steps/12-while-for/article.md @@ -1,54 +1,54 @@ -# Loops: while and for +# Bucles: while y for -We often need to repeat actions. +Usualmente necesitamos repetir acciones. -For example, outputting goods from a list one after another or just running the same code for each number from 1 to 10. +Por ejemplo, mostrar los elementos de una lista uno tras otro o simplemente ejecutar el mismo código para cada número del 1 al 10. -*Loops* are a way to repeat the same code multiple times. +Los *Bucles* son una forma de repetir el mismo código varias veces. -## The "while" loop +## El bucle "while" -The `while` loop has the following syntax: +El bucle `while` (mientras) tiene la siguiente sintaxis: ```js while (condition) { - // code - // so-called "loop body" + // código + // llamado "cuerpo del bucle" } ``` -While the `condition` is `true`, the `code` from the loop body is executed. +Mientras que la `condition` (condición) sea `true`, el `código` del cuerpo del bucle será ejecutado. -For instance, the loop below outputs `i` while `i < 3`: +Por ejemplo, el bucle debajo imprime `i` mientras que `i < 3`: ```js run let i = 0; -while (i < 3) { // shows 0, then 1, then 2 +while (i < 3) { // muestra 0, luego 1, luego 2 alert( i ); i++; } ``` -A single execution of the loop body is called *an iteration*. The loop in the example above makes three iterations. +Una sola ejecución del cuerpo del bucle es llamada *una iteración*. El bucle en el ejemplo de arriba realiza 3 iteraciones. -If `i++` was missing from the example above, the loop would repeat (in theory) forever. In practice, the browser provides ways to stop such loops, and in server-side JavaScript, we can kill the process. +Si `i++` no estuviera en el ejemplo de arriba, el bucle sería repetido (en teoría) eternamente. En la practica, el navegador proporciona maneras de detener dichos bucles, y en el JavaScript del lado del servidor, podemos eliminar el proceso. -Any expression or variable can be a loop condition, not just comparisons: the condition is evaluated and converted to a boolean by `while`. +Cualquier expresión o variable puede ser una condición del bucle, no solo comparaciones: la condición será evaluada y transformada a un booleano por `while`. -For instance, a shorter way to write `while (i != 0)` is `while (i)`: +Por ejemplo, una manera más corta de escribir `while (i != 0)` es `while (i)`: ```js run let i = 3; *!* -while (i) { // when i becomes 0, the condition becomes falsy, and the loop stops +while (i) { // cuando i sea 0, la condición será un valor falso, y el bucle se detendrá */!* alert( i ); i--; } ``` -````smart header="Brackets are not required for a single-line body" -If the loop body has a single statement, we can omit the brackets `{…}`: +````smart header="Las llaves no son requeridas para un cuerpo de una sola línea" +Si el cuerpo del bucle tiene una sola sentencia, podemos omitir las llaves `{…}`: ```js run let i = 3; @@ -58,19 +58,19 @@ while (i) alert(i--); ``` ```` -## The "do..while" loop +## El bucle "do..while" -The condition check can be moved *below* the loop body using the `do..while` syntax: +La comprobación de la condición puede ser movida *debajo* del cuerpo del bucle usando la sintaxis de `do..while`: ```js do { - // loop body + // cuerpo del bucle } while (condition); ``` -The loop will first execute the body, then check the condition, and, while it's truthy, execute it again and again. +El bucle primero ejecuta el cuerpo, luego comprueba la condición, y, mientras sea un valor verdadero, la ejecuta una y otra vez. -For example: +Por ejemplo: ```js run let i = 0; @@ -80,107 +80,106 @@ do { } while (i < 3); ``` -This form of syntax should only be used when you want the body of the loop to execute **at least once** regardless of the condition being truthy. Usually, the other form is preferred: `while(…) {…}`. +Esta sintaxis solo debería ser usada cuando quieres que el cuerpo del bucle sea ejecutado **al menos una vez** sin importar que la condición sea verdadera. Usualmente, se prefiere la otra forma: `while(…) {…}`. -## The "for" loop +## El bucle "for" -The `for` loop is the most commonly used loop. +El bucle `for` es el bucle más comúnmente usado. -It looks like this: +Se ve así: ```js for (begin; condition; step) { - // ... loop body ... + // ... cuerpo del bucle ... } ``` -Let's learn the meaning of these parts by example. The loop below runs `alert(i)` for `i` from `0` up to (but not including) `3`: +Aprendamos el significado de cada parte con un ejemplo. El bucle debajo corre `alert(i)` para `i` desde `0` hasta (pero no incluyendo) `3`: ```js run -for (let i = 0; i < 3; i++) { // shows 0, then 1, then 2 +for (let i = 0; i < 3; i++) { // muestra 0, luego 1, luego 2 alert(i); } ``` -Let's examine the `for` statement part-by-part: +Vamos a examinar la declaración `for` parte por parte: -| part | | | +| parte | | | |-------|----------|----------------------------------------------------------------------------| -| begin | `i = 0` | Executes once upon entering the loop. | -| condition | `i < 3`| Checked before every loop iteration. If false, the loop stops. | -| step| `i++` | Executes after the body on each iteration but before the condition check. | -| body | `alert(i)`| Runs again and again while the condition is truthy. | +| comienzo | `i = 0` | Se ejecuta una vez al comienzo del bucle. | +| condición | `i < 3`| Comprobada antes de cada iteración del bucle. Si es falsa, el bucle se detiene. | +| paso | `i++` | Se ejecuta después del cuerpo en cada iteración pero antes de la comprobación de la condición. | +| cuerpo | `alert(i)`| Se ejecuta una y otra vez mientras que la condición sea verdadera. | - -The general loop algorithm works like this: +El algoritmo general del bucle funciona de esta forma: ``` -Run begin -→ (if condition → run body and run step) -→ (if condition → run body and run step) -→ (if condition → run body and run step) +Se ejecuta comenzar +→ (si condición → ejecutar cuerpo y ejecutar paso) +→ (si condición → ejecutar cuerpo y ejecutar paso) +→ (si condición → ejecutar cuerpo y ejecutar paso) → ... ``` -If you are new to loops, it could help to go back to the example and reproduce how it runs step-by-step on a piece of paper. +Si eres nuevo en bucles, te podría ayudar regresar al ejemplo y reproducir cómo se ejecuta paso por paso en una pedazo de papel. -Here's exactly what happens in our case: +Esto es lo que sucede exactamente en nuestro caso: ```js // for (let i = 0; i < 3; i++) alert(i) -// run begin +// se ejecuta comenzar let i = 0 -// if condition → run body and run step +// si condición → ejecutar cuerpo y ejecutar paso if (i < 3) { alert(i); i++ } -// if condition → run body and run step +// si condición → ejecutar cuerpo y ejecutar paso if (i < 3) { alert(i); i++ } -// if condition → run body and run step +// si condición → ejecutar cuerpo y ejecutar paso if (i < 3) { alert(i); i++ } -// ...finish, because now i == 3 +// ...finaliza, porque ahora i == 3 ``` -````smart header="Inline variable declaration" -Here, the "counter" variable `i` is declared right in the loop. This is called an "inline" variable declaration. Such variables are visible only inside the loop. +````smart header="Declaración de variable en línea" +Aquí, la variable "counter" `i` es declarada en el bucle. Esto es llamado una declaración de variable "en línea". Dichas variables son visibles solo dentro del bucle. ```js run for (*!*let*/!* i = 0; i < 3; i++) { alert(i); // 0, 1, 2 } -alert(i); // error, no such variable +alert(i); // error, no existe dicha variable ``` -Instead of defining a variable, we could use an existing one: +En vez de definir una variable, podemos usar una que ya exista: ```js run let i = 0; -for (i = 0; i < 3; i++) { // use an existing variable +for (i = 0; i < 3; i++) { // usa una variable existente alert(i); // 0, 1, 2 } -alert(i); // 3, visible, because declared outside of the loop +alert(i); // 3, visible, porque fue declarada fuera del bucle ``` ```` -### Skipping parts +### Saltando partes -Any part of `for` can be skipped. +Cualquier parte de `for` puede ser saltada. -For example, we can omit `begin` if we don't need to do anything at the loop start. +Por ejemplo, podemos omitir `comenzar` si no necesitamos realizar nada al comienzo del bucle. -Like here: +Como aquí: ```js run -let i = 0; // we have i already declared and assigned +let i = 0; // Ya tenemos i declarada y asignada -for (; i < 3; i++) { // no need for "begin" +for (; i < 3; i++) { // no hay necesidad de "comenzar" alert( i ); // 0, 1, 2 } ``` -We can also remove the `step` part: +También podemos eliminar la parte `paso`: ```js run let i = 0; @@ -190,32 +189,32 @@ for (; i < 3;) { } ``` -This makes the loop identical to `while (i < 3)`. +Esto hace al bucle idéntico a `while (i < 3)`. -We can actually remove everything, creating an infinite loop: +En realidad podemos eliminar todo, creando un bucle infinito: ```js for (;;) { - // repeats without limits + // se repite sin limites } ``` -Please note that the two `for` semicolons `;` must be present. Otherwise, there would be a syntax error. +Por favor, nota que los dos punto y coma `;` del `for` deben estar presentes. De otra manera, habría un error de sintaxis. -## Breaking the loop +## Rompiendo el bucle -Normally, a loop exits when its condition becomes falsy. +Normalmente, se sale de un bucle cuando la condición es falsa. -But we can force the exit at any time using the special `break` directive. +Pero podemos forzar una salida en cualquier momento usando la directiva especial `break`. -For example, the loop below asks the user for a series of numbers, "breaking" when no number is entered: +Por ejemplo, el bucle debajo le pide al usuario por una serie de números, "rompiendo" cuando un número no es ingresado: ```js let sum = 0; while (true) { - let value = +prompt("Enter a number", ''); + let value = +prompt("Ingresa un número", ''); *!* if (!value) break; // (*) @@ -224,35 +223,35 @@ while (true) { sum += value; } -alert( 'Sum: ' + sum ); +alert( 'Suma: ' + sum ); ``` -The `break` directive is activated at the line `(*)` if the user enters an empty line or cancels the input. It stops the loop immediately, passing control to the first line after the loop. Namely, `alert`. +La directiva `break` es activada en la línea `(*)` si el usuario ingresa una línea vacía o cancela la entrada. Detiene inmediatamente el bucle, pasando el control a la primera línea después de el bucle. En este caso, `alert`. -The combination "infinite loop + `break` as needed" is great for situations when a loop's condition must be checked not in the beginning or end of the loop, but in the middle or even in several places of its body. +La combinación "bucle infinito + `break` según sea necesario" es ideal en situaciones donde la condición del bucle debe ser comprobada no al inicio o al final de el bucle, sino a la mitad o incluso en varias partes del cuerpo. -## Continue to the next iteration [#continue] +## Continuar a la siguiente iteración [#continue] -The `continue` directive is a "lighter version" of `break`. It doesn't stop the whole loop. Instead, it stops the current iteration and forces the loop to start a new one (if the condition allows). +La directiva `continue` es una "versión más ligera" de `break`. No detiene todo el bucle. En su lugar, detiene la iteración actual y fuerza al bucle a comenzar una nueva (si la condición lo permite). -We can use it if we're done with the current iteration and would like to move on to the next one. +Podemos usarlo si hemos terminado con la iteración actual y nos gustaría movernos a la siguiente. -The loop below uses `continue` to output only odd values: +El bucle debajo usa `continue` para mostrar solo valores impares: ```js run no-beautify for (let i = 0; i < 10; i++) { - // if true, skip the remaining part of the body + // si es verdadero, saltar el resto del cuerpo *!*if (i % 2 == 0) continue;*/!* - alert(i); // 1, then 3, 5, 7, 9 + alert(i); // 1, luego 3, 5, 7, 9 } ``` -For even values of `i`, the `continue` directive stops executing the body and passes control to the next iteration of `for` (with the next number). So the `alert` is only called for odd values. +Para los valores pares de `i`, la directiva `continue` deja de ejecutar el cuerpo y pasa el control a la siguiente iteración de `for` (con el siguiente número). Así que el `alert` solo es llamado para valores impares. -````smart header="The `continue` directive helps decrease nesting" -A loop that shows odd values could look like this: +````smart header="La directiva `continue` ayuda a disminuir la anidación" +Un bucle que muestra valores impares podría verse así: ```js for (let i = 0; i < 10; i++) { @@ -264,15 +263,15 @@ for (let i = 0; i < 10; i++) { } ``` -From a technical point of view, this is identical to the example above. Surely, we can just wrap the code in an `if` block instead of using `continue`. +Desde un punto de vista técnico, esto es idéntico al ejemplo de arriba. Claro, podemos simplemente envolver el código en un bloque `if` en vez de usar `continue`. -But as a side-effect, this created one more level of nesting (the `alert` call inside the curly braces). If the code inside of`if` is longer than a few lines, that may decrease the overall readability. +Pero como efecto secundario, esto crearía un nivel más de anidación (la llamada a `alert` dentro de las llaves). Si el código dentro de `if` posee varias líneas, eso podría reducir la legibilidad en general. ```` -````warn header="No `break/continue` to the right side of '?'" -Please note that syntax constructs that are not expressions cannot be used with the ternary operator `?`. In particular, directives such as `break/continue` aren't allowed there. +````warn header="No `break/continue` a la derecha de '?'" +Por favor, nota que las construcciones de sintaxis que no son expresiones no pueden user usadas con el operador terniario `?`. En particular, directivas como `break/continue` no son permitidas aquí. -For example, if we take this code: +Por ejemplo, si tomamos este código: ```js if (i > 5) { @@ -282,52 +281,52 @@ if (i > 5) { } ``` -...and rewrite it using a question mark: +...y lo reescribimos usando un signo de interrogación: ```js no-beautify -(i > 5) ? alert(i) : *!*continue*/!*; // continue isn't allowed here +(i > 5) ? alert(i) : *!*continue*/!*; // continue no está permitida aquí ``` -...it stops working. Code like this will give a syntax error: +...deja de funcionar. Código como este generarán un error de sintaxis: -This is just another reason not to use the question mark operator `?` instead of `if`. +Esta es otra razón por la cual se recomienda no usar el operador de signo de interrogación `?` en lugar de `if`. ```` -## Labels for break/continue +## Etiquetas para break/continue -Sometimes we need to break out from multiple nested loops at once. +A veces necesitamos salirnos de múltiples bucles anidados al mismo tiempo. -For example, in the code below we loop over `i` and `j`, prompting for the coordinates `(i, j)` from `(0,0)` to `(3,3)`: +Por ejemplo, en el código debajo nosotros usamos un bucle sobre `i` y `j`, solicitando las coordenadas `(i,j)` de `(0,0)` a `(3,3)`: ```js run no-beautify for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { - let input = prompt(`Value at coords (${i},${j})`, ''); + let input = prompt(`Valor en las coordenadas (${i},${j})`, ''); - // what if I want to exit from here to Done (below)? + // ¿Y si quiero salir de aquí hacia Listo (debajo)? } } -alert('Done!'); +alert('Listo!'); ``` -We need a way to stop the process if the user cancels the input. +Necesitamos una manera de detener el proceso si el usuario cancela la entrada. -The ordinary `break` after `input` would only break the inner loop. That's not sufficient--labels, come to the rescue! +El `break` ordinario después de `input` solo nos sacaría del bucle interno. Eso no es suficiente--etiquetas, vengan al rescate! -A *label* is an identifier with a colon before a loop: +Una *etiqueta* es un identificar con dos puntos antes de un bucle: ```js labelName: for (...) { ... } ``` -The `break ` statement in the loop below breaks out to the label: +La declaración `break ` en el bucle debajo nos saca hacia la etiqueta: ```js run no-beautify *!*outer:*/!* for (let i = 0; i < 3; i++) { @@ -336,51 +335,51 @@ The `break ` statement in the loop below breaks out to the label: let input = prompt(`Value at coords (${i},${j})`, ''); - // if an empty string or canceled, then break out of both loops + // Si es una cadena de texto vacía o se canceló, entonces salir de ambos bucles if (!input) *!*break outer*/!*; // (*) - // do something with the value... + // hacer algo con el valor... } } -alert('Done!'); +alert('Listo!'); ``` -In the code above, `break outer` looks upwards for the label named `outer` and breaks out of that loop. +En el código de arriba, `break outer` mira hacia arriba por la etiquieta llamada `outer` y nos saca de dicho bucle. -So the control goes straight from `(*)` to `alert('Done!')`. +Así que el control va directamente de `(*)` a `alert('Listo!')`. -We can also move the label onto a separate line: +También podemos mover la etiqueta a una línea separada: ```js no-beautify outer: for (let i = 0; i < 3; i++) { ... } ``` -The `continue` directive can also be used with a label. In this case, code execution jumps to the next iteration of the labeled loop. +La directiva `continue` también puede usar usada con una etiqueta. En este caso, la ejecución del código salta a la siguiente iteración del bucle etiquetado. -````warn header="Labels are not a \"goto\"" -Labels do not allow us to jump into an arbitrary place in the code. +````warn header="Las etiquetas no son \"goto\"" +Las etiquetas no nos permiten saltar a un lugar arbitrario en el código. -For example, it is impossible to do this: +Por ejemplo, es imposible hacer esto: ```js -break label; // jumps to label? No. +break label; // saltar a label? No. label: for (...) ``` -A call to `break/continue` is only possible from inside a loop and the label must be somewhere above the directive. +Una llamada a `break/continue` solo es posible desde el interior del bucle y la etiqueta debe estar en alguna parte arriba de la directiva. ```` -## Summary +## Resumen -We covered 3 types of loops: +Cubrimos 3 tipos de bucles: -- `while` -- The condition is checked before each iteration. -- `do..while` -- The condition is checked after each iteration. -- `for (;;)` -- The condition is checked before each iteration, additional settings available. +- `while` -- La condición es comprobada antes de cada iteración. +- `do..while` -- La condición es comprobada después de cada iteración. +- `for (;;)` -- La condición es comprobada antes de cada iteración, con ajustes adicionales disponibles. -To make an "infinite" loop, usually the `while(true)` construct is used. Such a loop, just like any other, can be stopped with the `break` directive. +Para crear un bucle "infinito", usualmente se usa `while(true)`. Un bucle como este, tal y como cualquier otro, puede ser detenido con la directiva `break`. -If we don't want to do anything in the current iteration and would like to forward to the next one, we can use the `continue` directive. +Si no queremos hacer nada con la iteración actual y queremos adelantarnos a la siguiente, podemos usar la directiva `continue`. -`break/continue` support labels before the loop. A label is the only way for `break/continue` to escape a nested loop to go to an outer one. +`break/continue` soportan etiquetas antes del bucle. Una etiqueta es la única forma de usar `break/continue` para escapar de un bucle anidado para ir a uno exterior.