Javascript Programs

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

III Bcom(CA) 2010-2011

JAVASCRIPT PROGRAMS
1. Write a HTML document using Javascript to read two numbers and display the sum of them on the
Webpage.

<html>
<head> <title> Add </title>
<script language="javascript">
function add(form)
{
var a = parseInt(form.a.value);
var b = parseInt(form.b.value);
var c=a+b;
form.c.value=c;
}
</script>
</head>
<form name=form>
<pre>
<center>
<h2> Addition of two numbers </h2>
<p> <b> Enter first number <input type="text" name="a" >
<p> Enter second number <input type="text" name="b" >
<p> <input type="button" value="Add" onclick="add(this.form)" >
<input type="text" name="c" value=" ">
<p><p> <input type="reset" value ="Clear All" >
</b> </pre> </center>
</form>
</html>

Output

A.A.S College 1 Developed by Ansar


III Bcom(CA) 2010-2011

2. Write a HTML document using Javascript to read two strings and display the concatenated string
on the webpage.

<html>
<head><script language="javascript">
function concate(form)
{
var s1=form.string1.value;
var s2=form.string2.value;
form.result.value=s1.concat(s2);
}
</script></head>
<form name=form>
<center>
<h1> Concatenation of two strings </h1>
<b> Enter First String<input type=text name=string1>
<b> Enter Second String<input type=text name=string2>
<p>
<input type=button value=Concatenate onclick="concate(this.form)">
<input type=text name=result>
</center>
</form>
</html>

Output:

3. Write a HTML document using Javascript to read your name and display it with Blue Color,20
size,Lucida Handwriting face, BoldStyle at the center of webpage.

<html>
<head>
<body>
<script language="javascript">
var name=prompt("Enter your name"," ");
document.writeln(" <CENTER> <h3>Given Name is displayed in Blue color,<BR> Fontsize 20,
Fontface=Lucida & Boldstyle at the center of the page </h3></CENTER><BR> <BR> ");
document.writeln("<center><b><font face=lucida size=20 color=blue>"+ name + </font></b></center>");
</script></body> </html>
A.A.S College 2 Developed by Ansar
III Bcom(CA) 2010-2011

OUTPUT:

4. Write a HTML document using Javascript to check and display whether the given number is Prime
or not.

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function calculate(form)
{
var num=parseInt(form.number.value);
if (isNaN(num) || num < 0)
{
form.result.value=(form.number.value + " is not a valid number! Try again!");
}
if (num == 1 || num == 2)
{
form.result.value=(num + " is prime!");
}
for (var i=2;i<num;i++)
{
if (num % i == 0)
{
var prime="yes";
form.result.value=(num + " is not prime. It is divisible by " + i + ".");
break;
}
else var prime="no";
}
if (prime == "no") form.result.value=(num + " is prime!");
}
</SCRIPT>
</HEAD>
<center>

<form name=form>
<h2>Prime Number Calculator</h2><P>
Please enter a number:<br>
A.A.S College 3 Developed by Ansar
III Bcom(CA) 2010-2011

<input type=text name=number size=7>


<input type=button value="Calculate" onClick="calculate(this.form)">
<P>
<input type=text name=result size=45 value="">
</form>
</center>
</html>

Output:

1.

2.

3.

A.A.S College 4 Developed by Ansar


III Bcom(CA) 2010-2011

5. Write a HTML document using Javascript to read the details of a person such as Name, Age,
Highest Qualification and percentage of marks through prompt dialog box and display them along
with the system Date and Time on the webpage.

<html>
<head><title> time </TITLE>

<script language="javascript">
function f()
{
var d=new Date();
var name=prompt("Enter name "," ");
var age=prompt("Enter age"," ");
var qual=prompt("Enter qualification "," " );
var per=prompt("Enter percentage marks"," ");
document.writeln("<h1> STUDENT DETAILS </h1>");
document.writeln("<b>");
document.writeln("Name: " + name+" <p>" );
document.writeln("Age: " + age +" <p>" );
document.writeln("Qualification: " + qual +" <p>" );
document.writeln("Percentage: " + per +" <p>" );
document.writeln("Date: " + d.getDate() + "/" + d.getMonth() + "/" + d.getFullYear() + " " );
document.writeln("Time: " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() );
document.writeln("</b>");
}
</script>
</head>
<body onLoad="f()">
</body>
</html>

Output:

A.A.S College 5 Developed by Ansar

You might also like