reptile7's JavaScript blog is Andrew Peak's personal technical writing project: it focuses on JavaScript and the analysis of JavaScript scripts, although HTML, CSS, and anything else related to coding for the Web are also fair game.

The checka( ) function gets the qform.a.value, which has a string data type, and assigns it to an a variable. The avalue is fed to the top-level isNaN( ) function to see if it is NaN (Not-A-Number): for our example, the "2" string is converted to a numeric 2, the isNaN(a) operation returns false, and the function exits.

But suppose we enter hi into the a field. In this case, the isNaN(a) operation returns true and the browser pops up a confirm( ) box bearing a "You have inputted an invalid number for the variable a. Begin again?" message.

Clicking the box's button takes us to the index.html page of the current directory. Clicking the box's button calls the script17.js over( ) function.

The over( ) function sets the values of the a, b, c, and x fields of the qform form to empty strings, reloads the page, and returns focus to the a field.

Noteworthily, the checka( ) function does not stop us from
(a) setting the avalue to 0 or
(b) leaving the a field blank: the empty string converts to 0 for an isNaN( ) operation.
We'll address these possibilities in a little bit.

and hit the tab key to move to the corresponding c (Variable c) field. Changing the bvalue calls the script17.js checkb( ) function, which does for the qform.b.value what the checka( ) function does for the qform.a.value. We enter -4 into the c field

and hit the tab key to move to the x (Resultant value of x) field. Changing the cvalue calls two script17.js functions:
(1) checkc( ), which does for the qform.c.value what checka( ) does for the qform.a.value and checkb( ) does for the qform.b.value, and
(2) process( ), which solves the 2x2 - 5x - 4 = 0 equation and displays its roots in the x field.

The process( ) function initially looks at the a/b/c fields to see if any of them have been left blank: iftrue, then it either calls the over( ) function to clear the a/b/c/x fields or sends us to the index.html page à la the checka( )/checkb( )/checkc( ) functions.

The plus and minus numerators are divided by the den denominator. The plus quotient is isNaN(plus)ed so as to check if the b2 - 4ac difference is a negative number; iftrue, then a "the roots aren't real" confirm( ) box pops up (that's not quite what it says, but that's the underlying idea).

As before, clicking the box's button clears the a/b/c/x fields via the over( ) function and clicking its button takes us to the index.html page.

The plus quotient is also NaN if a is 0, so we choke that situation off as well.

Our denum solution is:3.1374586088176875 or -0.6374586088176875
This value is not truncated by the maxlength="30" setting although we don't see the whole thing due to the input size="30".

Subsequently, the 2 in the a field is selected and focus is returned to the a field, focus in this case meaning a focus rectangle and not a blinking insertion point. (FWIW: The browsers on my computer do not cleanly separate the select and focus events. If the focus( ) command is commented out and the a field's 2 is select( )ed, then typing x still inputs x into the a field.)

Button functions

At any time during the process, we can clear the a/b/c/x fields via the over( ) function by clicking the button