如何停止包含嵌套循环和 if else 语句的整个 javascript 代码?
How to stop whole javascript code containing nested loops and if else statements?
我是 javascript 的初学者。我写了我的第一个程序,它以特定格式写入日期,即“DOB=mm%2Fdd%2Fyyyy”并在控制台中给出输出。
var testdate = "DOB=06%2F07%2F2004"
var minyear = 2003;
for(var year = 2005; year>=minyear; year--){
if (year % 4 == 0 && year % 100 !== 0 || year % 400 == 0){
var maxmonth = 12;
for (var month = 1; month <= maxmonth; month++) {
var fmonth = ("0" + month).slice(-2);
if (fmonth == 01 || fmonth == 03 || fmonth == 05 || fmonth == 07 || fmonth == 08 || fmonth == 10 || fmonth == 12) {
var maxdate = 31;
for (var date = 1; date <= maxdate; date++) {
var fdate = ("0" + date).slice(-2);
var sentdate = "DOB=" + fmonth + "%2F"+ fdate + "%2F" + year
console.log(sentdate)
}
}
else if (fmonth == 02) {
var maxdate = 29;
for (var date = 1; date <= maxdate; date++) {
var fdate = ("0" + date).slice(-2);
var sentdate = "DOB=" + fmonth + "%2F"+ fdate + "%2F" + year
console.log(sentdate)
}
}
else {
var maxdate = 30;
for (var date = 1; date <= maxdate; date++) {
var fdate = ("0" + date).slice(-2);
var sentdate = "DOB=" + fmonth + "%2F"+ fdate + "%2F" + year
console.log(sentdate)
}
}
}
}
else {
var maxmonth = 12;
for (var month = 1; month <= maxmonth; month++) {
var fmonth = ("0" + month).slice(-2);
if (fmonth == 01 || fmonth == 03 || fmonth == 05 || fmonth == 07 || fmonth == 08 || fmonth == 10 || fmonth == 12) {
var maxdate = 31;
for (var date = 1; date <= maxdate; date++) {
var fdate = ("0" + date).slice(-2);
var sentdate = "DOB=" + fmonth + "%2F"+ fdate + "%2F" + year
console.log(sentdate)
}
}
else if (fmonth == 02) {
var maxdate = 28;
for (var date = 1; date <= maxdate; date++) {
var fdate = ("0" + date).slice(-2);
var sentdate = "DOB=" + fmonth + "%2F"+ fdate + "%2F" + year
console.log(sentdate)
}
}
else {
var maxdate = 30;
for (var date = 1; date <= maxdate; date++) {
var fdate = ("0" + date).slice(-2);
var sentdate = "DOB=" + fmonth + "%2F"+ fdate + "%2F" + year
console.log(sentdate)
}
}
}
}
}
我希望当 sentdate 等于 testdate 时,该程序将在该点停止。意思是,这段代码不应该进一步执行。我找不到任何办法。任何人,请帮助我实现这一目标。
这里发生了三件事。
- 我删除了单独的闰年部分,并相应地更改了二月的天数
- 我使用了一个单独的函数来测试
testdate
- 为什么要使用一个单独的函数? b/c 否则我们将对相同的逻辑进行 3 次编码
- 为了打破这个嵌套循环,我在开头应用了一个循环标签
dateloop:
。然后当找到匹配项时,我们就跳出:break dateloop;
var testdate = "DOB=06%2F07%2F2004"
function matchesTestDate(date) {
return testdate == date;
}
var minyear = 2003;
dateloop: // this is the label for your loop
for (var year = 2005; year >= minyear; year--) {
var maxmonth = 12;
for (var month = 1; month <= maxmonth; month++) {
var fmonth = ("0" + month).slice(-2);
if (fmonth == 01 || fmonth == 03 || fmonth == 05 || fmonth == 07 || fmonth == 08 || fmonth == 10 || fmonth == 12) {
var maxdate = 31;
for (var date = 1; date <= maxdate; date++) {
var fdate = ("0" + date).slice(-2);
var sentdate = "DOB=" + fmonth + "%2F" + fdate + "%2F" + year
console.log(sentdate)
if (matchesTestDate(sentdate)) break dateloop;
}
} else if (fmonth == 02) {
var maxdate = (year % 4 == 0 && year % 100 !== 0 || year % 400 == 0) ? 29 : 28;
for (var date = 1; date <= maxdate; date++) {
var fdate = ("0" + date).slice(-2);
var sentdate = "DOB=" + fmonth + "%2F" + fdate + "%2F" + year
console.log(sentdate)
if (matchesTestDate(sentdate)) break dateloop;
}
} else {
var maxdate = 30;
for (var date = 1; date <= maxdate; date++) {
var fdate = ("0" + date).slice(-2);
var sentdate = "DOB=" + fmonth + "%2F" + fdate + "%2F" + year
console.log(sentdate)
if (matchesTestDate(sentdate)) break dateloop;
}
}
}
}
我是 javascript 的初学者。我写了我的第一个程序,它以特定格式写入日期,即“DOB=mm%2Fdd%2Fyyyy”并在控制台中给出输出。
var testdate = "DOB=06%2F07%2F2004"
var minyear = 2003;
for(var year = 2005; year>=minyear; year--){
if (year % 4 == 0 && year % 100 !== 0 || year % 400 == 0){
var maxmonth = 12;
for (var month = 1; month <= maxmonth; month++) {
var fmonth = ("0" + month).slice(-2);
if (fmonth == 01 || fmonth == 03 || fmonth == 05 || fmonth == 07 || fmonth == 08 || fmonth == 10 || fmonth == 12) {
var maxdate = 31;
for (var date = 1; date <= maxdate; date++) {
var fdate = ("0" + date).slice(-2);
var sentdate = "DOB=" + fmonth + "%2F"+ fdate + "%2F" + year
console.log(sentdate)
}
}
else if (fmonth == 02) {
var maxdate = 29;
for (var date = 1; date <= maxdate; date++) {
var fdate = ("0" + date).slice(-2);
var sentdate = "DOB=" + fmonth + "%2F"+ fdate + "%2F" + year
console.log(sentdate)
}
}
else {
var maxdate = 30;
for (var date = 1; date <= maxdate; date++) {
var fdate = ("0" + date).slice(-2);
var sentdate = "DOB=" + fmonth + "%2F"+ fdate + "%2F" + year
console.log(sentdate)
}
}
}
}
else {
var maxmonth = 12;
for (var month = 1; month <= maxmonth; month++) {
var fmonth = ("0" + month).slice(-2);
if (fmonth == 01 || fmonth == 03 || fmonth == 05 || fmonth == 07 || fmonth == 08 || fmonth == 10 || fmonth == 12) {
var maxdate = 31;
for (var date = 1; date <= maxdate; date++) {
var fdate = ("0" + date).slice(-2);
var sentdate = "DOB=" + fmonth + "%2F"+ fdate + "%2F" + year
console.log(sentdate)
}
}
else if (fmonth == 02) {
var maxdate = 28;
for (var date = 1; date <= maxdate; date++) {
var fdate = ("0" + date).slice(-2);
var sentdate = "DOB=" + fmonth + "%2F"+ fdate + "%2F" + year
console.log(sentdate)
}
}
else {
var maxdate = 30;
for (var date = 1; date <= maxdate; date++) {
var fdate = ("0" + date).slice(-2);
var sentdate = "DOB=" + fmonth + "%2F"+ fdate + "%2F" + year
console.log(sentdate)
}
}
}
}
}
我希望当 sentdate 等于 testdate 时,该程序将在该点停止。意思是,这段代码不应该进一步执行。我找不到任何办法。任何人,请帮助我实现这一目标。
这里发生了三件事。
- 我删除了单独的闰年部分,并相应地更改了二月的天数
- 我使用了一个单独的函数来测试
testdate
- 为什么要使用一个单独的函数? b/c 否则我们将对相同的逻辑进行 3 次编码 - 为了打破这个嵌套循环,我在开头应用了一个循环标签
dateloop:
。然后当找到匹配项时,我们就跳出:break dateloop;
var testdate = "DOB=06%2F07%2F2004"
function matchesTestDate(date) {
return testdate == date;
}
var minyear = 2003;
dateloop: // this is the label for your loop
for (var year = 2005; year >= minyear; year--) {
var maxmonth = 12;
for (var month = 1; month <= maxmonth; month++) {
var fmonth = ("0" + month).slice(-2);
if (fmonth == 01 || fmonth == 03 || fmonth == 05 || fmonth == 07 || fmonth == 08 || fmonth == 10 || fmonth == 12) {
var maxdate = 31;
for (var date = 1; date <= maxdate; date++) {
var fdate = ("0" + date).slice(-2);
var sentdate = "DOB=" + fmonth + "%2F" + fdate + "%2F" + year
console.log(sentdate)
if (matchesTestDate(sentdate)) break dateloop;
}
} else if (fmonth == 02) {
var maxdate = (year % 4 == 0 && year % 100 !== 0 || year % 400 == 0) ? 29 : 28;
for (var date = 1; date <= maxdate; date++) {
var fdate = ("0" + date).slice(-2);
var sentdate = "DOB=" + fmonth + "%2F" + fdate + "%2F" + year
console.log(sentdate)
if (matchesTestDate(sentdate)) break dateloop;
}
} else {
var maxdate = 30;
for (var date = 1; date <= maxdate; date++) {
var fdate = ("0" + date).slice(-2);
var sentdate = "DOB=" + fmonth + "%2F" + fdate + "%2F" + year
console.log(sentdate)
if (matchesTestDate(sentdate)) break dateloop;
}
}
}
}