Why does my if statement always evaluate to true?

Posted by Pobe on Stack Overflow See other posts from Stack Overflow or by Pobe
Published on 2012-11-10T16:38:01Z Indexed on 2012/11/10 17:00 UTC
Read the original article Hit count: 167

Filed under:
|

I need to go through the months of the year and find out if the last day of the month is 28, 29, 30 or 31. My problem is that the first if statement always evaluates to true:

MOIS_I = 31
if (mois == "Janvier" || "Mars" || "Mai" || "Juillet" || "Août" || "Octobre" || "Décembre" || "1" || "3" || "5" || "7" || "8" || "10" || "12" || "01" || "03" || "05" || "07" || "08") {
            window.alert("Le mois " + mois + " de l'année " + annee + " compte " + MOIS_I + " jours ");
}

Also, it seems like it is necessary to do if (mois == "Janver" || mois == "Février" || ... ) and so on, but I wanted to know if there was a better way to do it.

Here is the full code:

    var mois, annee, test4, test100, test400;
    const MOIS_P = 30;
    const MOIS_I = 31;
    const FEV_NORM = 28; 
    const FEV_BISSEX = 29;
    const TEST_4 = 4;
    const TEST_100 = 100;
    const TEST_400 = 400;

    mois = window.prompt("Entrez un mois de l'année", "");
    annee = window.prompt("Entrez l'année de ce mois", "");
    /* MOIS IMPAIRS */
    if (mois == "Janvier" || "Mars" || "Mai" || "Juillet" || "Août" || "Octobre" || "Décembre" || "1" || "3" || "5" || "7" || "8" || "10" || "12" || "01" || "03" || "05" || "07" || "08") {
            window.alert("Le mois " + mois + " de l'année " + annee + " compte " + MOIS_I + " jours ");
    /* MOIS PAIRS */
    } else if (mois == "Février" || "Avril" || "Juin" || "Septembre" || "Novembre" || "2" || "4" || "6" || "9" || "11" || "02" || "04" || "06" || "09") { 
        if (mois == "Février") {
            test4 = parseInt(annee) % TEST_4;
            test100 = parseInt(annee) % TEST_100;
            test400 = parseInt(annee) % TEST_400;
            if (test4 == 0) {
                if (test100 != 0) {
                    window.alert("Le mois " + mois + " de l'année " + annee + " compte " + FEV_BISSEX + " jours ");
                } else {
                    window.alert("Le mois " + mois + " de l'année " + annee + " compte " + FEV_NORM + " jours ");
                }
            } else if (test400 == 0) {
                    window.alert("Le mois " + mois + " de l'année " + annee + " compte " + FEV_BISSEX + " jours ");
            } else {
                window.alert("Le mois " + mois + " de l'année " + annee + " compte " + FEV_NORM + " jours ");
            }
        } else {
            window.alert("Le mois " + mois + " de l'année " + annee + " compte " + MOIS_P + " jours ");
        }           
    } else {
        window.alert("Apocalypse!");
    }

TEST_4, TEST_100, TEST_400 are to test if the year is a leap year (which means february has 29 days instead of 28).

Thank you!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about if-statement