Pobe
Pobe

Reputation: 2793

Why does my if statement always evaluate to true?

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!

Upvotes: 1

Views: 798

Answers (6)

Steve Wellens
Steve Wellens

Reputation: 20620

This seems to work without tricky date math:

var SomeDate = new Date(2012, 10);  // November, 2012

SomeDate.setDate(SomeDate.getDate() - 1);  // go back one day

var DaysInMonth = SomeDate.getDate();

Upvotes: 0

inhan
inhan

Reputation: 7470

Here's a shortening suggestion for your if() conditions: the RegExp Object (although this will not answer your question).

/^(Janvier|Mars|Mai|Juillet|Août|(Octo|Décem)bre|0?[13578]|1[02])$/.test(mois))
// append i after the last forward slash if you want it not to respect case

is the same with

if (
mois == "Janvier" || mois == "Mars" || mois == "Mai" || mois == "Juillet" ||
mois == "Août" || mois == "Octobre" || mois == "Décembre" || mois == "1" ||
mois == "3" || mois == "5" || mois == "7" || mois == "8" || mois == "10" ||
mois == "12" || mois == "01" || mois == "03" || mois == "05" || mois == "07" ||
mois == "08"
)

and

if (/^(Février|Avril|Juin|(Sept|Nov)embre|0?[2469]|11)$/.test(mois))
// same thing about the case-insensitive (i) flag here...

is the same with

if (
mois == "Février" || mois == "Avril" || mois == "Juin" || mois == "Septembre" ||
mois == "Novembre" || mois == "2" || mois == "4" || mois == "6" || mois == "9" ||
mois == "11" || mois == "02" || mois == "04" || mois == "06" || mois == "09"
)

Upvotes: 0

jfriend00
jfriend00

Reputation: 707328

Here's a better way to check against a whole bunch of strings. You put all the allowed names in an object (often called a map) and then you can check to see if the name is in the map with one line of code:

var names = {
    "Janvier": true, "Mars": true, "Mai": true, "Juillet": true, 
    "Août": true, "Octobre": true, "Décembre": true, 
    "1": true, "3": true, "5": true, "7": true, "8": true, "10": true, "12": true, 
    "01": true, "03": true, "05": true, "07": true, "08": true
};

if (names[mois] === true) {
     window.alert("Le mois " + mois + " de l'année " + annee + " compte " + MOIS_I + " jours ");
}

Upvotes: -1

neu-rah
neu-rah

Reputation: 1693

do like this (the simple way)

var last_day=new Date(year,month,-1).getDate()

this will give the last day of month

Upvotes: 2

user2398029
user2398029

Reputation: 6937

Here is how I would go about it:

  1. Get month and convert it to a number. You'll probably want to use a hash to do this.
  2. Get year and convert it to a number, using parseInt() as you have done.
  3. If month % 2 == 1, then its an odd month:
    • If year % 400 == 0 || year % 100 == 0 || year % 4 == 0: handle leap years appropriately.
    • Else: return 30.
  4. Else: its an even months, return 31.

Upvotes: 0

NOtherDev
NOtherDev

Reputation: 9672

In terms of if statement, yes, you always need to add mois == part, otherwise you're checking boolean value of string "Fevrier" instead of comparing it to mois variable value.

In terms of the general result you're trying to achieve, there are probably a lot of easier ways, available for you in the standard library. If this is JavaScript, see e.g. this article.

Upvotes: 0

Related Questions