A More Useful Program

Suppose we want to calculate the resonant frequency of an L-C circuit using the equations:
&omega L = 1 / &omega C
Where:
&omega = 2 π f

(If the Greek and maths symbols are not displaying correctly in your browser you may wish to take this opportunity to install current Mozilla which will do so.)

These can be rearranged to:

&omega = √(1/LC)
f = &omega / 2 π
The last two equations are now suitable formulas for this calculation.

The `business' bit of the JavaScript program is thus:

```L = ...
C = ...
omega = 1/Math.sqrt (L*C)
f = omega / (2*Math.PI)
```

Notes

• L, C, omega are variables in the program. In JavaScript you should always specify what variables you are going to use before you use them. There is a special instruction for this. Here we would say:
```var L, C, omega
```
We can choose essentially any names we like so long as they' begin with a letter.
• In Javascript the names of mathematical functions must be preceded by:
`Math.`
Note the dot and the capital M! Upper and lower case letters are not the same, so if I have used L I cannot subsequently get lazy and type l.
• L and C
• I must be careful with the units in my equations. The best way to avoid making mistakes is always to use the fundamental units in equations, unless you are copying a formula which has been specifically modified to use something else. Here this means Henries, Farads and Hertz - not the most convienient for use, but not to worry.

A Bit More

Let's add unit conversion to the program and build in a couple of values to test.

We want to use inductance in microhenries which means divide by one million to get Henries, and capacitance in picoFarads, which means dividing by a million times a million to ger Farads. Then I'd like my answer in MegaHertz, which means dividing Hertz by a million.

```var omega, f, L, C
var million = 1000000
L = 12      // microH
C = 10      // pF
L = L / million                 // Henries
C = C / million / million        // Farads
// Calculate...
omega = 1 / Math.sqrt(L*C)      // rads/s
f = omega / (2*Math.PI)         // Hz
f = f / million                 // MHz
```

Notes

• Everything on a line following // is ignored. Use this facility liberally to make your programs easier to follow.
• Since I am using the value 1,000,000 several times I have defined it as a variable. this minimises the risk of typing the wrong number of zeros. Notice that I can combine the the var definition of a variable with setting its value.
• By performing a bit of algebra I might have saved the computer the trouble of one or two of those divisions by a million. Don't even think about it. The computer's time is less valuable than yours. You'd probably get it wrong anyway.

The Final Program

Do view - Source to look at it.

The only things that have been added are output instructions:

• to provide a title for the calculation,
• to repeat the data so that the output is self contained, and
• (obviously) to show the result of the calculation.
When you display anything that has units, always also say what these are!

Note:

1. The instruction writeln which is like write except that it puts a new line after whatever has been displayed.
2. Both output instructions can have multiple items, separated by commas, in the brackets after the keyword. If these are text in quotes this is displayed exactly as specified. If they are names of variables their vaules are displayed.
3. The text<pre> at the beginning of the first output instruction. Don't worry about why it's there meantime, just always put it in. You might like to try removing it to see what happens..