logo To Foot
© J R Stockton, ≥ 2008-04-17

JavaScript Miscellany 1.

No-Frame * Framed Index * Frame This
Links within this site :-

See "About This JavaScript Site" in JavaScript Index and Introduction.

Bar Chart

The result should somewhat resemble that in GIF.

Enter a few numbers :-


  Mean:   Figures :   Base 0 :

The principle is to write coloured divs of calculated size and position them absolutely to build the chart. The Mean (red) and Zero (blue) lines are div-border-bottoms.

Alternatively, images of colour could be positioned.

Object Properties

This should show the enumerable properties of an Object and their present values.

 window
 document
 document.body
 document.body.style
 this
 this.form
 the textarea
 


Various Maths Tests

Padding

Unary + - Test

Suffixes

For dates :-

Possibly the best function so far; for all numbers ≥ 0 :-

Pi

John Wallis :-

ii

See Wikipedia and discussion.

The Sieve of Eratosthenes

The Sieve is a classical, intrinsically fast method for listing prime numbers.

 

For the Estimate, look up Prime Number Theorem.

Cacheing

Work should be repeated as little as possible. Intermediate results required repeatedly should be determined once and referenced as required.

Poor : 	document.getElementById("Fred").rows = 3
 	document.getElementById("Fred").cols = 66

Good :	f = document.getElementById("Fred")
	f.rows = 3 ; f.cols = 66

Good :	with (document.getElementById("Fred")) { rows = 3 ; cols = 66 }

Also, code should be repeated as little as possible; so use functions for such code, if as above will not suffice.

Factorial Recursion

     

I've not speed-tested; but, except for light use, cached should beat iterative and recursive. It's substantially equivalent to using a lookup table, except that the table it generates is just as long as is needed, and no more.

A better example of cacheing follows.

Ways of Giving Change

JAD's Quiz 1995, question #14 - to determine the number of ways (49) to give change, in stated coin of the realm (50, 20, 10, 5 pence), for a given "Total" amount (£1.00).

Translated to JavaScript from Pascal program jad_9514.pas :-

The elements of "Coins" ought to be integers in strict descending order. That order was intended at design time; but now it seems to affect mainly speed when cache is used.


         

For £1.00 = 100p, cacheing intermediate results changed the time taken from intolerable to imperceptible in Borland Pascal on a 486dx/33.

Note the performance gain here with Cache set; allow for the resolution of timing.

Day-of-Week Checking

This tests apparent instances of YYYY-MM-DD DoW in texts. It checks that Y-M-D is valid AD Gregorian and that DoW agrees. The year may be any number of digits, but if past about 275000 the date and DoW will not be verified. Paste the input into the textarea. See the code for details. Please do not use this page repeatedly; make a local copy.

Buttons 1 - Site check : pre-process at DOS prompt with (mtr is MiniTrue-32)
  mtr -x+ -o *.htm - "<li><tt>(\d.*)</tt>.*" = \1\r > $X
or
  mtr -x+ -o -c- *.htm - \d\d\d\d-\d\d-\d\d\s*[A-Z][a-z]{2}\W > $X
or
  findstr /r [0-9]-[0-9][0-9]-[0-9][0-9] *.htm > $X
or
  findstr /rn /c:"[0-9]-[0-9][0-9]-[0-9][0-9] [a-z][a-z][a-z]\>" *.htm > $X
or otherwise (maybe SED?). Checks the first date on each line (lines without dates are removed), and shows errors at end of line. Button 1a shows all lines, 1b omits good ones.

Button 2 - Checks an HTML page, or anything else. Each error is reported in a confirm box, with some leading context.


Text Substitution

In the command box, the field separator is TAB; if it is not possible to type it in, then paste or drag it.


 

Note that RegExps are used, and compose the commands accordingly.

XMLHTTP

Not yet working correctly.


Home Page
Mail: no HTML
© Dr J R Stockton, near London, UK.
All Rights Reserved.
These pages are tested mainly with MS IE 6 and W3's Tidy.
This site, http://www.merlyn.demon.co.uk/, is maintained by me.
Head.