Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!