You are on page 1of 16

1.Write a javascript function to generate Fibonacci series till user defined limit.

<html>
<head>
<title>
Program to display Fibonacci Series
</title>
<script>
function str()
{
num=parseInt(prompt("Enter a number :"));
t1=0;
t2=1;
ans=0;
buff=0;
document.write("<br>"+"Program to display Fibonacci Series "+"<br><hr><br>")
if(num==0)
document.write("Can't display Series !!");
else
{
document.write("Displaying Series for "+num+" numbers :<br>");
if(num==1)
document.write(t1);
else
{
document.write(t1+"<br>");
document.write(t2+"<br>");
for(i=3;i<=num;i++)
{
ans=t1+t2;
t1=t2;
t2=ans;
document.write(ans+"<br>");
}
}
}
document.write("<br><hr><br>"+"Executed By Shaikh Salman !! ");
}
</script>
</head>
<body onload="str()">
</body>
</html>
2. Write a simple calculator program using switch case in JavaScript.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple calculator</title>
</head>
<body>
<script>
var num1 = prompt("enter num1:");
var num2 = prompt("enter num2:");
var result = prompt("\nChoose Below
Options:\n1.Addition\n2.subtraction\n3.Multiplication\n4.Division");
switch(result){
case "1": alert("Addition of "+num1+" and "+num2+" is :"+(num1+num2));
break;
case "2": alert("subtraction of "+num1+" and "+num2+" is :"+(num1-num2));
break;
case "3": alert("Multiplication of "+num1+" and "+num2+" is :"+(num1*num2));
break;
case "4": alert("division of "+num1+" and "+num2+" is :"+(num1/num2));
break;
default:alert("Invalid Option!");
}
</script>
</body>
</html>
4. Write a JavaScript program that will display current date in DD/MM/YYYY format.
and Write Java script to create person object with properties firstname, lastname, age,
eye color, delete eye color property and display remaining properties of person object

DD/MM/YY

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display Current Date</title>
</head>
<body>
<script>
function getCurrentDate() {
var currentDate = new Date();
var day = currentDate.getDate();
var month = currentDate.getMonth() + 1; // Months are zero-based
var year = currentDate.getFullYear();

// Add leading zeros if necessary


if (day < 10) {
day = '0' + day;
}

if (month < 10) {


month = '0' + month;
}
var formattedDate = day + '/' + month + '/' + year;
document.write("Current Date:", formattedDate);
}
getCurrentDate();
</script>
</body></html>
5. Write JavaScript to check whether the entered string is palindrome or not.

<html>
<head>
<title>Exp 3.1</title>
</head>
<body>
<script>
function pal(str,rev)
{
if(str==rev)
document.write("<br>"+str+" is a Palindrome");
else
document.write("<br>"+str+" is not a Palindrome");
}
document.write("<h3><b>WAP to check whether the entered string is palindrome or
not.</b></h3><hr><br>");
var s=prompt("Enter a string :");
var r=s.split('');
r=r.reverse();
r=r.join("");
document.write("Original String : "+s);
pal(s,r);
</script>
</body>
</html>

6. Write a JavaScript program that will remove the duplicate element from an array.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Remove duplicate element</title>
</head>
<body>
<script>
var arr1 = [1,3,4,5,3,4,8];
var demoarr = [];
document.write("Orignal Array:"+arr1+"<br>");
for(var i=0 ; i<=arr1.length ; i++){
if(demoarr.indexOf(arr1[i])==-1){
demoarr.push(arr1[i]);
}
}
document.write("After removing duplicate:"+demoarr);
</script>
</body>
</html>

7. Write a JavaScript function to merge two array & removes all duplicate values.

<html>
<head>
<title>Assignment 2</title>
</head>
<body>
<h2>Write a JavaScript function to merge two array & removes all duplicate
values.</h2>
<script>
var arr1 = [1,2,3,4]
var arr2 = [1,2,3,4,5,6,7]
var varconcat = arr1.concat(arr2)
document.write("Orignal Array : "+varconcat)
var demoArr = []
for(var i = 0 ; i<=varconcat.length ; i++)
{
if(demoArr.indexOf(varconcat[i])==-1)
{
demoArr.push(varconcat[i])
}
}
document.write("<br>Array After removing duplicate values : "+demoArr)
</script>
</body>
</html>
8. Write a Java Script code to display elements of array (56, 35, 90,12, 50, 46, 26) in sorted
order.

<html>
<body>
<script>
var a = [56,35,90,12,50,46,26];
document.write("Original : "+a);
document.write("\nSorted : "+a.sort());
</script>
</body>
</html>

15. Write a javascript program for creating following structure .

<html>
<head>
<title>Frame Demo</title>
</head>
<body>
<table border="1">
<tr>
<td align="center" colspan="2">
FRAME 1
</td>
</tr>
<tr>
<td>
FRAME 2
<ul>
<li>
<a href="fruits.html"
target="mainframe">FRUITS</a>
</li>
<li>
<a href="flowers.html"
target="mainframe">FLOWERS</a>
</li>
<li>
<a href="cities.html"
target="mainframe">CITIES</a>
</li>
</ul>
</td>
<td>
FRAME 3<BR>
<iframe name="mainframe"></iframe>
</td>
</tr>
</table>
</body>
</html>

16. "Write a javascript program to design HTML page with books information Book name and its
price in tabular format, use rollovers to display the discounted price
at the place of original price "

<html>
<head>
<title>Books Information</title>
<style>
table {
border-collapse: collapse;
width: 60%;
margin: 20px auto;
}
th, td {
border: 1px solid black;
padding: 8px;
text-align: center;
}
th {
background-color: #f2f2f2;
}
</style>
<script>
function displayDiscount(book) {
var discountInfo = document.getElementById(book);
discountInfo.style.visibility = "visible";
}
function hideDiscount(book) {
var discountInfo = document.getElementById(book);
discountInfo.style.visibility = "hidden";}
</script>
</head>
<body>
<table>
<tr>
<th>Book Title</th>
<th>Author</th>
<th>Price</th>
<th>Discount</th>
</tr>
<tr onmouseover="displayDiscount('book1')" onmouseout="hideDiscount('book1')">
<td>Book 1</td>
<td>Author 1</td>
<td>$20.00</td>
<td><p id="book1" style="visibility:hidden;">20% off</p></td>
</tr>
<tr onmouseover="displayDiscount('book2')" onmouseout="hideDiscount('book2')">
<td>Book 2</td>
<td>Author 2</td>
<td>$25.00</td>
<td><p id="book2" style="visibility:hidden;">15% off</p></td>
</tr>
<!-- Add more rows for additional books -->
</table>
</body>
</html>

19. Write a script for creating following frame structure : Frame 1 contains three buttons
SPORT,MUSIC and DANCE that will perform following action : When user clicks SPORT button,
sport.html webpage will appear in Frame 2. When user clicks MUSIC button, music.html
webpage will appear in Frame 3. When user clicks DANCE button, dance.html webpage will
appear in Frame 4.

<!DOCTYPE html>
<html>
<head>
<title>Frame Structure</title>
<script>
function loadPage(page, frameId) {
document.getElementById(frameId).src = page;
}
</script>
</head>

<body>

<table border="1" width="100%" height="100%">


<tr>
<!-- Frame 1 with buttons -->
<td colspan="4" align="center">
<button onclick="loadPage('sport.html', 'frame2')">SPORT</button>
<button onclick="loadPage('music.html', 'frame3')">MUSIC</button>
<button onclick="loadPage('dance.html', 'frame4')">DANCE</button>
</td>
</tr>
<tr>
<!-- Frame 2 for SPORT -->
<td width="25%" height="75%" align="center">
<iframe id="frame2" name="frame2" width="100%" height="100%"
frameborder="0"></iframe>
</td>
<!-- Frame 3 for MUSIC -->
<td width="25%" height="75%" align="center">
<iframe id="frame3" name="frame3" width="100%" height="100%"
frameborder="0"></iframe>
</td>
<!-- Frame 4 for DANCE -->
<td width="25%" height="75%" align="center">
<iframe id="frame4" name="frame4" width="100%" height="100%"
frameborder="0"></iframe>
</td> </tr> </table></body></html>

20. "Write a Javascript to create a pull – down menu with three options [Google,MSBTE,
Yahoo] once the user will select one of the options then user will be redirected to that
site."

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>drop down list</title>
</head>
<body>
<select id="selectwebsite" onchange="selectedsite()">
<option value="" disabled selected>CHOOSE YOUR WEBSITE</option>
<option value="https://www.google.com">GOOGLE</option>
<option value ="https://www.yahoo.com">YAHOO</option>
<option value ="https://www.msbte.org.in">MSBTE</option>
</select>
<script>
function selectedsite(){
var web = document.getElementById("selectwebsite").value;

if(web){ window.location.href = web; }} </script></body></html>

21. Write a javascript program to link banner advertisements to different URLs.

<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body onload="dispBanner()">
<img onclick="openLink()" src="" id="banner" height="200" width="200">
</body>

<script>
var banImage = Array('1.png','2.png','3.png');
var banUrl = Array('youtube.com','instagram.com','gmail.com');
var currentIndex = -1
var image = document.getElementById("banner")

function openLink(){
window.location = "https://"+banUrl[currentIndex];
}
function dispBanner(){
currentIndex++
if(currentIndex == banImage.length){
currentIndex = 0
}
image.src = banImage[currentIndex]
setTimeout(dispBanner,2000)
}
</script>
</html>

23. Write a Java script to modify the status bar using rollover

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Status Bar Rollover</title>
<script>
function changeStatusBar(message) {
window.status = message;
return true;
}

function resetStatusBar() {
window.status = "";
return true;
}
</script>
</head>
<body>

<p onmouseover="changeStatusBar('Hovered!');" onmouseout="resetStatusBar();">


Hover over me to change the status bar.
</p>

</body>
</html>

24. Create a slideshow with the group of three images, also simulate next and previous
transition between slides in your Java script.

<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body onload="dispBanner()">
<img onclick="openLink()" src="" height="200" width="200" id="banner">
<button onclick="prevImg()">Previous</button>
<button onclick="nextImg()">Next</button>
</body>
<script>
var banImage = Array('1.png','2.png','3.png');
var banUrl = Array('youtube.com','instagram.com','gmail.com');
var currentIndex = 0;
var image = document.getElementById("banner");
function dispBanner(){
image.src = banImage[currentIndex];
}
function openLink(){
window.location = "https://"+banUrl[currentIndex];
}
function nextImg(){
currentIndex++;
if(currentIndex==banImage.length){
currentIndex = 0;
}
image.src = banImage[currentIndex];
}

function prevImg(){
currentIndex--;
if(currentIndex == -1){
currentIndex = banImage.length-1;
}
image.src = banImage[currentIndex];
} </script> </html>

25. Write a JavaScript for moving car using setTimeOut() and clearTimeOut() method.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
#car {
background-color:red;
color:white;
position: absolute;
top: 50px;
left: 50px;
}
</style>
</head>
<body>
<div id="car"><img src="car.jpg"></div>
<input type="button" onclick="fun()" value = "stop car">
<input type="button" onclick="moveCar()" value = "move car">
<script>
var carElement = document.getElementById('car');
var currentPosition = 50;
var mov;
function moveCar() {
currentPosition += 5;
carElement.style.left = currentPosition + 'px';
if (currentPosition < window.innerWidth - 50) {
move = setTimeout(moveCar, 50);
}}
function fun(){
clearTimeout(move);
}
moveCar();
</script>
</body>
</html>

26. Write a javascript program to changing the contents of a window.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Change Window Content</title>
</head>
<body>

<h1 id="content">Hello, this is the initial content!</h1>


<button onclick="changeContent()">Change Content</button>

<script>
function changeContent() {
var newContent = "Now, the content has been changed!";
document.getElementById("content").innerText = newContent;
}
</script>

</body>
</html>
27. Write a javascript program to validate user accounts for multiple set of user ID
and password (using swith case statement).

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>User Account Validation</title>
</head>
<body>
<script>
function validateUser() {
var userId = prompt("Enter your user ID:");
var password = prompt("Enter your password:");

switch (userId) {
case "user1":
switch (password) {
case "pass1":
alert("User 1 successfully logged in!");
break;
default:
alert("Invalid password for User 1.");
break; }
break;
case "user2":
switch (password) {
case "pass2":
alert("User 2 successfully logged in!");
break;
default:
alert("Invalid password for User 2.");
break;}
break;
case "user3":
switch (password) {
case "pass3":
alert("User 3 successfully logged in!");
break;
default:
alert("Invalid password for User 3.");
break;}
break;
default:
alert("Invalid user ID.");
break; } }
// Call the function to start the validation process
validateUser();
</script>
</body>
</html>

29. Write a Java script that will replace following specified value with another value in a
string.
String = “ I will fail”
Replace “fail” by “pass”

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>replace String</title>
</head>
<body>
<script>
var string = "I will fail";
document.write("Orignal String:"+string+"<br>");
var replaceString = string.replace("fail",'pass');
document.write("Replaced String:"+replaceString);
</script>
</body>
</html>

30.Develop javascript to convert the given character to unicode and vice-versa.

<!DOCTYPE html>
<html>

<body>
<script>
var str ="a hello world";
var a = String.fromCharCode(97);
document.writeln("<br>"+a);
document.write("<br>"+str.charCodeAt(0));
</script>
</body>
</html>

You might also like