Sei sulla pagina 1di 14

SCRIPTING LANGUAGES LAB

1. Write a JavaScript program to compute the sum of an array of integers


2. Write a JavaScript program to determine whether a given year is a leap year in the
Gregorian calendar
3. Write a JavaScript for loop that will iterate from 0 to 15. For each iteration, it will check if
the current number is odd or even, and display a message to the screen
4. Write a JavaScript program to calculate multiplication and division of two numbers
5. Write a JavaScript program to convert temperatures to and from Celsius, Fahrenheit
6. Write a JavaScript function that reverses a number
7. Write a JavaScript function that checks whether a passed string is palindrome or not
8. Write a JavaScript program to test the first character of a string is uppercase or not
9. Write a JavaScript program to set the background colour of a paragraph
10. Write a JavaScript program to check the given number is mobile number or not using
form
11. Write a VBScript program for Fibonacci using for loop
12. Write a VBScript Program for age validation
13. Write a VBScript program to Copy contents of one folder to other folder
14. Write a VBScript program to demonstrate the checkbox and list box

Prepared by Lt. J.Hajiram Beevi, Dept of IT, JMC, Trichy Page 1


1.Write a JavaScript program to compute the sum of an array of integers
Aim: To write a program to compute the sum of an array of integers using javascript.
Procedure:
 Start the execution of the program
 Create the array with 6 elements
 Calculate the sum of array of integers using for loop
 Print the value of sum
 Stop the execution of the program
Coding:
<html><head>
<title>Compute the sum of an array of integers</title>
<script>
var array = [1, 2, 3, 4, 5, 6], s = 0, i;
for (i = 0; i < array.length; i += 1)
s += array[i];
document.write("Sum of array of Integers: "+s);
</script></head><body></body></html>
Result: Thus the calculation of sum of array of integers program is developed and executed
successfully.
2.Write a JavaScript program to determine whether a given year is a leap year in the
Gregorian calendar
Aim: To write a program to determine whether a given year is a leap year or not using
javascript.
Procedure:
 Start the execution of the program
 Declare and define the function leapYear()
 Using the conditional constructs, check whether the given year is leap year or not.
 Show the result using ‘alert’ dialog box.
 Stop the execution of the program.
Coding:
<html><head>
<title>Leap Year</title>

Prepared by Lt. J.Hajiram Beevi, Dept of IT, JMC, Trichy Page 2


<script>
function leapYear (year) {
if (year % 4 == 0){
if( year % 100 == 0){
if( year % 400 != 0) {
alert (" " + year + " is not a Leap Year");
return false;}
else {
alert (" " + year + " is a Leap Year");
return true;}}
else{
alert(" "+ year +" is a leap year");
return true;}}
else{
alert(""+ year +" is not a leap year");
return false;}}
var year=prompt("enter a year to find leap year");
leapYear (year);
</script>
</body></html>
Result: Thus the program for checking the given year is leap year or not is developed and
executed successfully.
3.Write a JavaScript for loop that will iterate from 0 to 15. For each iteration, it will
check if the current number is odd or even, and display a message to the screen
Aim: To write a javascript program for checking the given number is odd or even.
Procedure:
 Start the execution of the program
 Using for loop and conditional construct, check whether the given number is odd or
even
 Print the output
 Stop the execution of the program

Prepared by Lt. J.Hajiram Beevi, Dept of IT, JMC, Trichy Page 3


Coding:
<html><head>
<title>Odd/Even</title><head><script>
for(var num=0;num<=15;num++) {
if(num==0){
document.write(num + " is even");
document.write("<br>");}
else if (num%2==0) {
document.write(num + " is even");
document.write("<br>");}
else{
document.write(num + " is odd");
document.write("<br>");}}
</script></head></html>
Result: Thus the program for checking the given number is odd or even is developed and
executed successfully.
4.Write a JavaScript program to calculate multiplication and division of two numbers
Aim: To write a javascript program to calculate the multiplication and division of two
numbers.
Procedure:
 Start the execution of the program
 Declare and define two functions multiplyBy() and divideBy()
 Get the input values num1 and num2 from user using form element
 Print the output
 Stop the execution of the program
Coding:
<html> <head>
<title>JavaScript program to calculate multiplication and division of two numbers </title>
<script>
function multiplyBy() {
num1 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;

Prepared by Lt. J.Hajiram Beevi, Dept of IT, JMC, Trichy Page 4


document.getElementById("result").innerHTML = num1 * num2;}
function divideBy() {
num1 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;
document.getElementById("result").innerHTML = num1 / num2;}
</script></head><body><form>
1st Number : <input type="text" id="firstNumber" /><br>
2nd Number: <input type="text" id="secondNumber" /><br>
<input type="button" onClick="multiplyBy()" Value="Multiply" />
<input type="button" onClick="divideBy()" Value="Divide" /></form>
<p>The Result is : <br>
<span id = "result"></span></p></body></html>
Result: Thus the javascript program for multiplication and division of two numbers is
developed and executed successfully.
5.Write a JavaScript program to convert temperatures to and from Celsius, Fahrenheit
Aim: To write a javascript progam to convert temperatures to and from Celsius, Fahrenheit.
Procedure:
 Start the execution of the program
 Declare and define two functions FartoCen() and CentoFar()
 Get the input values Far and Cen from user using form element
 Print the output
 Stop the execution of the program
Coding:
<html> <head><title>Fahrenheit and Centigrade Temperatures</title>
<script>
function FartoCen(){
Far = document.getElementById("fartocen").value;
Cen=(Far-32)*5/9;
document.write("Fahrenheit " + Far+"\xB0F is " + Cen+"\xB0C." ); }
function CentoFar() {
Cen = document.getElementById("fartocen").value;
Far=Cen*9/5+32;

Prepared by Lt. J.Hajiram Beevi, Dept of IT, JMC, Trichy Page 5


document.write("Centigrade " + Cen+"\xB0C is " + Far+"\xB0F" ); }
</script></head><body><form>
Enter the Centigrade/Farenheit value : <input type="text" id="fartocen" /><br>
<input type="button" onClick="FartoCen()" Value="Centigrade" />
<input type="button" onClick="CentoFar()" Value="Fahrenheit" /></form></body></html>
Result: Thus the javascript program for temperature conversion is developed and executed
successfully.
6.Write a JavaScript function that reverses a number
Aim: To write a javascript program to reverse the given number.
Procedure:
 Start the execution of the program.
 Declare and define the function rev_num()
 Get the input num from the user
 Print the result
 Stop the execution of the program
Coding:
<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 = Math.floor(n/10);}
document.write("The given number is : " +num+ " <br/> The reversed number is : " +rev+
"\n");}
</script><body onload="rev_num();"> <body></html>
Result: Thus the javascript program to reverse the given number is developed and executed
successfully.

Prepared by Lt. J.Hajiram Beevi, Dept of IT, JMC, Trichy Page 6


7.Write a JavaScript function that checks whether a passed string is palindrome or not
Aim: To write a javascript program to check whether a passed string is palindrome or not.
Procedure:
 Start the execution of the program
 Declare and define two functions myFunction() and checkPalindrome()
 Get the input ‘str’ from the user
 Print the output
 Stop the execution of the program
Coding:
<html><head><title>Check Palindrome with JavaScript Program</title>
<script>
function myFunction(){
var str = document.getElementById('txtbox').value;
var result = checkPalindrome(str);
alert('The Entered String "'+str +'" is "'+result+'"');}
function checkPalindrome(str){
var orignalStr;
str = str.toLowerCase();
orignalStr = str;
str = str.split('');
str = str.reverse();
str = str.join('');
var reverseStr = str;
if(orignalStr == reverseStr){
return 'Palindrome';
}else{
return 'Not Palindrome';}}
</script></head><body>
<form action="" method="get">
<h3>Palindrome</h3>
<input type="text" id="txtbox" placeholder="Enter String" />
<input type="button" onclick="myFunction()" value="Check Palindrome" />

Prepared by Lt. J.Hajiram Beevi, Dept of IT, JMC, Trichy Page 7


</form></body></html>
Result: Thus the program for checking whether the passed string is palindrome or not is
developed and executed successfully.
8.Write a JavaScript program to test the first character of a string is uppercase or not
Aim: To write a javascript program to test the first character of a string is uppercase or not.
Procedure:
 Start the execution of the program
 Declare and define the function upper_case()
 Pass the input string using the function
 Print the output
 Stop the execution of the program
Coding:
<html><head>
<title>Check string's first character is uppercase or not</title> </head>
<script>
function upper_case(str) {
regexp = /^[A-Z]/;
if (regexp.test(str)) {
document.write("The given string " + str +"'s first character is uppercase");
document.write("<br>");}
else {
document.write("The given string " + str +"'s first character is not uppercase");
document.write("<br>");} }
upper_case('hello');
upper_case('Hajira') ;</script><body> </body> </html>
Result: Thus the program for testing the first character of a string is uppercase or not is
developed and executed successfully.
9.Write a JavaScript program to set the background colour of a paragraph
Aim: To write a javascript program to set the background colour of a paragraph text.
Procedure:
 Start the execution of the program
 Declare and define the function set_background()

Prepared by Lt. J.Hajiram Beevi, Dept of IT, JMC, Trichy Page 8


 Using onclick() method, change the background of a paragraph text.
 Stop the execution of the program.
Coding:
<html>
<head>
<title>Set the background color of a paragraph</title>
<script>
function set_background() {
docBody = document.getElementsByTagName("body")[0];
myBodyElements = docBody.getElementsByTagName("p");
myp1 = myBodyElements[0];
myp1.style.background = "rgb(255,0,0)";
myp2 = myBodyElements[1];
myp2.style.background = "rgb(255,255,0)";}
</script></head><body>
<input type="button" value="Change Background color"onclick="set_background()">
<p>Welcome to Jamal Mohamed College(Autonomous), Trichy</p>
<p>Welcome to Department of Information Technology</p>
</body></html>
Result: Thus the program for setting the background colour of a paragraph is developed and
executed successfully.
10.Write a JavaScript program to check the given number is mobile number or not
using form
Aim: To write a javascript program to check whether the given number is mobile no or not.
Procedure:
 Start the execution of the program
 Declare and define the function MobileNoValidation()
 Get the input mobile no from the user
 Print the output
 Stop the execution of the program
Coding:
<html><head><title>Mobile No Validation</title>

Prepared by Lt. J.Hajiram Beevi, Dept of IT, JMC, Trichy Page 9


<script type="text/javascript">
function MobileNoValidation() {
var fid = document.getElementById('txtbox');
if (fid.value == "" || fid.value == null) {
alert("Please enter your Mobile No.");
return false; }
if (fid.value.length < 10 || fid.value.length > 10) {
alert("Mobile No. is not valid, Please Enter 10 Digit Mobile No.");
return false; }
alert("Success ");
return true;}
</script></head><body>
<form action="" method="get">
<h3>Mobile Number validation</h3>
<input type="text" id="txtbox" placeholder="Enter Mobile No" />
<input type="button" onclick="MobileNoValidation();" value="CheckMobileno" />
</form></body></html>
Result: Thus the program for checking whether the given number is valid mobile number or
not is developed and executed successfully.
11.Write a VBScript program for Fibonacci using for loop
Aim: To write a VBScript program to generate Fibonacci series using ‘for’ loop.
Procedure:
 Start the execution of the program
 Declare and initialize variables a=0 and b=1
 Using for loop generate the series
 Print the output
 Stop the execution of the program
Coding:
<html><meta http-equiv="X-UA-Compatible" content="IE=10">
<body><script type="text/vbscript">
option explicit
dim a,b,c,i

Prepared by Lt. J.Hajiram Beevi, Dept of IT, JMC, Trichy Page 10


a=0
b=1
for i=1 to 10
c=a+b
document.write(b&"<br/>")
a=b
b=c
next
</script></body></html>
Result: Thus the vbscript program for generating Fibonacci series is developed and executed
successfully.
12.Write a VBScript Program for age validation
Aim: To write a VBscript program for age validation.
Procedure:
 Start the execution of the program
 Read the input age from the user using input box
 Using conditional construct, perform the age validation
 Stop the execution of the program.
Coding:
<html><meta http-equiv="X-UA-Compatible" content="IE=10"><head>
<script type="text/vbscript">
Dim age
age = InputBox("Enter your age")
If age<18 Then
document.write("You are too young.")
ElseIf age<45 Then
document.write("You are still young.")
ElseIf age<70 Then
document.write("You are getting older.")
Else
document.write("You are too old.")
End If

Prepared by Lt. J.Hajiram Beevi, Dept of IT, JMC, Trichy Page 11


</script></head><body></body></html>
Result: Thus the vbscript program for validating the age is developed and executed
successfully.
13.Write a VBScript program to Copy contents of one folder to other folder
Aim: To write a vbscript program to copy contents of one folder to other folder
Procedure:
 Start the execution of the program
 Specify the source and destination folder names explicitly
 Using FileSystemObject() function, copy the contents of one folder into another
 Stop the execution of the program
Coding:
Const OverWriteFiles=True
Dim strSource
Dim strDestination
Dim objFSO
Dim startTime
Dim endTime
startTime=Timer
WScript.Echo "Beginning copy"
strSource="C:\Users\Boss\Desktop\NewFolder"
strDestination="C:\Users\Boss\Desktop\ekalavya"
Set objFSO=CreateObject("Scripting.FileSystemObject")
objFSO.CopyFolder strSource, strDestination, OverWriteFiles
endTime=Timer
WScript.Echo "ending copy."
Result: Thus the vbscript program for copying the contents of one folder into another folder
is developed and executed successfully.
14.Write a VBScript program to demonstrate the checkbox and list box
a. Check Box
Aim: To write a vbscript program to illustrate the use of check box.
Procedure:
 Start the execution of the program

Prepared by Lt. J.Hajiram Beevi, Dept of IT, JMC, Trichy Page 12


 Declare and define two functions cmdButton1_click() and cmdButton2_click()
 Get the choice of input from the user
 Display the output
 Stop the execution of the program
Coding:
<HTML><meta http-equiv="X-UA-Compatible" content="IE=10"><HEAD>
<TITLE>Check Box</TITLE>
<Script Language=VBSCRIPT>
Sub cmdButton1_onclick
document.MyForm.White.checked=True
End Sub
Sub cmdButton2_onclick
document.MyForm.White.checked=False
End Sub
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="MyForm">
<INPUT type="checkbox"name="White"Value="White">
<INPUT type="button"name="cmdButton1"Value="Set Test Check TRUE">
<INPUT type="button"name="cmdButton2"Value="Set Test Check FALSE">
<br></form>
</BODY></HTML>
Result: Thus the vbscript program for illustrating the use of check box is developed and
executed successfully.
b. List Box
Aim: To write a vbscript program to illustrate the use of list box control.
Procedure:
 Start the execution of the program
 Get the choice of input from the user and perform the case selection
 Display the output
 Stop the execution of the program

Prepared by Lt. J.Hajiram Beevi, Dept of IT, JMC, Trichy Page 13


Coding:
<html><meta http-equiv="X-UA-Compatible" content="IE=10">
<head>
<title>List Box</title>
</head
<body>
<select name="sel1">
<option>Kiwi
<option>Cherry
<option>Blue Berries
</select>
<input type="submit"name=btn1 value="test">
<script type="text/vbscript">
sub btn1_onclick
select case sel1.selectedindex
case 0
msg="you selected kiwi"
case 1
msg="you selected cherry"
case 2
msg="you selected blueberries"
end select
msgbox msg
end sub
</script></body></html>
Result: Thus the vbscript program for illustrating the use of list box is developed and
executed successfully.

Prepared by Lt. J.Hajiram Beevi, Dept of IT, JMC, Trichy Page 14

Potrebbero piacerti anche