当输入不是月份时循环提示
Loop prompt when input isn't a month
我试图在用户输入错误时循环提示。我在谷歌上搜索并点击了几乎所有的顶部链接,尝试了 while 循环和 for 循环,但没有任何进展。当我使用我发现的循环方法时,我的按钮不再可单击或单击但当我输入无效月份时,没有显示任何警报,并且它不会循环。
如果有人能指出我正确的方向,或者告诉我我做错了什么,我将不胜感激!!
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;
}
在switch语句中,可以设置一个default
条件,当none个case满足条件时执行。在您的情况下,您只需在默认情况下调用 myFunction
即可“循环”。
当然这不会在用户提供有效月份时再次要求提示。
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
}
你可以试试这个
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>
我试图在用户输入错误时循环提示。我在谷歌上搜索并点击了几乎所有的顶部链接,尝试了 while 循环和 for 循环,但没有任何进展。当我使用我发现的循环方法时,我的按钮不再可单击或单击但当我输入无效月份时,没有显示任何警报,并且它不会循环。 如果有人能指出我正确的方向,或者告诉我我做错了什么,我将不胜感激!!
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;
}
在switch语句中,可以设置一个default
条件,当none个case满足条件时执行。在您的情况下,您只需在默认情况下调用 myFunction
即可“循环”。
当然这不会在用户提供有效月份时再次要求提示。
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
}
你可以试试这个
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>