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


Groups > comp.lang.javascript > #14441 > unrolled thread

the if condition not workiong properly

Started byGanesh <ganesh.vemakoti45@gmail.com>
First post2012-06-10 22:54 -0700
Last post2012-06-12 20:10 +0100
Articles 20 on this page of 23 — 10 participants

Back to article view | Back to comp.lang.javascript


Contents

  the if condition not workiong properly Ganesh <ganesh.vemakoti45@gmail.com> - 2012-06-10 22:54 -0700
    Re: the if condition not workiong properly Jason C <jwcarlton@gmail.com> - 2012-06-11 02:27 -0700
    Re: the if condition not workiong properly "Tom de Neef" <tdeneef@qolor.nl> - 2012-06-11 11:35 +0200
      Re: the if condition not workiong properly Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-06-11 13:12 +0200
        Re: the if condition not workiong properly John G Harris <john@nospam.demon.co.uk> - 2012-06-11 15:39 +0100
          Re: the if condition not workiong properly Dr J R Stockton <reply1224@merlyn.demon.co.uk.not.invalid> - 2012-06-12 20:34 +0100
            Re: the if condition not workiong properly Gene Wirchenko <genew@ocis.net> - 2012-06-12 20:47 -0700
              Re: the if condition not workiong properly Dr J R Stockton <reply1224@merlyn.demon.co.uk.not.invalid> - 2012-06-14 18:49 +0100
                Re: the if condition not workiong properly Gene Wirchenko <genew@ocis.net> - 2012-06-14 20:45 -0700
                  Re: the if condition not workiong properly "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-06-15 06:30 +0000
                    Re: the if condition not workiong properly Gene Wirchenko <genew@ocis.net> - 2012-06-15 09:42 -0700
                      Re: the if condition not workiong properly "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-06-15 18:38 +0000
                  Re: the if condition not workiong properly Stefan Weiss <krewecherl@gmail.com> - 2012-06-15 19:23 +0200
                    Re: the if condition not workiong properly Gene Wirchenko <genew@ocis.net> - 2012-06-15 12:54 -0700
                  Re: the if condition not workiong properly Dr J R Stockton <reply1224@merlyn.demon.co.uk.not.invalid> - 2012-06-16 21:46 +0100
                    Re: the if condition not workiong properly "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-06-17 08:03 +0000
            Re: the if condition not workiong properly John G Harris <john@nospam.demon.co.uk> - 2012-06-14 11:08 +0100
              Re: the if condition not workiong properly Gene Wirchenko <genew@ocis.net> - 2012-06-14 09:23 -0700
              Re: the if condition not workiong properly Dr J R Stockton <reply1224@merlyn.demon.co.uk.not.invalid> - 2012-06-15 19:56 +0100
                Re: the if condition not workiong properly Andrew Poulos <ap_prog@hotmail.com> - 2012-06-16 13:26 +1000
                  Re: the if condition not workiong properly John G Harris <john@nospam.demon.co.uk> - 2012-06-16 11:33 +0100
                  Re: the if condition not workiong properly Dr J R Stockton <reply1224@merlyn.demon.co.uk.not.invalid> - 2012-06-17 19:42 +0100
        Re: the if condition not workiong properly Dr J R Stockton <reply1224@merlyn.demon.co.uk.not.invalid> - 2012-06-12 20:10 +0100

Page 1 of 2  [1] 2  Next page →


#14441 — the if condition not workiong properly

FromGanesh <ganesh.vemakoti45@gmail.com>
Date2012-06-10 22:54 -0700
Subjectthe if condition not workiong properly
Message-ID<dfc89f22-155c-44f0-aaf6-9bb50f237395@t2g2000pbl.googlegroups.com>
<html>
<head>

	<script type="text/javascript" charset="utf-8" </script>

	function search()
        {
         var x=document.getElementById("From").selectedIndex;
         var y=document.getElementById("From").options;
         var z=y[x].text;

         if(z==RTC Complex)
          {
          document.write(z);
          }
        if(z==M.D.palem)
          {
           document.write(z);
          }
        if(z==Akkayapalem)
          {
          document.write(z);
          }
       document.write('hi');
          }
	</script>
</head>
	<body>
		  <fieldset style="margin-top: 50px">
	           <form name="Fruits">

	  	 From:<br>
	  	<select id="From">
        <option value="Apple">Apple</option>
        <option value="Mango">Mango</option>
        <option value="Orange">Orange</option></select><br>
	<br><input type="submit" name="SEARCH" value="SEARCH"
        onclick="search()" style="margin-left: 50px"/>
        </form>
	</fieldset>
	</body>
	</html>

[toc] | [next] | [standalone]


#14444

FromJason C <jwcarlton@gmail.com>
Date2012-06-11 02:27 -0700
Message-ID<68d48782-1d48-4450-bd9a-30f0d4d9ed93@googlegroups.com>
In reply to#14441
On Monday, June 11, 2012 1:54:49 AM UTC-4, Ganesh wrote:
> <html>
> <head>

I'm no pro or anything, so there might be a better way than what I'll say. But I did see a few syntax errors that could be messing you up.


> 	<script type="text/javascript" charset="utf-8" </script>

1. Why is the </script> here? You should just have a > after the "utf-8"

> 	function search()
>         {
>          var x=document.getElementById("From").selectedIndex;
>          var y=document.getElementById("From").options;
>          var z=y[x].text;

If all you want is the value of "From", then you could skip x and y, and just say:

var z = document.getElementById("From").value;

Or you could skip getElementById() altogether with:

var z = document.Fruits.From.value;


>          if(z==RTC Complex)

You didn't have "RTC Complex" in the sample, so I'm guessing this was accidentally left over from the live script. If that's the case, it will need to be in quotes since you're matching a string:

if (z == "RTC Complex")

If RTC Complex were a variable (which I don't think it could be, because of the space, but I could be wrong), you would leave it out of the quotes.

If you're actually wanting to write this to the screen, then it's not going to work quite like you want. It will write for a second, but then the form is submitted so it's going to go away immediately. I doubt that this was your final goal, though, so I won't go in to that too much.

[toc] | [prev] | [next] | [standalone]


#14445

From"Tom de Neef" <tdeneef@qolor.nl>
Date2012-06-11 11:35 +0200
Message-ID<4fd5bbd1$0$6946$e4fe514c@news2.news.xs4all.nl>
In reply to#14441
"Ganesh" <ganesh.vemakoti45@gmail.com> schreef in bericht 
news:dfc89f22-155c-44f0-aaf6-9bb50f237395@t2g2000pbl.googlegroups.com...
> <html>
> <head>
>
> <script type="text/javascript" charset="utf-8" </script>
>
> function search()
>        {
>         var x=document.getElementById("From").selectedIndex;
>         var y=document.getElementById("From").options;
>         var z=y[x].text;
>
>         if(z==RTC Complex)    //**** illegal statement
>          {
>          document.write(z);
>          }
>        if(z==M.D.palem)          //**** M is undefined, M.D illegal
>          {
>           document.write(z);
>          }
>        if(z==Akkayapalem)      //**** ???
>          {
>          document.write(z);
>          }
>       document.write('hi');
>          }
> </script>

Tom 

[toc] | [prev] | [next] | [standalone]


#14451

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-06-11 13:12 +0200
Message-ID<1597347.oMNUckLgyt@PointedEars.de>
In reply to#14445
Tom de Neef wrote:

> "Ganesh" <ganesh.vemakoti45@gmail.com> schreef in bericht
> news:dfc89f22-155c-44f0-aaf6-9bb50f237395@t2g2000pbl.googlegroups.com...

Please do not post attribution novels. <http://www.insideoe.com/>

>> <html>
>> <head>
>>
>> <script type="text/javascript" charset="utf-8" </script>
>>
>> function search()
>>        {
>>         var x=document.getElementById("From").selectedIndex;
>>         var y=document.getElementById("From").options;
>>         var z=y[x].text;
>>
>>         if(z==RTC Complex)    //**** illegal statement
>>          {
>>          document.write(z);
>>          }
>>        if(z==M.D.palem)          //**** M is undefined, M.D illegal
>>          {
>>           document.write(z);
>>          }
>>        if(z==Akkayapalem)      //**** ???
>>          {
>>          document.write(z);
>>          }
>>       document.write('hi');
>>          }
>> </script>

Please add comments *below* quoted text, not inside.  To me, your posting 
looked like one big (accidental) full quote at first (as is was colored so).

"Illegal" is nonsense in programming (except where software licenses are 
concerned).  Source code can be syntactically *invalid* or *valid*, and it 
can *work* or *not work*.

The first `if' statement is syntactically *invalid*.  The second property 
access *throws a TypeError exception*, and the third statement *may throw a 
ReferenceError exception* if `Akkayapalem' was not declared and is not a 
property of an object in the scope chain.


PointedEars
-- 
> If you get a bunch of authors […] that state the same "best practices"
> in any programming language, then you can bet who is wrong or right...
Not with javascript. Nonsense propagates like wildfire in this field.
  -- Richard Cornford, comp.lang.javascript, 2011-11-14

[toc] | [prev] | [next] | [standalone]


#14461

FromJohn G Harris <john@nospam.demon.co.uk>
Date2012-06-11 15:39 +0100
Message-ID<n1BUf3GqMg1PFwpq@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD>
In reply to#14451
On Mon, 11 Jun 2012 at 13:12:02, in comp.lang.javascript, Thomas
'PointedEars' Lahn wrote:
>Tom de Neef wrote:
>
>> "Ganesh" <ganesh.vemakoti45@gmail.com> schreef in bericht
>> news:dfc89f22-155c-44f0-aaf6-9bb50f237395@t2g2000pbl.googlegroups.com...
>
>Please do not post attribution novels.

Nonsense. Please stop saying this.


> <http://www.insideoe.com/>
  <snip>

This link takes you to a page that appears to say nothing about
attribution in news articles.


>"Illegal" is nonsense in programming

In colloquial English "illegal" has two meanings. One is something
criminal. The other, more general, is disobeying some rules.

Example, taken from the C++ standard :
  "Using these specifiers with type declarations is illegal in C++."


>(except where software licenses are
>concerned).

Breaking a software license is a breach of contract, not a crime.


>Source code can be syntactically *invalid* or *valid*,

Or incorrect or correct;
or wrong or right;
or non-conforming or conforming.


>and it
>can *work* or *not work*.
  <snip>

Too few cases : think Halting Theorem.


  John
-- 
John Harris

[toc] | [prev] | [next] | [standalone]


#14472

FromDr J R Stockton <reply1224@merlyn.demon.co.uk.not.invalid>
Date2012-06-12 20:34 +0100
Message-ID<d4joHGNgn51PFwfh@invalid.uk.co.demon.merlyn.invalid>
In reply to#14461
In comp.lang.javascript message <n1BUf3GqMg1PFwpq@J.A830F0FF37FB96852AD0
8924D9443D28E23ED5CD>, Mon, 11 Jun 2012 15:39:38, John G Harris
<john@nospam.demon.co.uk> posted:

>On Mon, 11 Jun 2012 at 13:12:02, in comp.lang.javascript, Thomas
>'PointedEars' Lahn wrote:

>>"Illegal" is nonsense in programming
>
>In colloquial English "illegal" has two meanings. One is something
>criminal. The other, more general, is disobeying some rules.
>
>Example, taken from the C++ standard :
>  "Using these specifiers with type declarations is illegal in C++."

Nevertheless, one should eschew its use in public; in particular, in
error messages that a normal respectable user might see.  Normal users
can be consternated by an assertion that an illegal operation has been
attempted or performed.  The English language contains many words ... (

>>(except where software licenses are
>>concerned).
>
>Breaking a software license is a breach of contract, not a crime.

and "license" is not one of them) ... which can be used instead.


>>Source code can be syntactically *invalid* or *valid*,

>>and it
>>can *work* or *not work*.
>  <snip>
>
>Too few cases : think Halting Theorem.

It can also work unreliably.  Consider

        D = new Date() ; D.setMonth(5)

which has about a 1 in 52 chance of doing what probably was not
expected; and consider the careless implementation of Zeller's
Congruence which was in a (draft?) RFC or similar in the nineteen-
nineties; and consider the implementation of Number.toString(B) in which
the final character was incremented if the remainder was a half or
more.

-- 
 (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk  Turnpike 6.05  WinXP.
 Web  <http://www.merlyn.demon.co.uk/> - FAQ-type topics, acronyms, and links.
 Command-prompt MiniTrue is useful for viewing/searching/altering files. Free,
 DOS/Win/UNIX now 2.0.6; see <URL:http://www.merlyn.demon.co.uk/pc-links.htm>.

[toc] | [prev] | [next] | [standalone]


#14475

FromGene Wirchenko <genew@ocis.net>
Date2012-06-12 20:47 -0700
Message-ID<ov2gt7p84ffdfdd7krf601rnt49cgemfuv@4ax.com>
In reply to#14472
On Tue, 12 Jun 2012 20:34:56 +0100, Dr J R Stockton
<reply1224@merlyn.demon.co.uk.not.invalid> wrote:

>In comp.lang.javascript message <n1BUf3GqMg1PFwpq@J.A830F0FF37FB96852AD0
>8924D9443D28E23ED5CD>, Mon, 11 Jun 2012 15:39:38, John G Harris
><john@nospam.demon.co.uk> posted:
>
>>On Mon, 11 Jun 2012 at 13:12:02, in comp.lang.javascript, Thomas
>>'PointedEars' Lahn wrote:
>
>>>"Illegal" is nonsense in programming
>>
>>In colloquial English "illegal" has two meanings. One is something
>>criminal. The other, more general, is disobeying some rules.
>>
>>Example, taken from the C++ standard :
>>  "Using these specifiers with type declarations is illegal in C++."
>
>Nevertheless, one should eschew its use in public; in particular, in
>error messages that a normal respectable user might see.  Normal users
>can be consternated by an assertion that an illegal operation has been
>attempted or performed.  The English language contains many words ... (

     "invalid" is not perfect either.  I read one story where a user
angrily asked, "Are you calling me an invalid?"

>>>(except where software licenses are
>>>concerned).

     Or data privacy laws, or ...

>>Breaking a software license is a breach of contract, not a crime.
>
>and "license" is not one of them) ... which can be used instead.

>>>Source code can be syntactically *invalid* or *valid*,
>
>>>and it
>>>can *work* or *not work*.
>>  <snip>
>>
>>Too few cases : think Halting Theorem.
>
>It can also work unreliably.  Consider
>
>        D = new Date() ; D.setMonth(5)
>
>which has about a 1 in 52 chance of doing what probably was not
>expected; and consider the careless implementation of Zeller's

     It depends how familiar you are with JavaScript.  If you are
expecting months from 1 to 12 as everywhere else that I have seen, the
probability is 1 that it is not what was expected.  I still think that
it is the stupidest language misdesign that I have run across.

>Congruence which was in a (draft?) RFC or similar in the nineteen-
>nineties; and consider the implementation of Number.toString(B) in which
>the final character was incremented if the remainder was a half or
>more.

     What was that?  "9" becoming ":" in ASCII?

Sincerely,

Gene Wirchenko

[toc] | [prev] | [next] | [standalone]


#14502

FromDr J R Stockton <reply1224@merlyn.demon.co.uk.not.invalid>
Date2012-06-14 18:49 +0100
Message-ID<LgOtRBDYQi2PFwVE@invalid.uk.co.demon.merlyn.invalid>
In reply to#14475
In comp.lang.javascript message <ov2gt7p84ffdfdd7krf601rnt49cgemfuv@4ax.
com>, Tue, 12 Jun 2012 20:47:14, Gene Wirchenko <genew@ocis.net> posted:

>On Tue, 12 Jun 2012 20:34:56 +0100, Dr J R Stockton
><reply1224@merlyn.demon.co.uk.not.invalid> wrote:

>>It can also work unreliably.  Consider
>>
>>        D = new Date() ; D.setMonth(5)
>>
>>which has about a 1 in 52 chance of doing what probably was not
>>expected; and consider the careless implementation of Zeller's
>
>     It depends how familiar you are with JavaScript.  If you are
>expecting months from 1 to 12 as everywhere else that I have seen, the
>probability is 1 that it is not what was expected.  I still think that
>it is the stupidest language misdesign that I have run across.

Yes, but I put 1 in 52 - perhaps you have not been bitten by this one
yet.

>>Congruence which was in a (draft?) RFC or similar in the nineteen-
>>nineties; and consider the implementation of Number.toString(B) in which
>>the final character was incremented if the remainder was a half or
>>more.
>
>     What was that?  "9" becoming ":" in ASCII?

Not for B = 10.  I first noticed it when converting a fraction to binary
with   X.toString(2)   - about half the tome, the last digit of the
binary fraction was "2".  I think that error is fixed now.

But just what do you think   0.5.toString(9)   will give in Chrome?

-- 
 (c) John Stockton, nr London, UK.    ?@merlyn.demon.co.uk     Turnpike v6.05.
 Website  <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
 PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
 Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

[toc] | [prev] | [next] | [standalone]


#14505

FromGene Wirchenko <genew@ocis.net>
Date2012-06-14 20:45 -0700
Message-ID<ddblt7lefbg6p8tnbkqpdsksjtk0vtv1hu@4ax.com>
In reply to#14502
On Thu, 14 Jun 2012 18:49:12 +0100, Dr J R Stockton
<reply1224@merlyn.demon.co.uk.not.invalid> wrote:

>In comp.lang.javascript message <ov2gt7p84ffdfdd7krf601rnt49cgemfuv@4ax.
>com>, Tue, 12 Jun 2012 20:47:14, Gene Wirchenko <genew@ocis.net> posted:
>
>>On Tue, 12 Jun 2012 20:34:56 +0100, Dr J R Stockton
>><reply1224@merlyn.demon.co.uk.not.invalid> wrote:
>
>>>It can also work unreliably.  Consider
>>>
>>>        D = new Date() ; D.setMonth(5)
>>>
>>>which has about a 1 in 52 chance of doing what probably was not
>>>expected; and consider the careless implementation of Zeller's
>>
>>     It depends how familiar you are with JavaScript.  If you are
>>expecting months from 1 to 12 as everywhere else that I have seen, the
>>probability is 1 that it is not what was expected.  I still think that
>>it is the stupidest language misdesign that I have run across.
>
>Yes, but I put 1 in 52 - perhaps you have not been bitten by this one
>yet.

     I did see the possibility reading your previous post, but I did
not look to see where the 52 comes from.  There are seven months that
have 31 days.  If the current date is the 31st of one of those months,
trying to set a June 31st is going to be interesting.  I do not care
what happens, because whatever it is, it is going to be wrong.

>>>Congruence which was in a (draft?) RFC or similar in the nineteen-
>>>nineties; and consider the implementation of Number.toString(B) in which
>>>the final character was incremented if the remainder was a half or
>>>more.
>>
>>     What was that?  "9" becoming ":" in ASCII?
>
>Not for B = 10.  I first noticed it when converting a fraction to binary
>with   X.toString(2)   - about half the tome, the last digit of the
>binary fraction was "2".  I think that error is fixed now.

     Ouch!

>But just what do you think   0.5.toString(9)   will give in Chrome?

     I did not know that argument to number .toString().  In IE 9, it
gives 0.444444(lots of them).  Chrome gives something else?

Sincerely,

Gene Wirchenko

[toc] | [prev] | [next] | [standalone]


#14513

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2012-06-15 06:30 +0000
Message-ID<XnsA073569F18B3eejj99@194.109.133.133>
In reply to#14505
Gene Wirchenko wrote on 15 jun 2012 in comp.lang.javascript:

> If the current date is the 31st of one of those months,
> trying to set a June 31st is going to be interesting.  I do not care
> what happens, because whatever it is, it is going to be wrong.

Why do you not care for things you find interesting, Gene?
Is't that a clinical sign of being depressed?

You should care in JS, 
where the date is correctly recalculated:

var myYear = 2012;
var myMonth = 5; // June
var myDay = 31;
var result = new Date( myYear, myMonth, myDay, 0, 0, 0 );
alert( result ); // 1st of July 2012


-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#14520

FromGene Wirchenko <genew@ocis.net>
Date2012-06-15 09:42 -0700
Message-ID<22pmt75p3tr4pc2kdbmfp4g1va8l3blile@4ax.com>
In reply to#14513
On 15 Jun 2012 06:30:58 GMT, "Evertjan."
<exxjxw.hannivoort@inter.nl.net> wrote:

>Gene Wirchenko wrote on 15 jun 2012 in comp.lang.javascript:
>
>> If the current date is the 31st of one of those months,
>> trying to set a June 31st is going to be interesting.  I do not care
>> what happens, because whatever it is, it is going to be wrong.
>
>Why do you not care for things you find interesting, Gene?
>Is't that a clinical sign of being depressed?

     Up to your old tricks, I see.

     For all of your harping about correct English, you do not
understand nearly as much of it as you think as attested by your
posts.

     "interesting" as in that it is a problem.

     As to depression, I doubt you are qualified to make any such
assessment.

>You should care in JS, 
>where the date is correctly recalculated:
>
>var myYear = 2012;
>var myMonth = 5; // June
>var myDay = 31;
>var result = new Date( myYear, myMonth, myDay, 0, 0, 0 );
>alert( result ); // 1st of July 2012

     Go back and read what Dr. Stockton wrote.  He used .setMonth().

     It might well be that it works the same as your code, but it is
an area prone to error.  Get bit by a few of these, and many computing
professionals avoid such ambiguous areas.  We write our code so that
the situation does not come up.

     I do not care what happens in that case, because I almost
certainly am not going to do it.  If I do consider doing it, then I
will very carefully check that it is actually legal code with a
defined behaviour before I actually use it in production code.  That
is assuming that I do not just find another way that is not so likely
to cause problems.

Sincerely,

Gene Wirchenko

[toc] | [prev] | [next] | [standalone]


#14522

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2012-06-15 18:38 +0000
Message-ID<XnsA073D1F53B780eejj99@194.109.133.133>
In reply to#14520
Gene Wirchenko wrote on 15 jun 2012 in comp.lang.javascript:

> On 15 Jun 2012 06:30:58 GMT, "Evertjan."
> <exxjxw.hannivoort@inter.nl.net> wrote:
> 
>>Gene Wirchenko wrote on 15 jun 2012 in comp.lang.javascript:
>>
>>> If the current date is the 31st of one of those months,
>>> trying to set a June 31st is going to be interesting.  I do not care
>>> what happens, because whatever it is, it is going to be wrong.
>>
>>Why do you not care for things you find interesting, Gene?
>>Is't that a clinical sign of being depressed?
> 
>      Up to your old tricks, I see.
> 
>      For all of your harping about correct English, you do not
> understand nearly as much of it as you think as attested by your
> posts.

So you are the expert, Gene?
Why do you think that?

> 
>      "interesting" as in that it is a problem.
> 
>      As to depression, I doubt you are qualified to make any such
> assessment.

You might be wrong.
I did not make an assesment, I just asked.

Maybe your English is not up to understanding 
the difference between a question and a statement?
 
>>You should care in JS, 
>>where the date is correctly recalculated:
>>
>>var myYear = 2012;
>>var myMonth = 5; // June
>>var myDay = 31;
>>var result = new Date( myYear, myMonth, myDay, 0, 0, 0 );
>>alert( result ); // 1st of July 2012
> 
>      Go back and read what Dr. Stockton wrote.  He used .setMonth().

So?

>      It might well be that it works the same as your code, but it is
> an area prone to error.  

As is Javascript in general in the hands of the less competent.

I, for one, can assure you it works the same way in all methods of the 
Date object.

> Get bit by a few of these, and many computing
> professionals avoid such ambiguous areas.  We write our code so that
> the situation does not come up.

Your selfprofessed kind of computing professional perhaps.

I know many exellent computing professionals who do not care to know 
anything about computer programming. They just use the computer for their 
job.

This calculating action of the Date object transgressing the border of 
their temporal boundaries is standard in Javascript, and longtime used 
bij many a professional Javascript programmer, like John and me. 

I grant you it is a bit confusing for newcomers.

>      I do not care what happens in that case, because I almost
> certainly am not going to do it.  If I do consider doing it, then I
> will very carefully check that it is actually legal code with a
> defined behaviour before I actually use it in production code.  That
> is assuming that I do not just find another way that is not so likely
> to cause problems.

You are very eloquent about things you do not care about and still find 
interesting. 

I suggest you use these Javascript handy peculiarities to your advantage.

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#14521

FromStefan Weiss <krewecherl@gmail.com>
Date2012-06-15 19:23 +0200
Message-ID<jrfr23$k6$1@news.albasani.net>
In reply to#14505
On 2012-06-15 05:45, Gene Wirchenko wrote:
>>>On Tue, 12 Jun 2012 20:34:56 +0100, Dr J R Stockton
>>><reply1224@merlyn.demon.co.uk.not.invalid> wrote:
>>
>>>>It can also work unreliably.  Consider
>>>>
>>>>        D = new Date() ; D.setMonth(5)
>>>> 
>>>> which has about a 1 in 52 chance of doing what probably was not
>>>> expected; and consider the careless implementation of Zeller's
...
>      I did see the possibility reading your previous post, but I did
> not look to see where the 52 comes from.  There are seven months that
> have 31 days.

And that's where the 52 comes from: 365/7 ~ 52

> If the current date is the 31st of one of those months,
> trying to set a June 31st is going to be interesting.  I do not care
> what happens, because whatever it is, it is going to be wrong.

You say it's wrong, Dr Stockton says it's probably unexpected. The real
problem is giving a calculator an ambiguous task (add "a month"), and
expecting it to solve the task according to each user's expectations of
what is correct.

According to the specs, there's exactly one outcome in this situation
(month with 30 days, day-of-month set to 31). If that result is not what
the users expect, it's a user error. These cases can be trivially
anticipated and handled so that the result is more in line with what the
user needs.

There are several possible outcomes for an operation like this, e.g.
- move the additional day into the next month (like the Date object)
- limit the day-of-month to the highest valid number for that month
- cause the operation to fail

None of these will satisfy all users, and each has problems of its own.
I'm afraid this will continue to be the case until we finally move to a
decimal time system. Unfortunately, for the ideal case, that involves
slowing Earth's orbit around the sun so that it always takes exactly
1000 days. Another solution would be to increase the Earth's speed of
rotation so that the days are shorter. For best results, use a
combination of both approaches.

- stefan

[toc] | [prev] | [next] | [standalone]


#14523

FromGene Wirchenko <genew@ocis.net>
Date2012-06-15 12:54 -0700
Message-ID<7q3nt7ll12r2b55v06jv4gl89pq2l9q9m7@4ax.com>
In reply to#14521
On Fri, 15 Jun 2012 19:23:14 +0200, Stefan Weiss
<krewecherl@gmail.com> wrote:

>On 2012-06-15 05:45, Gene Wirchenko wrote:
>>>>On Tue, 12 Jun 2012 20:34:56 +0100, Dr J R Stockton
>>>><reply1224@merlyn.demon.co.uk.not.invalid> wrote:
>>>
>>>>>It can also work unreliably.  Consider
>>>>>
>>>>>        D = new Date() ; D.setMonth(5)
>>>>> 
>>>>> which has about a 1 in 52 chance of doing what probably was not
>>>>> expected; and consider the careless implementation of Zeller's
>...
>>      I did see the possibility reading your previous post, but I did
>> not look to see where the 52 comes from.  There are seven months that
>> have 31 days.
>
>And that's where the 52 comes from: 365/7 ~ 52
>
>> If the current date is the 31st of one of those months,
>> trying to set a June 31st is going to be interesting.  I do not care
>> what happens, because whatever it is, it is going to be wrong.
>
>You say it's wrong, Dr Stockton says it's probably unexpected. The real
>problem is giving a calculator an ambiguous task (add "a month"), and
>expecting it to solve the task according to each user's expectations of
>what is correct.

     If it is ambiguous, then it is wrong in my book.  I want a
expected result.  If I have to guess or if there is more than one
logical way to do it, here be dragons.

     Note that the language spec may say which way that particular
language jumps, but that is largely irrelevant to me.  It is
potentially troublesome so I will avoid it as best as I can.  After
all, I use more than one language.  Languages do not even agree on how
to do modulo arithmetic despite there being a very precise
mathematical meaning for this mathematical operation.

     "add a month" is a good example of ambiguous.  In my VFP app that
I support, I long ago wrote a version of gomonth() (which
adds/subtracts months) to return the last day of the month desired
when the input date is the last day of its month.  I need this when
computing transaction date ranges for recurring work orders.

     An even easier-to-see example of this is adding one month to a
date then adding one month to that date and comparing this with adding
two months to the starting date.  There are cases where this is not
the same.  The cases are all at/near the end of a month.

>According to the specs, there's exactly one outcome in this situation
>(month with 30 days, day-of-month set to 31). If that result is not what
>the users expect, it's a user error. These cases can be trivially
>anticipated and handled so that the result is more in line with what the
>user needs.

     No, it does not follow that it is a user error.  One could just
as easily argue that the specs are user-hostile in this regard.

>There are several possible outcomes for an operation like this, e.g.
>- move the additional day into the next month (like the Date object)
>- limit the day-of-month to the highest valid number for that month
>- cause the operation to fail
>
>None of these will satisfy all users, and each has problems of its own.

     Truly, and that is why I tend to avoid such circumstances.  One
language may jump one way and another another.

>I'm afraid this will continue to be the case until we finally move to a
>decimal time system. Unfortunately, for the ideal case, that involves
>slowing Earth's orbit around the sun so that it always takes exactly
>1000 days. Another solution would be to increase the Earth's speed of
>rotation so that the days are shorter. For best results, use a
>combination of both approaches.

     You will have to mess with the moon, too, to handle months.  Some
religions may then persist in using an already-extant calendar anyway.
Think Easter calculations.

Sincerely,

Gene Wirchenko

[toc] | [prev] | [next] | [standalone]


#14540

FromDr J R Stockton <reply1224@merlyn.demon.co.uk.not.invalid>
Date2012-06-16 21:46 +0100
Message-ID<yccj+tFVCP3PFwJv@invalid.uk.co.demon.merlyn.invalid>
In reply to#14505
In comp.lang.javascript message <ddblt7lefbg6p8tnbkqpdsksjtk0vtv1hu@4ax.
com>, Thu, 14 Jun 2012 20:45:24, Gene Wirchenko <genew@ocis.net> posted:

>
>
>>But just what do you think   0.5.toString(9)   will give in Chrome?
>
>     I did not know that argument to number .toString().  In IE 9, it
>gives 0.444444(lots of them).  Chrome gives something else?

You have nearly answered a question that I forgot to ask - by giving an
ambiguous answer.

What does   (0.5).toString(9).length     give in IE9 (and, if possible,
in IE10?) ?

The answer will go in <http://www.merlyn.demon.co.uk/js-flaws.htm#Auto>,
row I.  Can I remove the beta from the IE9 column heading?



As regards a previous matter, this is what a JavaScript date-neophyte
might well write to set April 15th of the current year (that appears to
be a sort of ritual day to your South) :

        D = new Date( ) ; D.setMonth(3) ; D.setDate(15) ; D
which sometimes gives May 15th, whereas
        D = new Date( ) ; D.setMonth(3, 15) ; D
and
        D = new Date(0) ; D.setMonth(3) ; D.setDate(15) ; D
should be reliable.



Another GW article in this thread :

>You will have to mess with the moon, too, to handle months.  Some
>religions may then persist in using an already-extant calendar anyway.
>Think Easter calculations.

When, in 1581 and 1751, a change to the secular calendar was enacted, a
revised Easter rule was also authorised.  The Orthodox, being anti-
Papist, ignored both.  If the secular calendar is again changed, there
should be no religious objection to making a corresponding change.
Moreover, consider the UK's Easter Act 1928 : the Church found a so-
called fixed Easter date acceptable.  OTOH, my advice is to have Easter
Sunday always on yyyy-W15-7, which differs only slightly from the effect
of the (unimplemented) Act.  That Act ought to have applied to, or been
copied by, the Dominions of the time.

-- 
 (c) John Stockton, nr London, UK.    ?@merlyn.demon.co.uk     Turnpike v6.05.
 Website  <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
 PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
 Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

[toc] | [prev] | [next] | [standalone]


#14545

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2012-06-17 08:03 +0000
Message-ID<XnsA075666147BFFeejj99@194.109.133.133>
In reply to#14540
Dr J R Stockton wrote on 16 jun 2012 in comp.lang.javascript:

> What does   (0.5).toString(9).length     give in IE9 (and, if possible,
> in IE10?) ?

<script type='text/javascript'>
  document.write(  (0.5).toString(9) + '<br>' );
  document.write(  (0.5).toString(9).length  + '<br>' );
</script>

IE9.0.8:
0.444444444444444444
20


IE10:
?? sorry.


FF12.0:
0.44444444444444445
19


Chrome:
0.44444444444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444444444444
444444
1101




-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#14499

FromJohn G Harris <john@nospam.demon.co.uk>
Date2012-06-14 11:08 +0100
Message-ID<m+Pm1REPgb2PFwB7@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD>
In reply to#14472
On Tue, 12 Jun 2012 at 20:34:56, in comp.lang.javascript, Dr J R
Stockton wrote:
>In comp.lang.javascript message <n1BUf3GqMg1PFwpq@J.A830F0FF37FB96852AD0
>8924D9443D28E23ED5CD>, Mon, 11 Jun 2012 15:39:38, John G Harris
><john@nospam.demon.co.uk> posted:

  <snip>
>>In colloquial English "illegal" has two meanings. One is something
>>criminal. The other, more general, is disobeying some rules.
>>
>>Example, taken from the C++ standard :
>>  "Using these specifiers with type declarations is illegal in C++."
>
>Nevertheless, one should eschew its use in public; in particular, in
>error messages that a normal respectable user might see.  Normal users
>can be consternated by an assertion that an illegal operation has been
>attempted or performed.  The English language contains many words ... (

Better still, avoid technical words.
  "This program has done something it shouldn't do"
is better than
  "Illegal operation performed".


  <snip>
>>>Source code can be syntactically *invalid* or *valid*,
>
>>>and it
>>>can *work* or *not work*.
>>  <snip>
>>
>>Too few cases : think Halting Theorem.
>
>It can also work unreliably.
  <snip>

Surely not working sometimes is just one way of not working.

The third case is "really don't know". For instance, a program that
still hasn't found the solution after several years, and no-one has been
able to prove that it definitely will eventually, nor prove that it
might not.


  John
-- 
John Harris

[toc] | [prev] | [next] | [standalone]


#14501

FromGene Wirchenko <genew@ocis.net>
Date2012-06-14 09:23 -0700
Message-ID<lt3kt79emdrl6e3fkg1fbqseg30dodca9o@4ax.com>
In reply to#14499
On Thu, 14 Jun 2012 11:08:15 +0100, John G Harris
<john@nospam.demon.co.uk> wrote:

[snip]

>Better still, avoid technical words.
>  "This program has done something it shouldn't do"
         ^^^^^^^
     "A program?  You mean like on TV?"

     "program" is a technical word, too.

>is better than
>  "Illegal operation performed".

[snip]

Sincerely,

Gene Wirchenko

[toc] | [prev] | [next] | [standalone]


#14525

FromDr J R Stockton <reply1224@merlyn.demon.co.uk.not.invalid>
Date2012-06-15 19:56 +0100
Message-ID<HaGOfnD6V42PFwX6@invalid.uk.co.demon.merlyn.invalid>
In reply to#14499
In comp.lang.javascript message <m+Pm1REPgb2PFwB7@J.A830F0FF37FB96852AD0
8924D9443D28E23ED5CD>, Thu, 14 Jun 2012 11:08:15, John G Harris
<john@nospam.demon.co.uk> posted:

>On Tue, 12 Jun 2012 at 20:34:56, in comp.lang.javascript, Dr J R
>Stockton wrote:
>>In comp.lang.javascript message <n1BUf3GqMg1PFwpq@J.A830F0FF37FB96852AD0
>>8924D9443D28E23ED5CD>, Mon, 11 Jun 2012 15:39:38, John G Harris
>><john@nospam.demon.co.uk> posted:
>
>  <snip>
>>>In colloquial English "illegal" has two meanings. One is something
>>>criminal. The other, more general, is disobeying some rules.
>>>
>>>Example, taken from the C++ standard :
>>>  "Using these specifiers with type declarations is illegal in C++."
>>
>>Nevertheless, one should eschew its use in public; in particular, in
>>error messages that a normal respectable user might see.  Normal users
>>can be consternated by an assertion that an illegal operation has been
>>attempted or performed.  The English language contains many words ... (
>
>Better still, avoid technical words.
>  "This program has done something it shouldn't do"
>is better than
>  "Illegal operation performed".

Agreed.  I have seen something very like "The impossible has happened";
and I have read of "GOTO is followed by an improper designational
expression".


>>It can also work unreliably.
>  <snip>
>
>Surely not working sometimes is just one way of not working.

Yes; but in practice the exact meaning of "sometimes" is important.

There is a difference in practice between a short run-time program that
unsuspectedly contains something equivalent to
        if (!(Math.random()*25)) Misbehave()
and the snippet which I posted before.

Both, if I have it right, fail about 2% of the time.  But one of them
can be investigated by repeated testing whenever one wants; and the
other will fail on the 31st of the month, always and only.

That is why I say that new Date() should not be used _for_ creating a
Date Object, only _for_ reading the date/time.  For the former purpose,
use new Date(0) (or other argument).

-- 
 (c) John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v6.05   MIME.
   Web  <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
 Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
 Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

[toc] | [prev] | [next] | [standalone]


#14528

FromAndrew Poulos <ap_prog@hotmail.com>
Date2012-06-16 13:26 +1000
Message-ID<3dmdnUQGvrGUYEbSnZ2dnUVZ_jydnZ2d@westnet.com.au>
In reply to#14525
On 16/06/2012 4:56 AM, Dr J R Stockton wrote:
> In comp.lang.javascript message<m+Pm1REPgb2PFwB7@J.A830F0FF37FB96852AD0
> 8924D9443D28E23ED5CD>, Thu, 14 Jun 2012 11:08:15, John G Harris
> <john@nospam.demon.co.uk>  posted:
>
>> On Tue, 12 Jun 2012 at 20:34:56, in comp.lang.javascript, Dr J R
>> Stockton wrote:
>>> In comp.lang.javascript message<n1BUf3GqMg1PFwpq@J.A830F0FF37FB96852AD0
>>> 8924D9443D28E23ED5CD>, Mon, 11 Jun 2012 15:39:38, John G Harris
>>> <john@nospam.demon.co.uk>  posted:
>>
>>   <snip>
>>>> In colloquial English "illegal" has two meanings. One is something
>>>> criminal. The other, more general, is disobeying some rules.
>>>>
>>>> Example, taken from the C++ standard :
>>>>   "Using these specifiers with type declarations is illegal in C++."
>>>
>>> Nevertheless, one should eschew its use in public; in particular, in
>>> error messages that a normal respectable user might see.  Normal users
>>> can be consternated by an assertion that an illegal operation has been
>>> attempted or performed.  The English language contains many words ... (
>>
>> Better still, avoid technical words.
>>   "This program has done something it shouldn't do"
>> is better than
>>   "Illegal operation performed".
>
> Agreed.  I have seen something very like "The impossible has happened";
> and I have read of "GOTO is followed by an improper designational
> expression".

On the other hand both messages are close to useless for the developer 
whose job it is to fix such issues. Maybe its better to have a two part 
message??? The first part tells everyone that the something 
bad/unexpected happened, and the second part, for those that care, what 
bad thing happened.

[Something bad happened]
"Oops"
[What exactly happened]
At line 3247 of ... and illegal operation threw the error ERR11487525 
occurred which caused ...

Andrew Poulos

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.javascript


csiph-web