Javascript Practicals
Javascript Practicals
DOCTYPE html>
<html>
<head>
<script>
function multiplyBy()
{
num1 = document.getElementById("firstNumber").value;
num2 = document.getElementById("secondNumber").value;
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>
<meta charset=utf-8 />
<title>JavaScript program to calculate multiplication and division of two numbers </title>
<style type="text/css">
body {margin: 30px;}
</style>
</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
Create event driven JavaScript program to convert temperature to and from Celsius, Fahrenheit.
Formula: c/5= (f-32)/9
[where c=Temperature in Celsius and f=Temperature in Fahrenheit.]
Output format : 40 Celsius=104 Fahrenheit
45 Fahrenheit = 7.22222222 Celsius
Temp.html
<!DOCTYPE html>
<html>
<head>
<title>Temperature Conversion</title>
<body>
<h2>JavaScript code to convert Celcius to Fahrenhiet or vice-versa</h2>
<br>
<p>Enter the Temperature</p>
<p><input type="text" id="txt_celsius" onkeyup="convert('C')"> Temperature in degree Celsius</p>
<br>
<p><input type="text" id="txt_fah" onkeyup="convert('F')"> Temperature in degree Fahrenheit</p>
<script>
function convert(temperature) {
var t;
if (temperature == "C") //Celsius to fahrenit
{
t = document.getElementById("txt_celsius").value * 9 / 5 + 32;
document.getElementById("txt_fah").value = Math.round(t);
}
else //fahrenirt to celsius
{
t = (document.getElementById("txt_fah").value -32) * 5 / 9;
document.getElementById("txt_celsius").value = Math.round(t);
}
}
</script>
//Note:-you can remove math.round function if you need answer in decimal
</body>
</html>
Create event driven JavaScript program for the following. Make use of appropriate variables, JavaScript
inbuilt string functions and control structures.To accept string from user and reverse the given string and
check whether it is palindrome or not
Palindrome.html
<!DOCTYPE html>
<html>
<head>
<title>Palindrome</title>
<script type="text/javascript">
function chk_palindrome()
{
var str,str_case,i,len;
str=f1.t1.value;
str_case=str.toLowerCase();
len=str_case.length;
var p=1;
for(i=0;i<len/2;i++)
{
if(str_case.charAt(i)!=str_case.charAt(len-1-i))
{
p=0;
break;
}
}
if(p==1)
{
alert("Entered string is Palindrome");
}
else
{
alert("Entered string is Not a Palindrome")
}
}
</script>
</head>
<body>
<form name="f1">
Enter the string to check it is palindrome or not!
<br>
<input type="text" name="t1">
<br>
<br>
<input type="button" name="check_palin" value="Check String" onclick="chk_palindrome()">
</form>
</body>
</html>
Create event driven JavaScript program for the following. Make use of appropriate variables, JavaScript
inbuilt string functions and control structures.
To accept string from user and count number of vowels in the given string.
Js1.html
<!DOCTYPE html>
<html>
<head>
<title>count number of vowels in the given string</title>
<script type="text/javascript">
function getVowels()
{
var s,i,ch,c;
c=0;
s=frm1.t1.value;
for(i=0;i<=s.length;i++)
{
ch=s.charAt(i);
alert(ch);
if(ch=="A"||ch=="a"||ch=="E"||ch=="e"||ch=="I"||ch=="i"||ch=="O"||ch=="o"||ch=="U"||ch=="u")
{
c++;
}
}
document.write("<h1>Number of Vowels in string are :"+c);
}
</script>
</head>
<body bgcolor="pink">
<form name="frm1">
<h2> To accept string from user and count number of vowels in the given string. </h2>
Enter your Name : <input type="text" name="t1" placeholder="Enter any string here"><br><br>