First JavaScript Program

Home Math Object First Script Demo 
This free JavaScript tutorial teaches scripting, electronics, and numeric-analysis simultaneously. It teaches pure JavaScript for scientific and technical calculations.  You should be able to program calculations using  only Notepad and Internet Explorer in one or two hours.

 

Math Object
First Script Demo

 

 

 

Writing and Running Our First Program 

Below, we see a very simple script that only displays a message box with message " script has started ".  The  HTML tag tells your browser to start interpreting text as HTML code (begins WEB page).  The head tag starts header portion of program. Title "Simple Script 1" will appear in header (upper blue bar) Title.  In future lessons, we will also define functions in the header. The /head tag ends header, and the Body tag begins the body of the Web page.  The SCRIPT LANGUAGE = JavaScript  tag tells the browser to interpret everything between it and the /Script tag as JavaScript.  

 

<HTML>

<head>
<TITLE>Simple Script 1 Phase 1</TITLE>
</head>
<Body>
<SCRIPT LANGUAGE = JavaScript>

    alert("FIRST PROGRAM  has started")

</SCRIPT>
</Body>
</HTML>

Note the above script can be used as a starting point for any script program.  Start with this program and then add the mathematics calculations. The alert method generates a dialog box that displays text that is in the parenthesis.  The quotation marks indicate that letters contained within should be displayed a text.  A letter or word not in quotation marks would interpreted as a variable and its value would be displayed as a integer, floating point variable, and so forth. 

Saving First Program (Phase 2)

Step 1 -  Open text file blank page in your Notepad (plan text editor).  Type program including the top and bottom " HTML" tags and save as a text file. Name file " FirstProgram " Note where or what folder file is being saved in, so that you can find it later.  This is something you should always do when you save a file.

Step 2 - Save file again as a text file.  Name file " FirstProgram.htm " .  The file extension ".htm" tells browser that this file is a Web Page. 

Step 3 - Close Notepad (text editor).

Step 4 - Open file First Program.htm with Internet Explorer.  Message Box with words "script has started" should appear. Warning: Windows Security may block running of program.  You will have to unblock program as directed by Windows.

Step 5 - Click OK button.

To run this program again click the reload button on browser upper Menu bar.

This program does not do anything but tell you that the program has started to run.  I will use this script as the framework or template for future program development in this tutorial.

 

Writing Our Second Program

Open file text file   

 Add the following lines to script.

    n=3
    x=2
    y1 = n +x
    alert("y1 = " + y1)
    y2 = n * x
    y3 = n/x
    alert("y2 = " + y2 + " and y3 = " + y3) 

Also change title to:  Simple Script 1 Phase 2

Step 7 - Then click File followed by Save.  SecondProgram.htm is now modified. Source code should appear as follows:

<HTML>
<head>
<TITLE>Simple Script 1 Phase 2</TITLE>
</head>
<Body>
<SCRIPT LANGUAGE = JavaScript>
alert("SECOND PROGRAM has started")
n=3
x=2
y1 = n +x
alert("y1 = " + y1)
y2 = n * x
y3 = n/x
alert("y2 = " + y2 + " and y3 = " + y3)
</SCRIPT>
</Body>
</HTML>


Step 8 - Script program will run every time you load page.  You can reload (Refresh) program by clicking the Reload button.  On my version of Internet Explorer (IE 5.0) Refresh appears in View Menu.

Student Exercise: In above script, I used the multiply, divide, and add operators. Modify code by adding a formula that uses subtraction operator.

Future programs will have practical Application: 

More Complex operators such as logs and exponents require the MATH object.  Will include Math Object next.  The object of this page was to give you a platform on which you can program.  Netscape also provides a great platform for writing JAVA.  I believe most of my users have Windows and Internet Explorer on their computers, and that is why I use it to teach on the Internet.

Continue on to Math Object and Inputting Data:

 

 

        
Questions or problems regarding this web site should be directed to BilPat4342@AOL.com

Copyright ©2003 Science Ebooks All rights reserved.
Last modified: Wednesday December 06, 2006.