Practical – 7
Aim: (A) Write a JavaScript code to perform the following tasks:
1) Take input from prompt box and calculate factorial of the number
using while loop.
2) Display the result of factorial using alert box.
3) Sort the array in descending order using for loop.
Code: 1 & 2
<html>
<head>
<script>
var a = parseInt(prompt("value"));
var fact=1;
//a=[10,20,30];
while (a >1)
{
fact=fact*a;
a=a-1;
}
document.write(fact);
console.log(fact);
alert(fact);
</script>
</head>
</html>
Code: 3
<html>
<head>
<script>
Web Designing (IT4014)
a=[1,2,3,4,5,6,7,8,9];
var i=0;
var j=0;
var temp=0;
for (i=0;i<a.length;i++)
{
for(j=i;j<a.length;j++)
{
if(a[i] < a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
document.write(a);
</script>
</head>
</html>
Output:
1 and 2)
Web Designing (IT4014)
3) Sort the array in descending order using for loop.
Web Designing (IT4014)
Aim: (B) Create a simple calculator for addition, multiplication and
division using switch case.
Code:
<html>
<head>
<script>
var operand1 = parseInt(prompt("Enter Value 1:"));
var operand2 = parseInt(prompt("Enter Value 2:"));
var c = prompt("Enter the operator (+, -, *, /):");
var result=0;
switch(c)
{
case "+":
result = operand1 + operand2;
break;
case "-":
result = operand1 - operand2;
break;
case "*":
result = operand1 * operand2;
break;
case "/":
result = operand1 / operand2;
break;
default:
console.log("Invalid operator");
}
document.write(result)
</script>
Web Designing (IT4014)
</head>
</html>
Output:
Web Designing (IT4014)
Practical – 8
Aim: Write a code using JavaScript for validation of form to perform the
given tasks:
a) No input type must be blank or unselected.
b) Text input like name must not have numbers, Text input like phone
must not have alphabets.
c) Text input like zip must be of length six.
d) Email entered must be of given pattern.
e) Mail Address 1 must be different from Mail address 2.
Code:
<html>
<head>
<style>
form
{
border: 2px solid black;
width: 500px;
margin: 300 auto;
padding: 20px;
text-align: center;
}
</style>
</head>
<body>
Web Designing (IT4014)
<form>
<center><h1>Employee Registration Form by Nirmal</h1></center>
<label for="fname">First Name:</label>
<input type="text" id="fname" name="fname" pattern="[A-Za-z]+"
required><br><br>
<label for="lname">Last Name:</label>
<inputtype="text" id="lname" name="lname" pattern="[A-Za-z]+"
required><br><br>
<label for="zipcode">Zipcode:</label>
<input type="text" id="zipcode" name="zipcode" pattern="[0-9]{6}"
required><br><br>
<label for="email1">Email Address 1:</label>
<input type="email" id="email1" name="email1" required><br><br>
<label for="email2">Email Address 2:</label>
<input type="email" id="email2" name="email2" required><br><br>
<input type="submit" value="Submit" onclick="validateForm()">
</form>
<script>
function validateForm()
{
const fnameInput = document.getElementById("fname");
const zipcodeInput = document.getElementById("zipcode");
const email1Input = document.getElementById("email1");
const email2Input = document.getElementById("email2");
if (fnameInput.value.trim() === "" || /\d/.test(fnameInput.value))
{
Web Designing (IT4014)
alert("Please enter a valid first name");
return false;
}
if (zipcodeInput.value.trim() === "" || zipcodeInput.value.trim().length !== 6)
{
alert("Please enter a valid zipcode (6 digits)");
return false;
}
if (email1Input.value.trim() === email2Input.value.trim())
{
alert("Please enter two different email addresses");
return false;
}
alert("Employee registration successful");
return true;
}
</script>
</body>
</html>
Output:
A & B ) Name must be filled out and no numbers
Web Designing (IT4014)
C) Zipcode has must of length of six digits
D) Email entered must be of given pattern
Web Designing (IT4014)
E) Mail address 1 must be different from mail address 2.
Web Designing (IT4014)