Javascript没有应用条件
Javascript not applying conditions?
编辑:我已将代码更新为新的代码。一切都很好,除了一行把这个月搞砸了,就是这一行:If (q <1 || q> 28) &&(m == 2) &&(q % 4 === 0) &&(!(q % 100))
所以我终于让我的计算器开始工作,但它不遵守我的条件。例如,如果我输入40 september,它会给我一天。显然这不应该发生。知道我哪里做错了吗?
感谢' function handleInput(form) {尝试{var form = document.getElementById("timeForm");
var strYears = form.years.value;
var strMonths = form.months.value;
var strDays = form.days.value;
var Y = parseInt(strYears);
var m = parseInt(strMonths);
var q = parseInt(strDays);
var h = "";
var output = "";
if (isNaN(Y))
throw ("Incorrect input. Years is not a number.");
if (Y < 0 || Y > 9999)
throw "Incorrect input. Years is out of expected range (0-9999).";
if (isNaN(m))
throw "Incorrect input. Months is not a number.";
if (m < 1 || m > 12)
throw "Incorrect input. Months is out of expected range (1-12).";
if (isNaN(q))
throw "Incorrect input. Days is not a number.";
if ((q < 1 || q > 31) && (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12))
throw "Incorrect input. Days is out of expected range (1-31).";
if ((q < 1 || q > 30) && (m == 4 || m == 6 || m == 9 || m == 11))
throw "Incorrect input. Days is out of expected range (1-30).";
if ((q < 1 || q > 28) && (m == 2) && ( q % 4 === 0 ) && (!( q % 100 )))
throw "Incorrect input. Days is out of expected range (1-28).";
if (m == 1 || m == 2) {
m = m + 12;
Y = Y - 1;
}
h = (q + Math.floor(13 * (m + 1) / 5) + Y + Math.floor(Y / 4)
- Math.floor(Y / 100) + Math.floor(Y / 400)) % 7;
{
if (h == 0)
h = "Saturday";
if (h == 1)
h = "Sunday";
if (h == 2)
h = "Monday";
if (h == 3)
h = "Tuesday";
if (h == 4)
h = "Wednesday";
if (h == 5)
h = "Thursday";
if (h == 6)
h = "Friday";
var output = h;
document.getElementById("output").innerHTML = output;
}
}
catch(error){
document.getElementById("output").innerHTML = "Error: " + error;
}
}`
应该没问题
function handleInput(form) {
try {
var form = document.getElementById("timeForm");
var strYears = form.years.value;
var strMonths = form.months.value;
var strDays = form.days.value;
var Y = parseInt(strYears);
var m = parseInt(strMonths);
var q = parseInt(strDays);
var h = "";
var output = "";
if (isNaN(Y))
throw ("Incorrect input. Years is not a number.");
if (Y < 0 || Y > 9999)
throw "Incorrect input. Years is out of expected range (0-9999).";
if (isNaN(m))
throw "Incorrect input. Months is not a number.";
if (m < 1 || m > 12)
throw "Incorrect input. Months is out of expected range (1-12).";
if (isNaN(q))
throw "Incorrect input. Days is not a number.";
if ((q < 1 || q > 31) && (m == 1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12))
throw "Incorrect input. Days is out of expected range (1-31).";
if ((q < 1 || q > 30) && (m == 4 || m== 6 || m== 9 || m==11))
throw "Incorrect input. Days is out of expected range (1-30).";
if ((q < 1 || q > 28) && (m == 2))
throw "Incorrect input. Days is out of expected range (1-28).";
if (m == 1 || m == 2) {
m = m + 12;
Y = Y - 1;
}
h = (q + Math.floor(13 * (m + 1) / 5) + Y + Math.floor(Y / 4)
- Math.floor(Y / 100) + Math.floor(Y / 400)) % 7;
{
if (h == 0)
h = "Saturday";
if (h == 1)
h = "Sunday";
if (h == 2)
h = "Monday";
if (h == 3)
h = "Tuesday";
if (h == 4)
h = "Wednesday";
if (h == 5)
h = "Thursday";
if (h == 6)
h = "Friday";
var output = h;
document.getElementById("output").innerHTML = output;
}
}
catch(error){
document.getElementById("output").innerHTML = "Error: " + error;
}
}
有很多错误。我希望我把它们都修好了。
// Code goes here
function handleInput(form) {
try {
var form = document.getElementById("timeForm");
var strYears = form.years.value;
var strMonths = form.months.value;
var strDays = form.days.value;
var Y = parseInt(strYears);
var m = parseInt(strMonths);
var q = parseInt(strDays);
var h = "";
var output = "";
if (isNaN(Y))
throw ("Incorrect input. Years is not a number.");
if (Y < 0 || Y > 9999)
throw "Incorrect input. Years is out of expected range (0-9999).";
if (isNaN(m))
throw "Incorrect input. Months is not a number.";
if (m < 1 || m > 12)
throw "Incorrect input. Months is out of expected range (1-12).";
if (isNaN(q))
throw "Incorrect input. Days is not a number.";
if ((q < 1 || q > 31) && ([1, 3, 5, 7, 8, 10, 12].indexOf(m) != -1))
throw "Incorrect input. Days is out of expected range (1-31).";
if ((q < 1 || q > 30) && ([4, 6, 9, 11].indexOf(m) != -1))
throw "Incorrect input. Days is out of expected range (1-30).";
if ((q < 1 || q > 28) && (m == 2))
throw "Incorrect input. Days is out of expected range (1-28).";
if (m == 1 || m == 2) {
m = m + 12;
Y = Y - 1;
}
h = (q + Math.floor(13 * (m + 1) / 5) + Y + Math.floor(Y / 4) - Math.floor(Y / 100) + Math.floor(Y / 400)) % 7; {
if (h == 0)
h = "Saturday";
if (h == 1)
h = "Sunday";
if (h == 2)
h = "Monday";
if (h == 3)
h = "Tuesday";
if (h == 4)
h = "Wednesday";
if (h == 5)
h = "Thursday";
if (h == 6)
h = "Friday";
var output = h;
document.getElementById("output").innerHTML = output;
}
} catch (error) {
document.getElementById("output").innerHTML = "Error: " + error;
}
}
相关文章:
- 有条件地在选项标记中应用布尔属性
- 我应该如何应用if-else条件来向具有不同用户角色的人显示不同的菜单
- 如何使用AngularJS应用基于Json字符串响应的条件CSS类
- CSS根据属性的可用性有条件地应用样式
- 在谷歌应用程序脚本中,如何在满足条件的情况下只发送一次电子邮件
- 根据角度 js 应用程序中的条件动态添加类
- Angular js ng repeat与条件ng类不应用css类
- 在输入后,有条件地应用Angular中的两个类之一
- 如何在 HTML5 中的日期选择器中对所选月份应用条件
- 在 AngularJS 应用程序中有条件地注入模块
- 如何将基于条件的样式应用于剑道 UI 网格
- 将过滤条件应用于地图的最干净方法
- 基于javascript条件应用样式
- 有条件地应用属性(AngularJS)
- 嵌入闪亮应用程序中的数据表的条件选择类型
- 筛选时,ng中应用的条件类重复使用$index不一致
- AngularJS:如何有条件地应用视图动画
- 有条件地应用!到JavaScript中的方法
- 如何将where条件应用于主干查询
- 根据ngDialog open中的条件应用css