Professional Documents
Culture Documents
Web Tech
Web Tech
Web Tech
3
Objective: Use javascript to develop the following programs:
a) To calculate multiplication and division of two numbers(input from
user):-
Theory:- HTML tags used in this program are :
iv. <form>:- The <form> tag is used to create an HTML form for user input.
vi. <input>:- The <input> tag specifies an input field where the user can enter
data.
Source Code:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body div{
margin: 4px 0px;
}
</style>
</head>
<body>
<p>Sample form:</p>
<form>
<div>
1st Number:<input type="number" id="firstNumber" >
</div>
<div>
2nd Number:<input type="number" id="secondNumber">
</div>
<!-- <button id="mul" onClick="multiplyBy()">Multiply</button> -->
<input type="button" onClick="multiplyBy()"
Value="Multiply" />
<!-- <button id="div" onClick="divideBy()">Divide</button> -->
<input type="button" onClick="divideBy()"
Value="Divide" />
</form>
<p>The Result is : <br>
<span id = "result"></span>
</p>
<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>
</body>
</html>
Output:-
Multiplication O/P:
Divide O/P:
iii. <input>:- The <input> tag specifies an input field where the user can
enter data.
iv. <span>:- The <span> tag is an inline container used to mark up a part
of a text, or a part of a document.The <span> tag is easily styled by
CSS or manipulated with JavaScript using the class or id attribute.
Source Code:-
<html>
<head>
<style>
body div{
margin: 4px 0px;
}
</style>
</head>
<body>
<div>
String:<input type="text" id="String" placeholder="Enter the string" >
</div>
<input type="button" onClick="CountVowels()" Value="Result" />
<p>The Result is : <br>
<span id = "result"></span>
</p>
<script>
function CountVowels()
{
var s1 = document.getElementById("String").value;
var m = s1.match(/[aeiou]/gi);
var c = null ? 0 : m.length;
document.getElementById("result").innerHTML =c;
}
</script>
</body>
</html>
Output:-
After operation:-
Source Code:-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>
VALUE:<input id="value" placeholder="enter the value">
</div>
<input type="button" onClick="Check()" Value="Result" />
<p>The Result is : <br>
<span id = "result"></span>
</p>
<script>
function Check()
{
var a=document.getElementById("value").value;
// if(isNaN(a))
// {document.getElementById("result").innerHTML ="not a number";}
// else
// {document.getElementById("result").innerHTML ="number";}
// if(typeof a == 'number'){
// document.getElementById("result").innerHTML ="number";
// }else{
// document.getElementById("result").innerHTML ="not a number";
// }
if(a%1 == 0){
document.getElementById("result").innerHTML ="number";
}else{
document.getElementById("result").innerHTML ="not a number";
}
}
</script>
</body>
</html>
Output:-
After operation:-