Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

217
Vistas
Aviso de bucle cuando la entrada no es un mes

Estoy tratando de repetir el aviso cuando el usuario ingresa una entrada incorrecta. Busqué en Google e hice clic en casi todos los enlaces principales, probé while loop y for loop y simplemente no llegué a ninguna parte. Cuando uso el método que encontré de un bucle, ya no se puede hacer clic en mi botón o hace clic, pero cuando ingreso un mes no válido, no aparece ninguna alerta y no se repite. si alguien me puede señalar en la dirección correcta, o mostrarme lo que estoy haciendo mal, ¡lo agradecería mucho!

 function myFunction() { let text; let month = prompt("What month would you like to know about?","Please type your answer here").toLowerCase(); switch(month) { case "january": text = "There are 31 days in the month of January"; break; case "february": text = "There are 28 days in the month of february, and 29 days on a leap year!"; break; case "march": text = "There are 31 days in the month of March"; break; case "april": text = "There are 30 days in the month of April"; break; case "may": text = "There are 31 days in the month of May"; break; case "june": text = "There are 30 days in the month of June"; break; case "july": text = "There are 31 days in the month of July"; break; case "august": text = "There are 31 days in the month of August"; break; case "september": text = "There are 30 days in the month of September"; break; case "october": text = "There are 31 days in the month of October"; break; case "november": text = "There are 30 days in the month of November"; break; case "december": text = "There are 31 days in the month of December"; break; } document.getElementById("days").innerHTML = text; }
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

en las declaraciones de cambio, puede establecer una condición default , que se ejecuta cuando ninguno de los casos proporcionados coincide con la condición. en su caso, puede llamar a myFunction dentro del caso predeterminado para "bucle".

por supuesto, esto no solicitará un aviso nuevamente cuando el usuario proporcione un mes válido.

 function myFunction() { let text = null; let month = prompt("What month would you like to know about?", "Please type your answer here").toLowerCase(); switch (month) { case "january": text = "There are 31 days in the month of January"; break; // fill all your cases default: text = "Incorrect input"; alert('Incorrect input, attempting prompt again'); myFunction(); } if (text) document.getElementById("days").innerHTML = text; // myFunction(); uncomment this if you want the loop to go on even when the user provides a valid month as input }

over 4 years ago · Santiago Trujillo Denunciar

0

Puedes intentar algo como esto

 const regularMonthConfig = { january: 31, march: 31, april: 30, may: 31, june: 30, july: 31, august: 31, september: 30, october: 31, november: 30, december: 31 } const february = 'There are 28 days in the month of february, and 29 days on a leap year!' const otherMonths = Object.fromEntries(Object.entries(regularMonthConfig).map(([month, days]) => [month, `There are ${days} days in the month of ${month}`])) const configMonths = { february, ...otherMonths } const ask = () => { let month = prompt("What month would you like to know about?", "Please type your answer here").toLowerCase(); const response = configMonths[month.toLowerCase()]|| "invalid month retry" document.getElementById("days").innerHTML = response; } ask()
 <div id="days"></div>

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda