lundi 21 juin 2021

How to stop whole javascript code containing nested loops and if else statements?

I am a beginner in javascript. I wrote my first program which writes dates in a specific format, i.e., "DOB=mm%2Fdd%2Fyyyy" and gives the output in console.

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)
                }
            }
        }
    }    
}                       

I want that whenvever sentdate becomes equal to testdate, this program will stop at that point. Means, this code shouldn't execute further. I couldn't find any way to do so. Anyone, please help me to achieve this.

Aucun commentaire:

Enregistrer un commentaire