let num_cap=parseInt((Math.round(prompt("Enter the highest number for your Higher-Lower game:")))); while(isNaN(num_cap)) { alert("Input must be a number, try again."); num_cap=parseInt((Math.round(prompt("Enter the max number:")))); } while(num_cap<=1) { alert("Number must be greater than 1, try again."); num_cap=parseInt((Math.round(prompt("Enter the highest number for your Higher-Lower game:")))); }Soy nuevo en programación, específicamente javascript. Escribí un juego de conjeturas de mayor a menor y resolví casi todos los problemas que encontré en el código, además de un problema en el que si mi primer ciclo while se activa y nuevamente solicita al usuario que ingrese, si luego ingreso una entrada que debería activar el segundo ciclo while, todavía pasa de todos modos. Este problema no ocurre si simplemente comienzo ingresando una entrada que activa el segundo ciclo while sin activar el primero. Solo sucede si activé el primer ciclo while de antemano.
Debe verificar ambos requisitos (el número es válido y también mayor que 1) al mismo tiempo.
let num_cap=parseInt((Math.round(prompt("Enter the highest number for your Higher-Lower game:")))); while(isNaN(num_cap) || num_cap <= 1) { alert("Input must be a number larger than 1, try again."); num_cap=parseInt((Math.round(prompt("Enter the max number:")))); } Este es otro ejemplo que prefiero porque hace el análisis una vez, en un solo lugar, también la verificación de validación es más simple, solo num_cap > 1 :
let num_cap, isValid; do { num_cap=parseInt((Math.round(prompt("Enter the max number:")))); isValid = num_cap > 1; if(isValid) { break; } alert("Input must be a number larger than 1, try again."); } while(!isValid);