Introduction to JavaScript (warm-up)
Program #1 Addition
<html>
<head>
<title>An Addition Program</title>
<script language= "javascript">
var firstNumber,
secondNumber,
number1,
number2,
sum;
firstNumber =
window.prompt( "Enter first integer", "0" );
secondNumber =
window.prompt( "Enter second integer", "0" );
number1 = parseInt( firstNumber );
number2 = parseInt( secondNumber );
sum = number1 + number2;
document.writeln( "<h1>The sum is " + sum + "</h1>" );
</script>
</head>
<body>
<p>Click Refresh (or Reload) to run the script again</p>
</body>
</html>
Program #2
<html>
<head>
<title>Performing Comparisons</title>
<script type = "text/javascript">
var first,
second,
number1,
number2;
first = window.prompt( "Enter first integer:", "0" );
second = window.prompt( "Enter second integer:", "0" );
number1 = parseInt( first );
number2 = parseInt( second );
document.writeln( "<h1<center>Comparison results</center</h1>" );
document.writeln("<table align=center border=5 width=100>" );
if ( number1 == number2 )
document.writeln( "<tr<td>" + number1 + " == " +
number2 + "</td</tr>" );
if ( number1 != number2 )
document.writeln( "<tr<td>" + number1 + " != " +
number2 + "</td</TR>" );
if ( number1 < number2 )
document.writeln( "<tr<td>" + number1 + " < " +
number2 + "</td</tr>" );
if ( number1 > number2 )
document.writeln( "<tr<td>" + number1 + " > " +
number2 + "</td</tr>" );
if ( number1 <= number2 )
document.writeln( "<tr<td>" + number1 + " <= " +
number2 + "</td</tr>" );
if ( number1 >= number2 )
document.writeln( "<tr<td>" + number1 + " >= " +
number2 + "</td</tr>" );
document.writeln( "</table>" );
</script>
</head>
<body>
<p align=center>Click Refresh (or Reload) to run the script again</p>
</body>
</html>
Program #3
<html>
<head>
<title>comparisons</title>
<script type = "text/javascript">
var number1, number2, number3,
firstNumber, secondNumber, thirdNumber,
largest, smallest;
number1 = window.prompt(
"Enter the first number", "0" );
number2 = window.prompt(
"Enter the second number", "0" );
number3 = window.prompt(
"Enter the third number", "0" );
firstNumber = parseInt ( number1 );
secondNumber = parseInt ( number2 );
thirdNumber = parseInt ( number3 );
largest = firstNumber;
smallest = firstNumber;
if ( secondNumber > largest )
largest = secondNumber;
if ( thirdNumber > largest )
largest = thirdNumber;
if ( secondNumber < smallest )
smallest = secondNumber;
if ( thirdNumber < smallest )
smallest = thirdNumber;
window.alert(
"Sum: " + ( firstNumber + secondNumber +
thirdNumber ) + "\n" + "Average: " + ( (firstNumber
+ secondNumber + thirdNumber) / 3 ) + "\n" +
"Product: " + ( firstNumber * secondNumber *
thirdNumber ) + "\n" + "Largest: " + largest +
"\n" + "Smallest: " + smallest );
// -->
</script>
</head>
<body>
<p>Click Refresh (or Reload) to run the script again</p>
</body>
</html>