Re: Proper coding? (JS newbie)



In microsoft.public.scripting.jscript message <46e1eb9c$0$24578$ecde5a14
@news.coretel.net>, Fri, 7 Sep 2007 20:21:08, Paul E. Schoen
<pstech@xxxxxxxxx> posted:

"Dr J R Stockton" <jrs@xxxxxxxxxxxxxxxxxx> wrote in message
news:5zDMQ7QAkC4GFwG5@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
In microsoft.public.scripting.jscript message <46dfaa68$0$24580$ecde5a14
@news.coretel.net>, Thu, 6 Sep 2007 03:20:30, Paul E. Schoen
<pstech@xxxxxxxxx> posted:

My new version of this page will be posted at:

www.smart.net/~pstech/DogLicenseJS.htm

I'll be updating it as I learn more.

You should put the date visibly on the page, in standard form (i.e.
YYYY-MM-DD) so that anyone can see immediately how new it is.

OK. That is certainly a good idea. Done!

Also, I don't know how the page will ultimately be distributed, but it
might be useful to state clearly which area it is for. The spelling
implies that it's not British.

I'd not use a drop-down for dog sex/type; radio-buttons seem better.
For the fee, I'd put the $ outside the box. Those are mere preferences.

Logically, there ought to be a date field or a continuation. If you're
promised a dog for Christmas, you won't want its license to start too
soon, etc.

W3's free "HTML Tidy for Windows released on 19 April 2007" said :
,,,
I'll get a copy of HTML Tidy and try it.

I should have said that it is intended mainly as a neatener, but I
prefer to use it only as a checker, using in essence
TIDY -f $$$$$$$$.CHK < %1 > nul
embedded in a batch file.

Yes, that is more concise. I found I can use:
AdjustArray = [[1.0, 1.1, 1.2, 1.5, 1.0], [1.0, 1.1, 1.2, 1.5, 1.0], [1.0,
0.9, 0.8, 0.7, 1.0]];
But it might be better to assign the values separately to the three named
arrays.

Personally, I'd use that code, but with each sub-array on its own line.

parseInt(Form1.Weight.value) can probably be +Form1.Weight.value

Yes. That fixes the problem where 090 is not the same as 90. I found this
also in the FAQ.

IIRC, I caused it to be put there.

I have fixed this using TotalFee.toFixed([2]).

Probably you cannot reach the values where toFixed is (in IE) wrong,
except for dogs of uncertain habits and negative weight,

I also now use "adjusted
by", so that 110% is an increase and 80% is a decrease.

I'd call that "adjusted by +10%" . "... -20%".

A 99-lb dog and an 099-lb dog cost differently.

This was caused by the parseInt function. It reads 099 as a zero, yet it
reads 010 as an 8. Apparently a leading zero indicates Octal. It's in the
FAQ.

IIRC, the standard deprecates that behaviour, but it seems general and
traditional. Don't use parseInt when not necessary.


Your new Array( 3, 5 ) is not a 2-dimensional array; Javascript does not
have them even though A[4,8] is meaningful.

It seems like the array can be accessed as a 3x5 array, as I have done with
AdjustArray[HumanBite][iter]. Is this the same as
AdjustArray[HumanBite,iter]?

Yes. The standards (ECMA 262, ISO/IEC 16262, free PDFs, first more
popular, second more recent and a bit better but meaning the same)
presumably refer to [x,y] near where they refer to [x], but I can't at
present locate either.

If A is an array then A[x] is an element of it, so if A[x] is an array
then A[x][y] is an element of it, so /ad infinitum aut nauseam/. And
somewhere comma must be defined as equivalent, in that context, to ][ .

I find the alerts tiresome; the text could easily be written to a
"latest change" readonly input control.

They were originally a debugging tool. I like to see what is being done for
each of those selections. Is there a way to have a message box with a
timeout so it just pops up for a second, and then closes?

Probably easier to have another input box in which the latest news is
put, or a textarea to which it is added.

Your hbh dbh obh are used as Booleans but given integer values;
javascript has Booleans.

I got confused when I used "True" rather than "true". Uppercase seems to be
inconsistent, because I can't use new array(1.0, 1.1, 1.2, 1.5, 1.0); it
must be capitalized.

Case is always important, except where it is turned off in a RegExp.

AdjustArray is indexed only by literal numeric constants; names could be
used (*some* resemblance to an enumerated type).

I defined variable names to simulate enumeration. I could not find a way to
assign constants.

Since there are no constants, you cannot do that. But you can use
variables (cf. Pascal/Delphi typed constants)

A = [1,2,3,4,5] ; C = "cat" ; A[C] = "Manx"
A[3] = true
X = [A, " and ", A['cat']].toString()
gives "1,2,3,true,5, and ,Manx" <g>.

Sometimes I got lots of warnings in FireBug console that said:

Element referenced by ID/NAME in the global scope. Use W3C standard
document.getElementById() instead.

This was where I had used something like Form1.DogType.value. I could not
get the recommended function to work. Is this really a problem? I found a
reference to the standards at:

http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html

I consider getElementById to be rather a crude method, ignoring
structure. But give each form an ID and when you start using one get
such a reference to it and use it for sub-referencing.

Standards, PDF, free, linked via sig line 3.

+ + +

Change // DogLicenseJS.htm - 09/04/07 - Paul E. Schoen
to ItIs="2007-09-08" // DogLicenseJS.htm - Paul E. Schoen
and <P>Date: 2007-09-07</P>
to <div ID=Now></div>
and do document.getElementById("Now").innerHTML = ItIs
and you'll only have one date to update.


For else if (hbh == false) {
I'd use else if (!hbh) {
as ! is NOT.


You have var Weight = Form1.Weight.value;
which is a String.
Use var Weight = + Form1.Weight.value;
which is a Number. Should not make any difference, but nicer.
The next statement could be
if(isNaN(Weight)) { ... }


If a neutered dog is re-enabled, the fee is without warning restored.


The form cannot handle a dog which has committed a single offensive bite
and has also defended its owner.

IMHO, a dog can attack a person justifiably in defence of itself (ours
once tried that when mother inadvertently stood on his ear, though
thought better before applying jaw pressure); and a dog can justifiably
attack a dog in defence of property.


function SubmitComplete()
could be shortened by an auxiliary
function Check(Field, valu, words) { var OK
if (OK = Field.value =- valu)) {
alert("Please enter " + words)
Field.focus() ; Field.select() }
return OK }
used repeatedly, except that the function and structure will need a
little adjustment to fit each other.

Then the parameters could be held in an Object, and you could iterate
through an array of objects to be checked ... Did you see
<URL:http://www.merlyn.demon.co.uk/js-valid.htm#VFF> yet?

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
.