简体   繁体   中英

Simple If statement not working in javascript

I can't see any problems with my code. But the first if statement never runs the alert. Anyone know why? And the function always returns zero whenever it runs even when I know holidays[i] == myDate . I think my if 's aren't working for som reason. Thanks in advance for any insight.

function checkForHoliday(date) {
    var myDate = new Date(date);

    for (var i = 0; i <= 9; i++) {

        if (i == 5) {
            alert(holidays[i]); 
        }

        if (holidays[i] == myDate) {
            return 1;
        }
        else {
            return 0;
        }
    }

}

*edit

changed else to:

else if (holidays[i] != myDate && i == 9 { return 0; }

You're using a return in the else . That's going to end the function.

The logic is wrong, the function always return either 0 or 1 when i is 0; So i never increases to 5.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM