Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How do you stop a javascript program?

Reply
Thread Tools

How do you stop a javascript program?

 
 
electrician@electrician.com
Guest
Posts: n/a
 
      03-04-2006
While running a program that exceeds the array limits it issues an
alert. I want to then stop the program after filling in the output
boxes with blanks. How do you stop the program?
I have worked on this for days and tried searching the net, but have
found nothing.

 
Reply With Quote
 
 
 
 
electrician@electrician.com
Guest
Posts: n/a
 
      03-04-2006
I jsut searched groups for stop javascript and found the answer. The
answer is to label loops and then use a break loop statement.

 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      03-04-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

[Added attribution and quotation]
> (E-Mail Removed) wrote:
>> While running a program that exceeds the array limits it issues an
>> alert. I want to then stop the program after filling in the output
>> boxes with blanks. How do you stop the program?


You don't.

>> I have worked on this for days and tried searching the net, but have
>> found nothing.

>
> I jsut searched groups for stop javascript and found the answer.
> The answer is to label loops and then use a break loop statement.

^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
No, this reads like utter nonsense. If I understood your OP correctly,
which you did not quote BTW[1], you should use something like

var es = f.elements;
for (var i = 0, len = es.length; i < len; i++)
{
// ... es[i] ...
}

Provided that `f' refers to a HTMLFormElement representing a `form' element
your "output boxes" are controls of. Where you should know that you are
not dealing with an Array object, but with an HTMLCollection object.


PointedEars
_________
[1] <URL:http://safalra.com/special/googlegroupsreply/>
 
Reply With Quote
 
electrician@electrician.com
Guest
Posts: n/a
 
      03-05-2006
The problem was that I searched an array of conductor ampacities, but
when the end of the array was reached and the right size conductor
could not be found because the derating was too high I needed to stop
the program and alert the user to enter smaller values and fill all the
output form values with blanks and terminate the program. I
accomplished this by setting a flag to True and broke out of the outer
loop with break loop then filled in the blanks by running the
endprogram when the flag is true. It works just fine, now. This is a
rather long program and I have obfuscated it, but if you want to see it
work it is at,
http://electrician.com/calculators/wireocpd_ver_1.html

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      03-05-2006
(E-Mail Removed) wrote:

> The problem was that I searched an array of conductor ampacities, but
> when the end of the array was reached and the right size conductor
> could not be found because the derating was too high I needed to stop
> the program and alert the user to enter smaller values and fill all the
> output form values with blanks and terminate the program. I
> accomplished this by setting a flag to True and broke out of the outer
> loop with break loop then filled in the blanks by running the
> endprogram when the flag is true. It works just fine, now. This is a
> rather long program and I have obfuscated it, but if you want to see it
> work it is at,
> http://electrician.com/calculators/wireocpd_ver_1.html


I will not pretend that I fully understand what you are talking about, since
I am not an electrician, and your explanation lacks specific reference to
the code you are using, and again, to what you are referring to (will you
learn to quote?). However, reviewing your code, it is obvious to me that
it (still) has a number of flaws.

- The underlying HTML code is not Valid. You cannot expect client-side
script code to work on not Valid markup, as you cannot expect a house
to stand on quicksand.

<URL:http://validator.w3.org/check?uri=http://electrician.com/calculators/wireocpd_ver_1.html>

- The script is quite large. For HTTP request efficiency, large resources
should be splitted into smaller chunks. This means the script it should
be moved to an external script resource, e.g. program.js, which should be
included in the HTML document with

<script type="text/javascript" src="program.js"></script>

See also:
<URL:http://www.websiteoptimization.com/services/analyze/wso.php?url=http://electrician.com/calculators/wireocpd_ver_1.html>

- ECMAScript implementations provide a boolean type, with predefined
values of `true' and `false'. It is quite inefficient and error-prone
(unrecognized typos!) to use the string values "true"/"True" and
"false"/"False" instead.

- for (var i = 0; i <= form.aN.length; i++)
{
if (form.aN[i].selected)
{
ay =(form.aN[i].value);
break;
}
}

is syntactically correct, but semantically wrong, as it loops also for
form.aN[form.aN.length], which does not exist because the collection's
index is zero-based. Using

for (var i = 0; i < form.aN.length; i++)

instead, solves this problem. However, it would be still inefficient
(because of repeated deep lookup) and error-prone (because of proprietary
referencing). Therefore, it should be

for (var i = 0, len = form.elements['aN'].options.length; i < len; i++)

Because the reference `form.elements' is used more than one time, it
should be assigned to a local variable once and that variable should
be used instead:

var es = form.elements, o = es['aN'].options;

for (var i = 0, len = o.length; i < len; i++)

But "aN" refers to one single select element that does not have the
"multiple" attribute. Therefore, it should be simply

ay = o[o.selectedIndex].value;

_without_ any unnecessary loop. You will also observe here that the
parentheses in

ay =(form.aN[i].value);

were redundant. Since you have been using obfuscation (which I recommend
against) to make the code as short as possible, you should endeavour not
to include any unnecessary code.

- au = eval(form.S[i].value);

is nonsense. The global eval() method merely evaluates its argument.
Unless its argument is a _string_ value that contains an arithmetic
expression, eval() is entirely redundant, inefficient, and error-prone.

While "S" refers to a `select' element in your HTML code and the `value'
property of HTMLSelectElement objects should not be relied upon (use
selectRef.options[selectRef.selectedIndex] instead), the value of
that property in your case could be either (e.g. for the first item)
"0" or "21-25 (70-77F)".

Assuming it would be "0", being the value of the `value' property of the
respective HTMLOptionElement object as specified in W3C DOM Level 2 HTML,

+es['S'][i]

or even

parseInt(es['S'][i], 10)

would be more efficient.

<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-59351919>

Assuming that it would be instead "21-25 (70-77F)" as IIRC known from
some DOMs, eval(form.S[i].value) or its standards-compliant reference
equivalent would result in a syntax error.

Therefore the loop should be

for (var i = 0, o = es['S'].options, len = o.length; i < len; i++)

and it should be either

// allows for interpretation of 0x... as hexadecimal value
au = +o[i].value;

or

// parses 0x... as 0
au = parseInt(o[i].value, 10);

However, no loop is necessary at all here, too:

au = parseInt(o[o.selectedIndex].value, 10);

- for (var n = 0; n <= form.aY.length; n++)
{
if (form.aY[n].checked)
{
aB =(form.aY[n].value);
break;
}
}

is different from the above insofar it refers to a group of radiobuttons
with the same name, and not a `select' element. Therefore, the loop is
necessary here, indeed. However,

var aRadio = es['aY'];
for (var n = 0, len = aRadio.length; n < len; n++)
{
if (aRadio[n].checked)
{
aB = aRadio[n].value;
break;
}
}

is still better. (You will observe that the iterator variable and the
`len' variable can be reused. To avoid repeated declaration, you should
declare them locally before the `for' statements only once, and only
[re-]initialize them in each `for' statement. Certainly you do not need
two iterator variables `i' and `n' if the corresponding loops are
independent.)

You should consider using a method (function) that returns the value of
the selected radiobutton in a group. IIRC such a method can be found in
the newsgroup's FAQ, for example.

- As you will probably have guessed after having read the above,

bx =(eval((1.25 *(form.ab.value)))) +(eval(form.aS.value));

is utter nonsense, really.

- If you use boolean values as suggested,

if (ax == "True")

could be simplified to

if (ax)

and

if ((as == "False") &&(G == "False"))

could be simplified to

if (!as && !G)

and so on. (Whereas I recommend to avoid using identifiers starting with
a capital letter if they does not refer to a constructor or a factory;
just to avoid confusion, and to make source code easier readable.)

- ISTM other flaws/errors in your code include merely repetitions of the
flaws/errors that are described above.


HTH

PointedEars
 
Reply With Quote
 
electrician@electrician.com
Guest
Posts: n/a
 
      03-05-2006
I don't really try to meet the W3.org standards. 98 per cent of the
visitors to my site use IE5.0+. I stopped trying to be compatible with
Netscape, Opera, and Firefox long ago. I used to spend about 30 per
cent of my time on compatibility issues, but now have the attitude that
if the other browsers can't meet MS standards, then too bad. MS is
more of a standard setter than W3.org. Also, most users of my site
have high speed connections so I don't worry about large file sizes. I
have one popular calculator that is 85k right in the web page and I
have not had one single complaint in six years. Besides, js files are
too hard to edit! I suppose I am not in line with academia, but then
I taught myself what I know, and speak from experience. I use
JavaScript because of the ease of use, but more than that, for the
portability. Write it, upload it, and it is available. And as far as
compact code like you suggest, I don't worry about that either. Let
the machines hum and do all the work, they are made for it.
So I am a sloppy programmer, but my site makes money. I would get an F
in your class, but I am not worried about grades any more either. Next
month I start drawing social security.

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      03-05-2006
(E-Mail Removed) wrote:

> [no quote, lame excuses about error-prone inefficient code]


It is clear to me now that I have wasted my time on you. FOAD.


PointedEars
 
Reply With Quote
 
electrician@electrician.com
Guest
Posts: n/a
 
      03-05-2006
Perhaps you have learned something from my experience. Let's hope so.
Too many nit pickers are not familiar with real world experiences. I
am a hard knots laborer, electrician, with a degree in Mathematics that
applies production lessons from real work in the electrical trade to
programming. Something that I think is unique and beneficial to
academia types.

 
Reply With Quote
 
electrician@electrician.com
Guest
Posts: n/a
 
      03-05-2006
> While running a program that exceeds the array limits it issues an
> alert. I want to then stop the program after filling in the output
> boxes with blanks. How do you stop the program?
> I have worked on this for days and tried searching the net, but have
> found nothing.


<<I put the whole thing in a function and do a return to exit.>>


Yes. that works, but when the program is very complicated and long it
makes it kind of messy with those brackets about 2000 lines apart. The
program that I have written has taken 10 weeks over the last 10 years
and is very complicated. It applies complex linear programming models
with many exceptions.

 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      03-05-2006
wrote on 05 mrt 2006 in comp.lang.javascript:

> Perhaps you have learned something from my experience. Let's hope so.
> Too many nit pickers are not familiar with real world experiences. I
> am a hard knots laborer, electrician, with a degree in Mathematics that
> applies production lessons from real work in the electrical trade to
> programming. Something that I think is unique and beneficial to
> academia types.


The academia types should be grateful.

But, who are you talking to?

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at the
top of the article, then click on the "Reply" at the bottom of the article
headers. <http://www.safalra.com/special/googlegroupsreply/>

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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
RE;Kontki if you delete kontiki any program you loaded with it in it 'will not work I have tried it with three programs and none work anymore (if you se it just stop download) 1-Twitch Computer Support 5 04-23-2009 02:45 PM
How to stop java service with command line parameter '-stop'? Will Java 1 11-02-2004 03:32 PM
How to stop a thread without using stop() Son KwonNam Java 11 04-09-2004 08:01 PM
Stop Debugging doesn't stop in ASP.NET Matt Theule ASP .Net 7 07-24-2003 07:38 PM



Advertisments