Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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;

}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading