Javascript Programs PDF
Javascript Programs PDF
Conditional Statements:
1. Javascript program on arithmetic operations
<html>
<head><title>Arithmetic Operations</title>
<script language = "javascript">
var res;
function add()
{
var s1 = parseInt(document.f1.t1.value);
var s2 = parseInt(document.f1.t2.value);
res = s1+ s2;
document.f1.res.value = res;
}
function sub()
{
var s1 = parseInt(document.f1.t1.value);
var s2 = parseInt(document.f1.t2.value);
res = s1- s2;
document.f1.res.value = res;
}
function mul()
{
var s1 = parseInt(document.f1.t1.value);
var s2 = parseInt(document.f1.t2.value);
res = s1 * s2;
document.f1.res.value = res;
}
function div()
{
var s1 = parseInt(document.f1.t1.value);
var s2 = parseInt(document.f1.t2.value);
res = s1/ s2;
document.f1.res.value = res;
}
</script></head>
<body>
<br><br><br><h2>MINI CALCULATOR PROGRAM</h2>
<br><br><form name = "f1">
<font size = 03 face = "Courier New">
<b>Enter first number : <input type = "text" size = "10" name = "t1"><br><br>
Enter second number : <input type = "text" size = "10" name = "t2"><br><br>
Result is : <input type = "text" size = "10" name = "res">
<br><br></b></font>
<INPUT TYPE="button" value="Addition" onclick = "add()">
<INPUT TYPE="button" value="Subtraction" onclick = "sub()">
<INPUT TYPE="button" value="Multiplication" onclick = "mul()">
<INPUT TYPE="button" value="Division" onclick = "div()">
</form>
</body></html>
Examples on Arrays:
1. Program to input n numbers display in sorting order
<html>
<head><script language = "javascript">
var n, i, j, temp;
n = parseInt(window.prompt("Enter value of n:", "0"));
var a = new Array(n);
for(i=0; i<n; i++)
a[i] = parseInt(window.prompt("Enter "+i+"th element:", "0"));
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
if(a[i]>a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
document.write("<h1>Sorted elements are:</h1>");
for(i=0; i<n; i++)
document.write("<h1>"+a[i] + " ");
</script></head>
</html>
2. Program for date and time in body tag and script in head tag.
<html>
<head><pre>
<script language = "javascript">
function start()
{
var mydate = new Date();
c.innerText= "Today is : "+mydate.toString();
}
</script>
</pre></head>
<body bgcolor = "wheat" onload="start()">
<font face = verdana size = 3 color = darkblue>
<h3 id = c></h3></font>
</body>
</html>
if(pos == 0){
masg = msg1;
pos = 1;
}
else if(pos == 1){
masg = msg2;
pos = 0;
}
document.title = masg;
timer = window.setTimeout("titlebar("+pos+")",speed);
}
titlebar(0);
</script>