Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #31964

Re: What is wrong with this If Else construction?

Newsgroups comp.lang.javascript
Date 2016-12-26 03:45 -0800
References <47744619-426f-4af1-8376-e2935a2570db@googlegroups.com> <XnsA6EA6C0A11982eejj99@194.109.6.166>
Message-ID <2ee99c33-38ae-4097-b847-c3b7fc6ad63d@googlegroups.com> (permalink)
Subject Re: What is wrong with this If Else construction?
From bart.smissaert@gmail.com

Show all headers | View raw


Verkeerde been uit bed gestapt?

I have reworked this and this is the full code:

function JS_GetAge(lDOB)

{

    var jDOB = new Date((lDOB - (25569)) * 86400 * 1000);
    //return jDOB;

    jDOBYear = jDOB.getFullYear();
    //return jDOBYear;

    jDOBMonth = jDOB.getMonth();  //will give January as 0. Fine, as we only compare months
    //return jDOBMonth;

    var todayDate = new Date();

    todayYear = todayDate.getFullYear();
    //return todayYear;

    todayMonth = todayDate.getMonth();
    //return todayMonth;

    age = todayYear - jDOBYear;
    //return age;

    //alert(age.toString(10));

    if (jDOBMonth > todayMonth) {
        age--;
        return age;
   }

    if (jDOBMonth < todayMonth) {
        return age;
   }

    jDOBDay = jDOB.getDate();
    todayDay = todayDate.getDate();

    if (jDOBDay < todayDay) {
         age--;
    }

    return age;

}

This runs but sometimes gives the wrong age where the age is 1 less than what it should be. I have checked all the values (as you can see from the commented out early returns) and all the values are fine. I thought maybe the problem is with non integer year values so I did Math.round(todayYear) etc. but that made no difference. I also checked if return age; indeed did a function exit and it did. So not sure what the problem is.

RBS

On Monday, 26 December 2016 09:37:22 UTC, Evertjan.  wrote:
> bart.smissaert@gmail.com wrote on 26 Dec 2016 in comp.lang.javascript:
> 
> > New to JavaScript and trying to calculate the date of birth from an
> > Excel date, provided as an integer value (1 Jan 1900 = 1). Having
> > trouble to get the right syntax for this block of code: 
> 
> There is no "THE right syntax",
> there is code that does not do what you want it to do,
> and that does.
> 
> >    If (today_month < M) {
> >        age--;
> >        }
> >     else  {
> >         F = parseInt(30.6001 * E);
> >         DOM = (B - (D + F)) + (Q - Z); //day of the month
> 
> You are talking in riddles, not declaring and not defining variables.
> 
> >         If (DOM > today_day) {
> >             age--;
> >       }
> >    }
> > 
> > Tried various constructions but no success sofar. 
> 
> You should first define what you want.
> Then define how you want to do it, perhaps in pseudocode.
> Then implement it in a way you understand.
> 
> > Note that the 2 lines
> > F = ....  and DOM = ... only need to run if (today_month < M) is false.
> 
> Lines don't run, they are executed, or not.
> 
> Try symplified:
> 
> 1 <script type='text/javascript'>
> 2 If (true) 
> 3  { alert('first line executes') }
> 4 else
> 5  { alert('second line executes') };
> 6 </script>
> 
> 
> > I tried with those 2 lines outside the If Else block, but no success
> > either.
> 
> Did you try debugging?
> 
> If so, you should have been aware that 'If' is not 'if'.
> 
> The Google Chrome console responds with:
> 
> "Uncaught SyntaxError: Unexpected token else   test.html:4"
> 
> Explanation: the 'else' on line 4 is unexpected because there is no 'if'!!!!
> 
> 'If' should be 'if' as javascript is case-sensitive,
> so you should think[!] case-sensitive too. 
>  
> > Thanks for any advice.
> 
> Using code you found on the web, 
> that you don't understand,
> is bound to fail you,
> and is bound to keep you in your self-professed newbee-status.
> 
> 
> -- 
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

What is wrong with this If Else construction? bart.smissaert@gmail.com - 2016-12-26 00:05 -0800
  Re: What is wrong with this If Else construction? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-12-26 10:37 +0100
    Re: What is wrong with this If Else construction? bart.smissaert@gmail.com - 2016-12-26 03:45 -0800
      Re: What is wrong with this If Else construction? Luuk <luuk@invalid.lan> - 2016-12-26 12:59 +0100
        Re: What is wrong with this If Else construction? bart.smissaert@gmail.com - 2016-12-26 04:02 -0800
          Re: What is wrong with this If Else construction? bart.smissaert@gmail.com - 2016-12-26 04:16 -0800
      Re: What is wrong with this If Else construction? me <mu@local.local> - 2016-12-26 13:34 +0100
        Re: What is wrong with this If Else construction? Bart Smissaert <bart.smissaert@gmail.com> - 2016-12-26 05:02 -0800
          Re: What is wrong with this If Else construction? Luuk <luuk@invalid.lan> - 2016-12-26 18:32 +0100
            Re: What is wrong with this If Else construction? Bart Smissaert <bart.smissaert@gmail.com> - 2016-12-26 15:32 -0800
              Re: What is wrong with this If Else construction? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-12-27 10:05 +0100
                Re: What is wrong with this If Else construction? Bart Smissaert <bart.smissaert@gmail.com> - 2016-12-27 03:27 -0800
                Re: What is wrong with this If Else construction? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-12-27 21:46 +0100
                Re: What is wrong with this If Else construction? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-12-29 18:36 +0100
                Re: What is wrong with this If Else construction? Dr J R Stockton <reply1600@merlyn.demon.co.uk.invalid> - 2016-12-28 23:16 +0000
              Re: What is wrong with this If Else construction? Luuk <luuk@invalid.lan> - 2016-12-27 22:28 +0100
                Re: What is wrong with this If Else construction? Gene Wirchenko <genew@telus.net> - 2016-12-27 15:07 -0800
                Re: What is wrong with this If Else construction? Luuk <luuk@invalid.lan> - 2016-12-28 19:37 +0100
      Re: What is wrong with this If Else construction? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-12-26 21:56 +0100
        Re: What is wrong with this If Else construction? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-12-26 22:14 +0100
          Re: What is wrong with this If Else construction? Gene Wirchenko <genew@telus.net> - 2016-12-26 14:12 -0800
            Re: What is wrong with this If Else construction? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-12-27 00:02 +0100
            Re: What is wrong with this If Else construction? Dr J R Stockton <reply1600@merlyn.demon.co.uk.invalid> - 2016-12-27 22:36 +0000
              Re: What is wrong with this If Else construction? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-12-28 10:36 +0100
  Re: What is wrong with this If Else construction? Luuk <luuk@invalid.lan> - 2016-12-26 17:00 +0100
    OT: Leap year bug in Excel (was: What is wrong with this If Else construction?) "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-12-26 17:12 +0100
  Re: What is wrong with this If Else construction? Dr J R Stockton <reply1600@merlyn.demon.co.uk.invalid> - 2016-12-27 22:52 +0000
    Re: What is wrong with this If Else construction? "Christoph M. Becker" <cmbecker69@arcor.de> - 2016-12-28 01:01 +0100
      Re: What is wrong with this If Else construction? $Bill <news@todbe.com> - 2016-12-27 22:19 -0800
        Re: What is wrong with this If Else construction? Tim Streater <timstreater@greenbee.net> - 2016-12-28 10:05 +0000
          Re: What is wrong with this If Else construction? [OT] $Bill <news@todbe.com> - 2016-12-28 13:59 -0800
            Re: What is wrong with this If Else construction? [OT] Tim Streater <timstreater@greenbee.net> - 2016-12-28 22:17 +0000

csiph-web