Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
Loop prompt when input isn't a month

I'm trying to loop the prompt when the user enters incorrect input. I googled and clicked almost all the top links, tried while loop and for loop and am just not getting anywhere. When I use the method i found of a loop, my button is no longer clickable or it clicks but when I input invalid month, no alert shows up, and it doesn't loop. if someone can point me in the right direction, or show me what Im doing wrong, I'd greatly appreciate it!!

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 Respostas
Responde à pergunta

0

in switch statements, you can set a default condition, which is executed when none of the cases provides matches the condition. in your case you can just call myFunction inside the default case to "loop".

of course this would not ask for a prompt again when the user provides a valid month.

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 Relatório

0

You can try something like this

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda