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

Java Script

Uploaded by

3wpao
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Java Script

Uploaded by

3wpao
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Q.12 Write a JavaScript program that find the Sum of digits of a two digits number.

[Note: Use the Alert Message to display the result]


HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<script src="1.js"></script>
</body>
</html>

JavaScript Code

var num = prompt ("Enter the two digit number here ");
var first = num %10;
num = parseInt(num/10);
num = num+first;
document.write("The sum of the digits of the number is "+num);

OUTPUT
Q.13 Write a JavaScript program that find all the even numbers from 1-100 using while
loop.

HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<script src="2.js"></script>
</body>
</html>

JavaScript Code

document.write("The even numbers from 1 to 100 are ");


var n = 0;
while (n < 100){
document.write(n+ ",");
n +=2;
}

OUTPUT
Q.14 Write a JavaScript program that prints all the Armstrong numbers between 1 to 1000
using for loop.
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<script src="3.js"></script>
</body>
</html>

JavaScript Code

document.write("All the arm strong numbers from 1 to 1000 are ");


for (var n = 0; n < 1000; n++) {
var i = n;
var sum =0;
while(i>0){
var rem=i%10;
sum = sum +( rem*rem*rem);
i=parseInt(i/10);
}
if(sum==n){
document.write(n+ ",");
}
}

OUTPUT
Q.15 Write a JavaScript program that display the below given pattern using Do While loop.
1
1, 2
1, 2, 3
1, 2, 3, 4
1, 2, 3, 4, 5
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<script src="4.js"></script>
</body>
</html>

JavaScript Code

var a= 1;
do{
var b=1;
while(b<=a)
{
document.write(b +" ");
b++;
}
document.write("<br>");
a++;
}while(a<=5)

OUTPUT
Q.16 Write a JavaScript program that perform a linear search on an array of strings.
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<script src="5.js"></script>
</body>
</html>

JavaScript Code

var array = ["sodium","hydrogen", "oxygen", "carbon" , "oxygen","iron"];


alert("The array is "+array+'.');
var element=prompt("Enter the element you want to search");
var x= 0;

for (let i = 0; i < array.length; i++)


{
if(element==array[i]){
x=1;
}
}
if(x==1){
alert("Found");
}else{
alert("Not Found");
}
OUTPUT
Q.17 Write a JavaScript program that perform sorting of an array of integers.
[Note: use new operator]
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<script src="6.js"></script>
</body>
</html>

JavaScript Code

var array = new Array();


array[0]=3;
array[1]=2;
array[2]=1;
document.write("The array before sorting is "+array+"<br>");
var flag=true ;
while(flag){
flag=false;
for (let i = 0; i < array.length; i++) {
if(array[i]>array[i+1]){
var temp = array[i];
array[i]=array[i+1];
array[i+1]=temp;
flag=true;
}
}}
document.write("The array after sorting is "+array);

OUTPUT
Q.18 Write a JavaScript program that find the Factorial of a number using a function.
[Note: Use Arguments]
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<script src="7.js"></script>
</body>
</html>

JavaScript Code

function factorial (num) {


let x=1;
while(num>0){
x= x*num;
num--;
}
return x;
}
let num=prompt ("Enter the number");
alert("The factorial is " + factorial(num));

OUTPUT
Q.19 Write a JavaScript program that prints the Fibonacci series using “onmousehower”
event.
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<button onmouseover="display()">Fibonacci series</button>
<script src="8.js"></script>
</body>
</html>

JavaScript Code

function f(n){
if (n<=1){
return n;
}
return f(n-1)+f(n-2);
}
function display(){
document.write("Fibonacci series is ")
for (var i = 0; i < 10; i++)
{
document.write( f(i) + ",");
}
document.write("...");
}

OUTPUT
Before

After
Q.20 Write a JavaScript program that creates five Menus on a web page and rhe colour
changes on the mouse hover.
HTML 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>
<button id="b1" onmouseover="change('b1')">B1</button>
<button id="b2" onmouseover="change('b2')">B2</button>
<button id="b3" onmouseover="change('b3')">B3</button>
<button id="b4" onmouseover="change('b4')">B4</button>
<button id="b5" onmouseover="change('b5')">B5</button>
</div>
<script src="9.js"></script>
</body>
</html>

JavaScript Code

function change(id){
document.getElementById(id).style.backgroundColor="red";
}

OUTPUT
Before

After

You might also like