0% found this document useful (0 votes)
11 views8 pages

JavaScript Programs for Number and String Operations

Uploaded by

janvhij11comm
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)
11 views8 pages

JavaScript Programs for Number and String Operations

Uploaded by

janvhij11comm
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

SOP 1: Create a JavaScript program for the following using appropriate

variables, JavaScript inbuilt functions, and control structures.

 To accept an integer and display the result by multiplying it with


3.
 To accept two integers and display a larger number of them.
 To check whether the user entered number is positive or
negative.
Answer:
[Link]
To accept integer and display the result by multiplying it with 3.
<!DOCTYPE html>
<head>
<title>To accept integer and display the result by multiplying it with
3.</title>
</head>
<body>
<script language=”javascript”>
var a,no,ans;
a=prompt(“Enter any value”);
no=parseInt(a);
ans=no*3;
document.
write(“The answer is :”+ans);
</script>
</body>
</html>

[Link]
To accept two integers and display larger number of them.
<!DOCTYPE html>
<head>
<title>To accept two integers and display larger number of them.</title>
</head>
<body>
<script language="javascript">
var a,b;
a=prompt("Enter first integers");
b=prompt("Enter second integers");
if(a>b)
{
alert("First ineteger is largest:"+a);
//[Link]("First integer is largest:"+a)
}
else
{
alert("Second ineteger is largest:"+b);
//[Link]("Second integer is largest:"+b)
}
</script>
</body>
</html>

[Link]
To check whether, user entered number is positive or negative.
<!DOCTYPE html>
<head>
<title>To check whether, user entered number is positive or negative</title>
</head>
<body>
<script language=”javascript”>
var n;
n=prompt(“Enter any number”);
if(n>0)
[Link](“Number is Positive”);
else
[Link](“Number is Negative”);
</script>
</body>
</html>

SOP 2: Create a JavaScript program for the following using appropriate


variables, JavaScript inbuilt functions, and control structures.

 To accept two positive or negative numbers and check whether


they are equal or not.
 To accept a number and display the square of it.
 To check whether the accepted integer is multiple of 3 or
multiple of 7.
Answer:
[Link]
To accept two positive or negative numbers and check whether they
are equal or not.
<!DOCTYPE html>
<head>
<title>To accept two positive or negative numbers and check whether they
are equal or not</title>
</head>
<body>
<script language="javascript">
var no1,no2;
no1=prompt("Enter first number");
no2=prompt("Enter Second number");
if(no1==no2)
{
[Link]("Both are equal");
}
else
{
[Link]("Given numbers are not equal");
}
</script>
</body>
</html>

[Link]
To accept number and display square of it.
<!DOCTYPE html>
<head>
<title>To accept number and display square of it</title>
</head>
<body>
<script language=“javascript”>
var no,sqr;
no=prompt(“Enter Any number”);
sqr=no*no;
document, write (“The Square is=”+sqr);
</script>
</body>
</html>
[Link]
To check whether the accepted integer is multiple of 3 or multiple of
7.
<!DOCTYPE html>
<html>
<head>
<title>To check whether the accepted integer is multiple of 3 or multiple of
7.</title>
</head>
<body>
<script language=”JavaScript”>
var a;
a=prompt(“Enter your first integer / number”);
if(a%3==0||a%7==0)
{
alert(“multiple of 3 or 7”);
}
else
{
alert(“not a multiple of 3 or 7”);
}
</script>
</body>
</html>
__________________________________________________________________________________

SOP 3: Create a JavaScript program for the following using appropriate


variables, JavaScript inbuilt string functions, and control structures.

 To accept a string and calculate its length.


 To accept a string and display it in lowercase and uppercase.
 To check whether the length of the string is 4 or greater.
Answer:
[Link]
To accept a string and calculate its length.
<!DOCTYPE html>
<head>
<title>program8</title>
</head>
<body>
<script language=”javascript”>
var a;
a=prompt(‘Enter string’);
[Link](“The length is=”+[Link]);
</script>
</body>
</html>

[Link]
To accept string and display it into lowercase and uppercase.
<!DOCTYPE html>
<head>
<title> To accept string and display it into lowercase and uppercase</title>
</head>
<body>
<script language=”javascript”>
var a;
a=prompt(“Enter any string”);
[Link](“<br>Entering Strings “+a);
[Link](“<br>Lowercase=”+[Link]());
[Link](“<br>Uppercase=”+[Link]());
</script>
</body>
</html>

[Link]
To check whether the length of string is 4 or greater.
<!DOCTYPE html>
<html>
<head>
<title> To check whether the length of string is 4 or greater </title>
</head>
<body>
<script language=JavaScript>
var a,b;
a=prompt(“Enter the string to check”);
b=[Link];
if(b==4 || b>=4)
{
alert(“Entered string length is 4 or greater”);
}
else
{
alert(“Entered string length is below 4”);
}
</script>
</body>
</html>

SOP 4: Create event-driven JavaScript programs for the following using


appropriate variables, JavaScript inbuilt functions, and control structures.
To accept numbers and validate if the given value is a number or not by
clicking on the button.

To calculate the addition and division of two numbers.

Answer:
[Link]
To accept number and validate if the given value is a number or not
by click
<!DOCTYPE html>
<html>
<head>
<title> To accept number and validate if the given value is a number or not
by clicking on the button.
</title></head><body>
<script language="JavaScript">
function display()
{
var a;
a=[Link];
if(a>=0)
{
alert("Value is a number"+a);
}
else
{
alert("Value is a string"+a);
}
}
</script>
<form name=form1>
Enter Value: <Input type="text" name="t1"><br><br>
<Input type="button" value="Check" onClick="display()">
</form>
</body>
</html>

[Link]
To calculate addition and division of two numbers.
<!DOCTYPE html>
<head>
<title>To calculate addition and division of two numbers.</title></head>
<body>
<script langauge="javascript">
function add()
{
var a,b,result;
a=[Link];
b=[Link];
result=parseInt([Link]) + parseInt([Link]);
alert("The Addition is ="+result);
}
function div()
{
var a,b,d;
a=[Link];
b=[Link];
d=[Link]/[Link];
alert("Divide is ="+d);
}
</script>
<form name="f1">
1st Number : <input type="text" name="t1"><br>
2nd Number : <input type="text" name="t2"><br>
<input type="button" value="Addition" name="b1" onClick="add()">
<input type="button" value="Division" name="b2" onClick="div()">
</form>
</body>
</html>

You might also like