Program no: 13
Odd Or Even Using Javascript
Aim: Write a javascript program to check whether a number is Even or Odd using alert box.
Program:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript</h1>
<script>
var number = prompt ("Enter a number to find Odd or Even ","Type
your number here");
if (number % 2 == 0)
{
alert ("The number entered is even.");
}
else
{
alert ("The number entered is odd.");
}
</script>
</body>
</html>
Output
Program no :14
JavaScript Array Sum
Aim: Create javascript program find the sum of elements in an array .
Program:
<html>
<head>
<title>JavaScript Array Sum</title>
</head>
<body>
<h1>JavaScript Array Sum</h1>
<script>
var array = [1, 2, 3, 4, 5];
var sum=0;
for(var i=0; i<[Link];i++)
var sum = sum+array[i];
[Link]("Sum of Values in an Array =",sum);
</script>
</body>
</html>
Output:
Program No: 15
Square Of Numbers
Aim: Create a user defined function square to find square of numbers from 1 to 10.
Program
<!DOCTYPE html>
<html>
<head>
<title>A User-Defined square Function</title>
<style type = "text/css">
margin: 0;
</style>
<script>
[Link]( "<h1>Square the numbers from 1 to 10</h1>" );
for (var x = 1; x <= 10; ++x)
[Link]( "<p>The square of " +x+ " is " +square(x)+ "</p>" );
function square (y)
return y * y;
</script>
</head>
<body>
<! -- empty body element -->
</body>
</html>
Output:
Program No:16
Prompt Dialog Box
Aim: Write a javascript code to show prompt dialog box.
Program:
<html>
<head>
<script>
function login()
var name = prompt ("Enter your UserName");
if(name!=null)
[Link]("<h2><center>Welcome. "+name +"</center></h2>");
else
[Link]("<h2><center>Please Enter Your Name</center></h2>");
</script>
</head>
<body>
<center>
<h1> Crayambal Inc </h1>
<p> Please Login to View Details </p>
<form>
<input type = "button" value = "Login" onclick = "login()" >
</form>
</center>
</body>
</html>
Output:
Program No:17
Form Validation
Aim: Create JavaScript code to validate a form created using HTML
Program:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" >
<title>Form Validation</title>
</head>
<body>
<h1>REGISTRATION FORM</h1>
<form name="RegForm" onsubmit="validateForm()" autocomplete="on">
<label for="name">Name:</label>
<input type="text" id="name" name="Name" placeholder="Enter your full name" >
<p>
<label for="email">E-mail Address:</label>
<input type="text" id="email" name="EMail" placeholder="Enter your email correctly" >
</p>
<p>
<label for="password">Password:</label>
<input type="password" id="password" name="Password" >
</p>
<p>
<input type="checkbox" id="agree" name="Agree" >
<label for="agree">I agree to the above information</label>
</p>
<input type="submit" value="Send" name="Submit" >
<input type="reset" value="Reset" name="Reset" >
</form>
<script>
function validateForm()
var name = [Link]("name").value;
var email = [Link]("email").value;
var password = [Link]("password").value;
var agree = [Link]("agree").checked;
var isValid = true;
if (name == "")
alert( "Please enter your name.");
isValid = false;
if (email == "" || )
alert("Please enter your email correctly.");
isValid = false;
if (password == "" || [Link] < 6)
alert("Please enter your password correctly.");
isValid = false;
if (!agree)
alert("Please agree to the above information.");
isValid = false;
if(isValid)
[Link](" Form Submitted Successfully");
}
</script>
</body>
</html>
Output:
Program No:18
Mouseclick Event
Aim: Write a javascript code to show Mouseclick Event.
Program:
<html>
<head>
<title>Mouseclickevent</title>
</head>
<body>
<h3>Javascript Events </h3>
<script language="Javascript" type="text/Javascript">
function clickevent()
[Link]("Hello World");
</script>
<form>
<input type="button" onclick="clickevent()" value="Click"/>
</form>
</body>
</html>
OUTPUT:
Program No:19
Reverse A Number
Aim: Write a javascript code to Reverse a number.
Program:
<html>
<title>Reverse a number</title>
<script type ="text/javascript">
function rev_num()
var num = prompt("Enter the number to be reveresed :", " ");
var n= num;
var rev = 0, rem;
while (n>0)
rem = n % 10;
rev = rev * 10 + rem ;
n = [Link](n/10);
[Link]("The given number is : " +num+ " <br/> The reversed number is : "
+rev+"\n");
</script>
<body onload="rev_num();">
<body>
</html>
Output:
Program No:20
Factorial
Aim: Write a javascript program to find the factorial of a numbers.
Program:
<html>
<head>
<title> Factorial </title>
<script type="text/javascript">
function factorial()
var n,i,f=1;
n=prompt("Enter Number");
for(i=1;i<=n;i++)
f=f*i;
[Link]("Factorial="+f);
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type="button" onclick="factorial()" value="Factorial">
</form>
</body>
</html>
OUTPUT:
Program No:21
Sum Of Array Using HTML, Javascript And CSS As Separate
External Files
Aim: Create a website to calculate sum of array using HTML, Javascript and CSS as separate external
files.
Program:
[Link]
<html>
<body>
<link rel="stylesheet" href="[Link]">
<h1> JavaScript Array Sum </h1>
<script src="[Link]"></script>
</body>
</html>
[Link]
body {
background-color: black;
}
h1 {
color: #000;
font-family: Arial, Helvetica, sans-serif;
}
[Link]
var number=[1,2,5,2];
var sum=0;
for(var i=0;i<[Link];i++)
{
sum+=number[i];
}
[Link](number);
[Link]("<br>The sum of above numbers are:"+sum);
OUTPUT:
Program No:22
XML
Aim: Display an XML tree structure of a bookstore.
Program:
[Link]
<?xml version="1.0" encoding="UTF-8"?>
<!—Bookstore Data structured with XML -->
<bookstore>
<book category="fiction">
<title>Harry Potter</title>
<author>J.K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="non-fiction">
<title>Introduction to XML</title>
<author>John Doe</author>
<year>2010</year>
<price>39.95</price>
</book>
</bookstore>
OUTPUT: