0% found this document useful (0 votes)
81 views14 pages

Javascript Assignment: Document - Write ("Difference Between "+a+" and "+B+" Is: "+res)

The document contains 7 JavaScript assignments involving various operations: 1) An arithmetic operator program that takes input and performs addition, subtraction, multiplication and division based on the operator. 2) A program that finds the largest and smallest number among four user input numbers. 3) A prime number checker program that takes a number as input and checks if it is prime. The assignments demonstrate functions, conditionals, and other JavaScript concepts.

Uploaded by

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

Javascript Assignment: Document - Write ("Difference Between "+a+" and "+B+" Is: "+res)

The document contains 7 JavaScript assignments involving various operations: 1) An arithmetic operator program that takes input and performs addition, subtraction, multiplication and division based on the operator. 2) A program that finds the largest and smallest number among four user input numbers. 3) A prime number checker program that takes a number as input and checks if it is prime. The assignments demonstrate functions, conditionals, and other JavaScript concepts.

Uploaded by

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

JAVASCRIPT ASSIGNMENT

1)

<!DOCTYPE html>
<html>
<head>
<title>JSQ1</title>
</head>
<body>
<script type="text/javaScript" language="javaScript"> var
a=parseInt(prompt("Enter the First Value: ")); var
b=parseInt(prompt("Enter the Second Value: ")); var
c=prompt("Enter the Arithmatic Operator: "); var
res;

if(c=="+")
{
res=a+b;
document.write("Summation of "+a+" and "+b+" is: "+res);
}

if(c=="-")
{
if(a>b)
{
res=a-b;
document.write("Difference between "+a+" and "+b+" is: "+res);
}
else
{
res=b-a;
document.write("Difference between "+b+" and "+a+" is: "+res);
}
}
if(c=='*')
{
res=a*b;
document.write("Product of "+a+" and "+b+" is: "+res);
}

if(c=="/")
{
if(a<b)
{
res=a/b;
document.write("Dividing "+a+" by "+b+" we get: "+res);
}
else if(a>b)
{
res=b/a;
document.write("Difference between "+b+}
}
</script>
</body>
</html>

2)

<!DOCTYPE html>
<html>
<head>
<title>JSQ2</title>
</head>
<body>

<script type="text/javascript">
var a=parseInt(prompt("Enter the First Value: "));
var b=parseInt(prompt("Enter the Second Value: "));
var c=parseInt(prompt("Enter the third Value: "));
var d=parseInt(prompt("Enter the Fourth Value: "));
// To Check the Largest value amongst all
numbers if((a>b) && (a>c) && (a>d))
{
document.write(a+" is the biggest amongst all other<br><br>");
}
else if((b>a) && (b>c) && (b>d))
{
document.write(b+" is the biggest amongst all other<br><br>");
}
else if((c>a) && (c>b) && (c>d))
{
document.write(c+" is the biggest amongst all other<br><br>");
}

else if((d>a) && (d>b) && (d>c))


{
document.write(d+" is the biggest amongst all other<br><br>");
}
// To check the smallest amongst all numbers
if((a<b) && (a<c) && (a<d))
{
document.write(a+" is the smallest amongst all other<br><br>");
}
else if((b<a) && (b<c) && (b<d))
{
document.write(b+" is the smallest amongst all other<br><br>");
}

else if((c<a) && (c<b) && (c<d))


{
document.write(c+" is the smallest amongst all other<br><br>");
}
else if((d<a) && (d<b) && (d<c))
{
document.write(d+" is the smallest amongst all other<br><br>");
}
</script>
</body>
</html>
3)

<!DOCTYPE html>
<html>
<head>
<title>Prime Number Check</title>
<script type="text/javascript">
//var n=document.getElementById("test1").value;
//var n= 1;
var n;
//document.write(n);

var flag=0;
function primecheck(n)
{

if (n==1)
{

alert("It is prime");
}
else if(n == 2)
{
alert("It is a Prime number")
}
else
{
for(var x = 2; x < n; x++)
{
if(n % x == 0)
{
var flag=1; break;
alert("It is not a Prime Number`")
}
}
if(flag==0)
alert("It is a Prime Number");
else
alert("it is not a prime no");
}
}
//var a=parseInt(prompt("Enter the Number: "));
//primecheck(n);
</script>
</head>
<body>
<form name='frm1'>
<label>Enter the Number: </label>
<input type="number" id = "num1" name="prime"
> <br>
<input type="button" name="btn" value="Check"
onClick="primecheck(document.frm1.prime.value);">
</form>
</body>
</html>
4)
<html>
<head>
<title>Number Search from Array</title>
<script type="text/javascript">
function num_search()
{
var N=parseInt(prompt("Enter the size of the Array:"));
var arr=new Array(N),j=0;

for(var i=0; i<arr.length; i++)


{
arr[i]=parseInt(prompt("Enter the Array Element:"));
}
var k=parseInt(prompt("Enter the Element to search: "));
for(var i=0; i<arr.length; i++) {

if(k==arr[i])
{
j=1;
break;
}
}
if(j==1)
{
var pos=i+1;
document.writeln("Element "+arr[i]+ " Found at Position:"+pos);
}
else
{
document.writeln("Element Not Found");
}
}
</script>
</head>
<body onLoad="num_search()">
</body>
</html>
5)
<!DOCTYPE html>
<html>
<head>
<title>GCD</title>

<script type="text/javascript">

var a=parseInt(prompt("Enter the first number: "));


var b=parseInt(prompt("Enter the second number: "));

function gcd(a, b)
{
if ((typeof a !== 'number') || (typeof b !== 'number'))
return false;
a = Math.abs(a);
b = Math.abs(b);

while(b)
{
var t = b;
b = a % b;
a = t;
}
return a;

document.write("GCD of "+a+" and "+b+" is: "+gcd(a, b));

</script>
</head>
<body>

</body>
</html>
6)
<!DOCTYPE html>
<html>
<head>
<title>Second Largest Number from the
Array</title> <script type="text/javascript">
function num_search()
{
var N=parseInt(prompt("Enter the size of the Array: "));
var arr=new Array(N);

for(var i=0; i<arr.length; i++)


{
arr[i]=parseInt(prompt("Enter the Array Element: "));
}

for(var i=0;i<arr.length;i++)
{
if(arr[i]>arr[i+1])
{
temp=arr[i+1];
arr[i+1]=arr[i];
arr[i]=temp;
}
}
document.write("Second Largest Number in the Array is: "+arr[arr.length-2]);

}
</script>
</script>
</head>
<body onload="num_search()">

</body>
</html>
7)
<!DOCTYPE html>
<html>
<head>
<title>Palindrome Check</title>
<script>
function Palindrome()
{
var rem, temp, final = 0;
var number = Number(document.getElementById("N").value);

temp = number;
while(number>0)
{
rem = number%10;
number = parseInt(number/10);
final = final*10+rem;
}
if(final==temp)
{
window.alert("The inputed number is Palindrome");
}
else
{
window.alert("The inputted number is not palindrome");
}
}
</script>
</head>
<body>
<br>
<h1>Whether a number is Palindrome or not</h1>
Enter The Number :<input type="text" name="n" id = "N"/>
<hr color="cyan">
<br>
<center><button
onClick="Palindrome()">CHECK</button> </body>
</html>
8)
<!DOCTYPE html>
<html>
<head>
<title>Form</title>

<script type="text/javascript">

function check()
{
var textbox=document.getElementById("tb1");
if(tb1.value.length>=1 &&
tb1.value.length<=30) {
alert("Yes.. Length of Name is less than 30");
}
else
{
alert("Sorry.. Length of Name can not exceed 30");
}
}

function regcheck()
{
var regchk=document.getElementById("tb2");
if(tb2.value.length==10)
{
alert("Yes, the length of Registration number is 10");
}
else
{
alert("No, the length of Registration number less than 10");
}
}

function idcheck()
{
var idchk=document.getElementById("tb3");
if(idchk.includes("."))
{
var flag1=1;
}
if(idchk.includes("@"))
{
var flag2=1;
}

if(flag1==1 && flag2==1)


{
alert("Yes,Email id entered is correct");
}

}
</script>
</head>
<body>

<br><br><br>
<center>
<form>
<table border="3">
<tr>
<td colspan="2" align="center">
FORM
</td>
</tr>
<tr>
<td>Name</td>
<td>
<input type="text" name="tf1" id="tb1" placeholder="Your Name here: ">
</td>
</tr>
<tr>
<td>
Registration Number
</td>
<td>
<input type="number" name="tf2" id="tb2" placeholder="Your Registration Number: ">
</td>
</tr>
<tr>
<td>
Email Id:
</td>
<td>
<input type="text" name="tf3" id="tb3"
placeholder="[email protected]"> </td>
</tr>
<tr>
<td colspan="2">
<input type="button" name="submit" value="SUBMIT" onclick="check(),regcheck()">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
9)
<!DOCTYPE html>
<html>
<head>
<title>Marbles Check</title>
<script type="text/javascript">
var num;
var gross=144;
var dozens=12;
var res1,res2,res3,res4,res5,res6;
function marbelcheck(num)
{
res1=num/gross;
res1=Math.floor(res1);
res2=gross*res1;
res3=num-res2;
res4=res3/dozens;
res4=Math.floor(res4);
res5=dozens*res4;
res6=res3-res5;
alert("You have "+res1+" Gross, "+res4+" Dozens and "+res6+" marbels");
}
</script>
</head>
<body>
<br><br>
<center>
<hr color="red">
<form name="frm1">
Enter the number of Marbels you have:
<input type="number" name="mar1" placeholder="Enter numbers here" >
<br><br>
<input type="button" name="btn" value="Check"
onClick="marbelcheck(document.frm1.mar1.value);">
</form>
</center>
</body>
</html>

You might also like