CssPrAns
CssPrAns
Write a JavaScript code to accept three names of flowers in an array and display them
using for loop.
Ans:
<html>
<body>
<script type="text/javascript">
var i;
flowers[0]=prompt("Enter name");
flowers[1]=prompt("Enter name");
flowers[2]=prompt("Enter name");
for(i=0; i<flowers.length;i++)
document.write(flowers[i] + "<br>");
</script>
</body>
</html>
Write a JavaScript code to display table of entered number using while loop.
Ans:
<html>
<body>
<script type="text/javascript">
var i=1;
while(i<=10)
{
i++;
</script>
</body>
</html>
Write a JavaScript code to accept a number and display its factorial using loop.
Ans:
<!DOCTYPE html>
<html>
<head>
<title>Factorial Calculator</title>
</head>
<body>
<h2>Factorial Calculator</h2>
<script>
function calculateFactorial() {
var factorial = 1;
if (number < 0) {
return;
}
for (var i = 1; i <= number; i++) {
factorial *= i;
</script>
</body>
</html>
Write a JavaScript code to accept a number and display whether it is prime or not.
Ans:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
if (number % i === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
} else {
</script>
</body>
</html>
Write a JavaScript code to accept three numbers and display a greatest number using
logical AND operator.
Ans:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
var greatest;
greatest = num1;
} else {
greatest = num3;
</script>
</body>
</html>
Write a JavaScript code to declare an array with five names of flowers and display them
in sorted order.
Ans:
<html>
<head></head>
<body>
<script type="text/javascript">
var i;
for(i=0;i<flowers.length;i++)
document.write("<br>" + flowers[i]);
flowers.sort();
for(i=0;i<flowers.length;i++)
document.write("<br>" + flowers[i]);
}
</script>
</body>
</html>
Write a JavaScript code to declare an array with five elements and display them in
reverse order of entry in an array.
Ans:
<html>
<head></head>
<body>
<script type="text/javascript">
var i;
flowers.reverse();
</script>
</body>
</html>
Write a JavaScript code to declare an array with three colors. Merge all array elements
in one string, separated with ‘&’ character and display the string. Write a JavaScript
code to copy one array into another array.
Ans:
<html>
<head></head>
<body>
<script type="text/javascript">
var i;
document. write ("Array elements are: ");
for(i=0;i<arr.length;i++)
{
document. write("<br>" + arr[i]);
var str=arr.join('&');
document. write("<br>String = " + str);
</script>
</body>
</html>
<html>
<body>
<script>
var originalArray=[1,2,3,4,5];
var copiedArray=originalArray.slice();
document.write("Original Array: " + originalArray.join(",")+"<br>");
</body>
</html>
Write a JavaScript code to define a function that accepts one number as an argument
and display its cube.
Ans:
<html>
<body>
<script type="text/javascript">
function cube(no1)
{
var c=no1*no1*no1;
document.write("Cube of number " + no1 + "=" + c);
}
</script>
</body>
<script type="text/javascript">
var n=prompt("Enter number");
cube(n);
</script>
</html>
Write a JavaScript code to define a function input() that accept data of a student as rollno
,name and marks.
<html>
<body>
<script>
function input(){
var rollno,name,marks;
rollno=prompt("Enter roll number");
name=prompt("Enter name");
marks=prompt("Enter marks");
document.write("<br>Roll number: " + rollno);
document.write("<br>Name: " + name);
document.write("<br>Marks: " + marks);
calculate(marks);
}
function calculate(marks){
if(marks>50)
document.write("<br>Grade: Pass");
else
document.write("<br>Grade: Fail");
}
input();
</script>
</body>
</html>
Write a JavaScript code to define one more function as calculate that assigns result as
pass or fail. Call both the functions and display result with all data.
<html>
<body>
<script>
function input(){
var rollno,name,marks;
rollno=prompt("Enter roll number");
name=prompt("Enter name");
marks=prompt("Enter marks");
document.write("<br>Roll number: " + rollno);
document.write("<br>Name: " + name);
document.write("<br>Marks: " + marks);
calculate(marks);
}
function calculate(marks){
if(marks>50)
document.write("<br>Grade: Pass");
else
document.write("<br>Grade: Fail");
}
input();
</script>
</body>
</html>
Write a JavaScript code to define two functions as total() to calculates sum of five subjects
marks out of 500 and average() that displays average of marks. (Use function call from
another function).
Ans:
<html>
<body>
<h2>Marks Calculation</h2>
<button onclick="calculate()">Calculate</button>
<script>
function total() {
return 85 + 90 + 78 + 88 + 92;
}
function average() {
return total() / 5;
}
function calculate() {
alert("Total Marks: " + total() + " out of 500\nAverage Marks: " + average());
}
</script>
</body>
</html>
Write a JavaScript code to define a function compare() that accepts two numbers and
returns greater number after comparison. Display the greater number in <body> HTML
document.
Ans:
<html>
<body>
<script>
</script>
</body>
</html>
Write a JavaScript code to accept email-id from user search ‘@’ symbol in the entered
string and display message as ‘valid email id’ if @ is present in entered string.
Ans:
<html>
<body>
<script type="text/javascript">
var index=str1.indexOf('@');
if(index!=-1)
else
</script>
</body>
</html>
Write a JavaScript code that will display count of occurrence of a character in a string.
Ans: <html>
<body>
<h2>Character Count</h2>
<script>
function countCharacter() {
let count = 0;
count++;
alert("The character '" + charToCount + "' appears " + count + " times in the string.");
</script>
</body>
</html>
write JavaScript code to create and display persistent cookies with name and rollno.
<html>
<body>
<form name="userentry">
</form>
</body>
<script>
function writecookie() {
with(document.forms.userentry) {
var ck=cname.value;
document.cookie="Custname=" + ck+";";
now.setMonth(now.getMonth()+1);
document.cookie="expires="+now.toUTCString()+";";
document.write("Cookies are set! Customer Name: " + ck + ", Roll Number: " + roll);
</script>
</html>
Write JavaScript code to open new window and close opened window after clicking on
OPEN and CLOSE buttons.
<html>
<body>
<script>
var mywindow;
function openwindow()
mywindow=window.open("http://www.google.com","google","width=400,height=400,top=2
00,left=200");
function closewindow()
mywindow.close();
</script>
</body>
</html>
Write JavaScript code to search for presence of any character between J-P in string.
Returns True if series character is present in string.
<html>
<head>
<script>
function validate()
{
var name='Jayvant';
re = /[J-P]/;
if (re.test(name))
{
alert('Match Found');
}
else
{
alert('Match Not Found');
}
}
</script>
</head>
<body>
<form>
<p>
<input name="reg" value="test" type="button" onclick="validate ()">
</p>
</form>
</body>
</html>
<html>
<head>
<script>
function validate()
{
with(document.forms.form1)
var name=form1.t1.value;
re = /\b[0-9]{10}\b/;
if (re.test(name))
else
</script>
</head>
<body>
<form name="form1">
<p>
</p>
</form>
</body>
</html>
<head>
<title>rollover text</title>
</head>
<body>
<tr valign="top">
<td width="50">
<a>
</a>
</td>
<td>
<a onmouseover="document.cover.src='s2.jpg'">
<br>
<br>
</td>
</tr>
</table>
</body>
</html>
Write a JavaScript code to apply image rollover effect on a web page.
<html>
<body>
<tr valign="top">
<td width="50">
<a>
</a>
</td>
<td>
</td>
<td>
<b><u>java demystified</u></b>
</td>
</tr>
</table>
</body>
</html>
<html>
<head>
</head>
<body>
<script>
if (number % i === 0) {
isPrime = false;
break;
if (isPrime) {
} else {
</script>
</body>
</html>
Write HTML code to design a form with one list box and submit button. Call JavaScript
function when webpage is loaded in the browser and add five elements in list box
dynamically.
Ans:
<!DOCTYPE html>
<html>
<head>
<title>Dynamic List Box</title>
<script>
function addOptions() {
var listBox = document.getElementById("myListBox");
var options = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5"];
<form>
<label for="myListBox">Choose an option:</label>
<select id="myListBox" name="myListBox"></select><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Write JavaScript code to create a pull-down menu with three options as URL of three
different web pages. When any option is selected from menu, corresponding web page
should be loaded in web browser.
Ans:
<html>
<head>
<script>
function getPage(choice)
{
page=choice.options[choice.selectedIndex].value;
if(page != "")
{
window.location=page;
}
}
</script>
</head>
<body>
<form name="myform">
Select Your Favourite Website:
<select name="MenuChoice" onchange="getPage(this)">
<option value="select any option">Select</option>
<option value="https://www.codecademy.com/catalog/language/javascript/">CodeAcademy
</option>
<option value="https://www.msbte.org.in">MSBTE</option>
<option value="https://www.javatpoint.com/javascript-tutorial">JavaTpoint</option>
</form>
</body>
</html>
Write code to create following output on browser window. Frame 2 contains links. When
links are clicked corresponding page opens in frame3.
Frame 2:
Cookies Frame 3
Rollover
StatusBar
Ans:
first.html:
<html>
<body>
<h1><b>Frame 1:</b></h1>
Welcome to JavaScript<br>
</body>
</html>
second.html:
<html>
<body>
<h1><b>Frame 2:</b><br>
<ul>
<li><a href="cookies.html" target="mainframe">Cookies</a></li>
<li><a href="Rollover.html" target="mainframe">Rollover</a></li>
<li><a href="statusbar.html" target="mainframe">Statusbar</a></li>
</ul>
</body>
</html>
third.html:
<html>
<body>
<h1><b>Frame 3:</b></h1>
<br>
</body>
</html>
cookies.html:
<html>
<body>
Hello Bhava...!
</body>
</html>
main.html:
<html>
<head>
<title>Create a Frame</title>
</head>
<frameset rows="30%,*" border="5">
<frame src="first.html" name="a" />
<frameset cols="30%,*" border="5">
<frame src="second.html" name="b" />
<frame src="third.html" name="mainframe" />
</frameset>
</frameset>
</html>
write a program to create dynamic menu with semester menu and subject’s
menu
Ans:
<html>
<head>
<title>Dynamically Changing Menu Options</title>
<script type="text/javascript">
fifthsem = new Array('CSS','AJP','OSY'); // Array elements for
subject menu if sem fifth is selected
sixthsem = new Array('PHP','MAD', 'ETI'); // Array elements for
subject menu if sem sixth is selected
function Getsubjects(Semester)
{
for(i=document.Form1.subjects.options.length-1;i>0; i--)
{
document.Form1.subjects.options.remove(i) //Clear existing
options from subjects
}
semvalue = Semester.options[Semester.selectedIndex].value; //
finding selected semester from sem menu
if (semvalue != "")
{
if (semvalue == '1')
{
for (i=1; i<=fifthsem.length;i++)
{
document.Form1.subjects.options[i] =new Option(fifthsem[i-1]);
// loading options from array into subjects menu for fifth sem
}
}
if (semvalue == '2')
{
for (i=1; i<=sixthsem.length;i++)
{
document.Form1.subjects.options[i] =new Option(sixthsem[i-1]);
// loading options from array into subjects menu for sixth sem
}
}
}
}
</script>
</head>
<body onload="document.Form1.Semester.selectedIndex=0">
<form name="Form1">
<select name="Semester" onchange="Getsubjects(this)"> // First
menu as semester
<option value="0">Semester</option>
<option value="1">Fifth</option>
<option value="2">Sixth</option>
</select>
<select name="subjects">
// Second menu as subjects
<option value="0">Subjects</option>
</select>
<br>
<p>
<input type="submit" value="Submit" />
<input type="reset" />
</p>
</form>
</body>
</html>
Write JavaScript code to create a banner with five images.
Ans:
<html>
<head>
<title>Banner Ads</title>
<script type="text/javascript">
Banners = new Array('s1.jpg','s2.png','s3.png','s4.png',’s5.png’);
CurrentBanner = 0; //index variable
function DisplayBanners()
{
if (document.images)
// Declare image array
{
CurrentBanner++;
if (CurrentBanner == Banners.length)
{
CurrentBanner = 0
}
document.RotateBanner.src= Banners[CurrentBanner]
setTimeout("DisplayBanners()",5000)
}
}
</script>
</head>
<body onload="DisplayBanners()">
<center>
// set timer
// call to function
<img src="s1.jpg" width="400" height="275" name="RotateBanner" >
</center>
</body>
</html>
Write a JavaScript code to create a slideshow with five images
Ans:
<html>
<head>
<title>Slideshow</title>
<script type="text/javascript">
var Pictures = new Array("image1.jpg", "image2.jpg", "image3.jpg",
"image4.jpg", "image5.jpg");
var CurrentPicture = 0;
function RunSlideShow(i) {
if (document.images) {
if (CurrentPicture >= Pictures.length) {
CurrentPicture = 0;
}
if (CurrentPicture < 0) {
CurrentPicture = Pictures.length - 1;
}
document.PictureDisplay.src = Pictures[CurrentPicture];
CurrentPicture = CurrentPicture + i;
}
}
</script>
</head>
<body>
<p align="center">
<img src="image1.jpg" name="PictureDisplay" width="400" height="275" />
</p>
<center>
<table border="0">
<tr>
<td align="center">
<input type="button" value="Forward" onclick="RunSlideShow(1)">
<input type="button" value="Back" onclick="RunSlideShow(-1)">
</td>
</tr>
</table>
</center>
</body>
</html>
write a JavaScript code to change the message in status bar with text
rollover.
<html>
<head>
<script type="text/javascript">
window.status="You are in home page";
function displaystatus(no)
{
if(no==1)
{
window.status="You are in CSS";
}
if(no==2)
{
window.status="You are in AJP";
}
if(no==3)
{
window.status="You are in OSY";
}
}
</script>
</head>
<body>
Hi welcome
<a onmouseover="displaystatus(1)">CSS</a>
<a onmouseover="displaystatus(2)">AJP</a>
<a onmouseover="displaystatus(3)">OSY</a>
</body>
</html>