JavaScript

JavaScript is a programming language whose programs are stored in web pages and which run in a web browser.  Since you already have a web browser, there is nothing you need to download in order to start writing JavaScript programs.  I recommend that you download one of the JavaScript solutions, save it on your computer, and open it with a very simple text editor like Notepad.  Do not use Word!  Do not use a web page editor either.  When you open sum_to_300.html in Notepad, it will look like this:

<html>
<title>Javascript program</title>
<body>

<font color=red>
<h1>Sum of numbers from 1 to 300</h1>
<font color = black>

<script type="text/javascript">

document.write("This program calculates the sum 1+2+3+ ... + 300");
document.write("<br />");

var i=1;
var s=0;
while (i<=300)
{
s = s + i;
i = i + 1;
}

document.write("The number of numbers from 1 to " + (i-1) + " is " + s);
document.write("<br />");

</script>

<p>
The code that generates these numbers is not shown on the web page.
Save the page and edit it with a simple text editor to see the code.
Or, select View, Source.
<br>
<a href="http://www.w3schools.com/js/default.asp">JavaScript tutorial at w3schools.com</a>
</p>
</body>
</html>
 

Don't be alarmed!  Much of this simply makes a web page and makes things look nice.  We'll go through it bit by bit.  These lines:

<html>
<title>Javascript program</title>
<body>

<font color=red>
<h1>Sum of numbers from 1 to 300</h1>
<font color = black>
 

tell the web browser that this is a web page (an html file), make the title be "Javascript program," start the body of the web page, change the text color to red, make some large text appear on the screen, then change the text color back to black.  The next lines are a JavaScript program:

<script type="text/javascript">

document.write("This program calculates the sum 1+2+3+ ... + 300");
document.write("<br />");

var i=1;
var s=0;
for(i=1; i<=300; i++)
  s = s + i;

document.write("The number of numbers from 1 to " + (i-1) + " is " + s);
document.write("<br />");

</script>
 

The beginning and ending tags <script> and </script> tell where the program starts and ends.  The document.write lines put some text on the web page.  This leaves the program itself, just a few lines.  They introduce variables i and s and give them initial values.  The "for loop" makes the value of i start at 1 and changes the value of s to be the current value of s (which is 0) plus the current value of i (which is 1), making the value of s equal to 1.  Then the for loop increases the value of i by 1 (this is what i++ means), and changes the value of s again (after which s will be 3).  This happens over and over again as long as i <= 300.  As soon as i equals 301, the for loop stops and the next commands are executed, which tell the value of the sum.

You can edit the program to calculate the sum of numbers to 500, save the file, and refresh the page in your web browser.  Every time you do, the program will run again.

The last lines of the html file,

<p>
The code that generates these numbers is not shown on the web page.
Save the page and edit it with a simple text editor to see the code.
Or, select View, Source.
<br>
<a href="http://www.w3schools.com/js/default.asp">JavaScript tutorial at w3schools.com</a>
</p>
</body>
</html>
 

put some useful text on the screen, put a hyperlink to a tutorial page, and end the body and the html page.  You can just leave these lines here.  You could also type your own text to explain what the program does or how it works.

Later examples show how to use html forms to get information from the user.  See the source of 3x_plus_1.html for an example.