while loops · bucles while
while repeats
while (test) { ... }runs again and again, as long as the test staystrue.- It checks the test before each pass — so the body might run zero times.
while repite
while (test) { ... }se ejecuta una y otra vez, mientras la prueba siga siendotrue.- Verifica la prueba antes de cada paso — por lo tanto, el cuerpo podría ejecutarse cero veces.
let n = 1;
while (n <= 3) {
console.log(n); // → 1, then 2, then 3
n++;
}
Make progress every pass
- Something inside must move the test toward
false. - If nothing changes, the loop never stops — an infinite loop (the Stop button saves you).
Hacer progreso en cada paso
- Algo dentro debe acercar la prueba a
false. - Si nada cambia, el bucle nunca termina — un bucle infinito (el botón Detener te salva).
let n = 1;
while (n < 50) {
n = n * 2; // 2, 4, 8, 16, 32, 64 — then stop
}
console.log(n); // → 64
Each pass doubles n · Cada paso duplica n
while re-checks its test each pass. n must change, or it runs forever. · while vuelve a verificar su condición en cada paso. n debe cambiar, o se ejecutará para siempre.
Common mistakes
- Change the condition inside the loop, or it runs forever.
- A
whilechecks its condition first, so it may run zero times.
Errores comunes
- Cambia la condición dentro del bucle, o se ejecutará para siempre.
- Un
whileverifica su condición primero, por lo que puede ejecutarse cero veces.
Now you try
- Use a
whileloop. - Press Check answer to test it.
Ahora tú intentas
- Usa un bucle
while. - Presiona Comprobar respuesta para verificarlo.
let n = 3. Use a while loop to count down · hacia abajo: log 3, 2, 1, one per line. · let n = 3. Usa un bucle while para contar hacia abajo: imprime 3, 2, 1, uno por línea.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
let n = 1. While n is 16 or less, log n then double it. Output should be 1, 2, 4, 8, 16. · let n = 1. Mientras n sea menor o igual a 16, imprime n y luego duplícalo. La salida debe ser 1, 2, 4, 8, 16.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Use a while loop to add up 1 to · hasta 4, then log the total (it is 10). · Usa un bucle while para sumar de 1 a 4, luego imprime el total (es 10).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.