0% found this document useful (0 votes)
136 views

Javascript Lab Programs

The document contains 5 programs: 1) a program to swap two variables, 2) a program to generate random numbers, 3) a program to count vowels in a string, 4) a program to check the number of occurrences of a character in a string, and 5) a program to pass a JavaScript function as a parameter.

Uploaded by

daniel007work
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views

Javascript Lab Programs

The document contains 5 programs: 1) a program to swap two variables, 2) a program to generate random numbers, 3) a program to count vowels in a string, 4) a program to check the number of occurrences of a character in a string, and 5) a program to pass a JavaScript function as a parameter.

Uploaded by

daniel007work
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Program No: 1: Write a program to Swap Two Variable

<html>
<head>
</head>
<body>
<label>Enter the first number
<input id="num1">
</label>
<br><br>
<label>Enter the Second number
<input id="num2">
</label>
<br><br>
<button onclick="swap()">
Swap
</button>
<input id='sum'>
<script>
function swap() {
var x, y, z,m;
x = Number( document.getElementById("num1").value );
y = Number( document.getElementById("num2").value );
z= x;
x=y;
y=z;
m=x+y;
// Comment the code section
document.getElementById("sum").value = m;
document.getElementById("num1").value = x;
document.getElementById("num2").value = y;
}
</script>
</body>
</html>

Output :

Program No: 2 Write a program to generate random numbers.


<html>
<head>
<script>
function getRndInteger()
{
var randomNumber;
randomNumber = Math.floor(Math.random() * 100);
document.getElementById("num1").value = randomNumber;
}
</script>
</head>
<body>
<h2>JavaScript Random Number</h2>
<label> Random Number generated <input id="num1"> </label>
<br><br>
<button onclick="getRndInteger()"> Click here</button>
</body>
</html>

Output :

Program No: 3 Write a program to Count the Number of Vowels in a String


<!DOCTYPE html>
<html>
<head>
<title>Check the number of Vowels in a given String</title>
<script>
function vowelCount() {
var count = 0;
var input1 = document.getElementById("str").value;
for (var i = 0; i < input1.length; i++) {
if (input1.charAt(i) == 'a' || input1.charAt(i) == 'e' || input1.charAt(i) == 'i' ||
input1.charAt(i) == 'o' || input1.charAt(i) == 'u') {
count++;
}
}
document.getElementById("vowelCountResult").value = count;
}
</script>
</head>
<body>
<label> String entered is <input id="str"> </label><br><br>
<label> Number of Vowels <input id="vowelCountResult"> </label>
<button onclick="vowelCount()"> Check </button>
</body>
</html>

Output

Program No: 4 Write a


program to Check the Number of Occurrences of a Character in the String.
<html>
<head>
<title>Character Count Code!!!</title>
<script>
function checkChar() {
var str,c, count = 0;
str=document.getElementById("str1").value;
c= document.getElementById("cha").value ;

// looping through the items


for (var i = 0; i < str.length; i++) {
// check if the character is at that position
if (str.charAt(i)==c) {
count += 1;
}
}
document.getElementById("str2").value=count;

}
</script>
</head>
<body>
<label>Enter the String <input id="str1"> </label>
<br><br>
<label>Enter the Character <input id="cha"> </label>
<br><br>
<label>Number of Occurences is <input id="str2"> </label>
<button onclick="checkChar()"> Check </button>

</body>
</html>

Output :
Program No: 5 Write a java script program to pass a 'javascript function' as parameter.
<html>
<head>
<meta charset="utf-8">
<title>JavaScript program to pass a JavaScript function as parameter</title>
<script>
function addStudent(id, refreshCallback)
{ document.write("The id of the student is "+id+" ");
refreshCallback();
}
function refreshStudentList() {
document.write("Aakash");
}
addStudent(101, refreshStudentList);
</script>
</head>
<body>
</body>
</html>
OUTPUT:

You might also like