Advanced Javascript (Practical)

 

3. Advanced JavaScript

Switch Statement -

·         Write JavaScript program to print marks for grade using Switch statement.

Ans :-

Grade.html

<html>

<head>

<title>JavaScript Switch</title>

</head>

<body>

<h1>JavaScript : switch statement</h1>

<script language="javascript">

var grade=prompt("Enter the grade : ","");

switch (grade)

{

case 'A+':

  alert("Marks >= 90");

  break;

case 'A':

  alert("Marks [ >= 80 and <90 ]");

  break;

case 'B+':

  alert("Marks [ >= 70 and <80 ]");

  break;

case 'B':

  alert("Marks [ >= 60 and <70 ]");

  break;

case 'C':

  alert("Marks < 60");

  break;

default:

  alert("Wrong grade.........");

}

</script>

</body>

</html>

Looping Statements –

 

·        Write a JavaScript program to print table using for loop as given format 2 x 1 = 2.

Ans :-

Table.html

<!DOCTYPE html>

<html>

<head>

<title>Table of Given</title>

<script type="text/javascript">

function tab()

{

var i,num;

num=document.f1.t1.value;

for(i=1;i<=10;i++)

{

document.write(num+" x "+i+" = "+num*i+"<br>");

}

}

</script>

</head>

<body>

<form name="f1">

Enter Number : <input type="text" name="t1"> <br>

<input type="submit" value="Print Table" onclick="tab()">

</form>

</body>

</html>

·        Write a JavaScript program to find given number is prime or not using while loop.

Ans :-

Prime.html

<html> 

<head>

<title>Prime</title> 

<script language="JavaScript"> 

var a=prompt("enter a value"); 

var c=0; 

for(var j=2;j<a;j++) 

{ 

if(a%j==0) 

{ 

c=j; 

} 

} 

if(c==0) 

{ 

alert(a+" is prime number"); 

} 

else 

{ 

alert(a+" is not a prime number"); 

} 

</script> 

</head> 

<body></body> 

</html> 

Window Object –

·        Write JavaScript program to demonstrate properties of window object.

Ans –

Winpro.html

<html>

<head>

<title>Windows Property</title>

<script language="javascript">

function nam()

{

var myWindow=window.open("", "MsgWindow", "width = 200, height = 100");

myWindow.document.write("<p>This window's name is: " + myWindow.name + "</p>");

}

</script>

</head>

<body>

<form>

<p>Name Property</p>

<input type="Button" value="Name" onClick="nam()">

</form>

<script language=javascript>

document.write("This is the location of current window : "+location.href+"<br>");

document.write("Window is closed or not : "+window.closed+"<br>");

</script>

</body>

</html>

·        JavaScript program to demonstrate use of window object methods.

Ans –

Winmethod.html

<html>

<head>

<script language="javascript">

function winalert()

{

window.alert("This is alert Method");

}

function winopen()

{

var myWindow = window.open("", "", "width=200, height=100");

myWindow.document.write("<p>A new window!</p>");

}

function winblur()

{

myWindow.blur();

}

function winconfirm()

{

window.confirm("This is confirm Method");

}

function winprompt()

{

var thename=window.prompt("Please Enter your Name","");

window.alert("Hello! "+thename);

}

function winclose()

{

window.close();

}

</script>

</head>

<body>

<form name="form1">

<p>Alert Method</p>

<input type=button value=Alert onclick="winalert()" >

<p>Open Method</p>

<input type=button value=Open onclick="winopen()" >

<p>Blur Method</p>

<input type=button value=Blur onclick="winblur()" >

<p>Confirm Method</p>

<input type=button value=Confirm onclick="winconfirm()" >

<p>Prompt Method</p>

<input type=button value=Prompt onclick="winprompt()" >

<p>Close Method</p>

<input type=button value=Close onclick="winclose()" >

</form>

</body>

</html>

·        JavaScript program to demonstrate setTimeout() method.

Ans –

timeeve.html

<!DOCTYPE html>

<html>

<head>

<title>Time Event</title>

<script language="JavaScript">

function t_msg()

{

var t1=setTimeout("Msg()",3000);

}

function Msg()

{

alert("Hello every one.....! I came after 3 seconds");

}

</script>

</head>

<body>

<form>

<input type="button" value="Click Me" onclick="t_msg()" />

</form>

</body>

</html>

Input Element Events –

·        JavaScript program to demonstrate input element events.

Ans –

Formobj.html

<html>

<head>

<title>Form Event</title>

<script language="javascript">

function concatenate()

{

document.form1.fulladdress.value=(document.form1.plot.value+" "+document.form1.colony.value+" "+document.form1.taluka.value+" "+document.form1.dist.value);

}

</script>

</head>

<body bgcolor="orange">

<form name="form1" onReset="confirm('You want to Clear Form')" onSubmit="alert('Thank you ' + form1.firstname.value +'!')">

<pre>

<p>Please enter the following information</p>

Student Name : <input type="text" name=firstname size=20 onFocus='alert("You focused in the textbox!!")'> <br><br>

Surname      : <input type="text" size=20 onSelect="alert('You select the text')"><br>

Gender :       Male <input type=radio name=r1>  Female <input type=radio name=r1><br>

Address :

Plot No.     : <input type="text" size=20 name=plot onBlur="concatenate()"> Press Tab Key<br>

Colony       : <input type="text" size=20 name=colony onBlur="concatenate()"> Press Tab Key<br>

Taluka       : <input type="text" size=20 name=taluka onBlur="concatenate()"> Press Tab Key<br>

Dist         : <input type="text" size=20 name=dist onBlur="concatenate()"> Press Tab Key<br>

State        : <select name=cmbstate onchange="alert('You change the Text')">

<option value=0 selected>Maharashtra</option>

<option value=1>MadhyaPradesh</option>

<option value=2>Gujrath</option>

<option value=3>Rajsthan</option>

</select><br>

Correspondance Address :

               <textarea rows=5 cols=30 name=fulladdress></textarea><br>

<hr>

<input type=submit value=Submit> <input type=reset value=Reset>

</pre>

</form>

</body>

</html>

·        JavaScript program to demonstrate onload and onunload events.

String Object –

·        JavaScript program to demonstrate properties and methods of string object.

Ans –

Stringobj.html

<html>

<head>

<title>String Object</title>

</head>

<body>

<h3>Property of string object</h3>

<script language="javascript">

var str="Hi, Welcome to javascript";

document.write("<h3>The entered string is : "+str+"</h3>")

len=str.length;

document.write("Length of string is : "+len+"<br>")

document.write("<h3> Methods of string object </h3>")

document.write("charAt() Return the character at a specified position : "+str.charAt(22)+"<br>")

document.write("indexOf() Return the first occurance of string/char : "+str.indexOf("a")+"<br>")

document.write("lastIndexOf() Return the last occurance of string/char : "+str.lastIndexOf("a")+"<br>")

document.write("substr() Return the specified character set : "+str.substr(8,15)+"<br>")

document.write("substring() Return the specified character set : "+str.substring(8,16)+"<br>")

document.write("toLowerCase() Return string into lower case : "+str.toLowerCase()+"<br>")

document.write("toUpperCase() Return string into Upper case : "+str.toUpperCase()+"<br>")

</script>

</body>

</html>

Math Object –

·        JavaScript program to demonstrate methods of math object.

Ans –

Math.html

<!DOCTYPE html>

<html>

<head>

<title>Math Object</title>

</head>

<body>

<script language="javascript">

document.write("<h1> Math object Methods</h1>");

document.write("abs(x) Method : "+Math.abs(-9.5)+"<br>");

document.write("cbrt(x) Method : "+Math.cbrt(125)+"<br>");

document.write("Ceil(x) Method : "+Math.ceil(4.4)+"<br>");

document.write("floor(x) Method : "+Math.floor(4.7)+"<br>");

document.write("Max(x,y,z) Method : "+Math.max(4,5,6)+"<br>");

document.write("Min(x,y,z) Method : "+Math.min(4,5,6)+"<br>");

document.write("pow(x) Method : "+Math.pow(4,3)+"<br>");

document.write("Random(x) Method : "+Math.random()+"<br>");

document.write("Sqrt(x) Method : "+Math.sqrt(25)+"<br>");

</script>

</body>

</html>

Date Object –

·        Write javascript program to demonstrate methods of date object.

Ans –

Date.html

Number Object –

·        JavaScript program to demonstrate properties and methods of number object.

Ans –

Number.html

<!DOCTYPE html>

<html>

<head>

<title>Number Object</title>

</head>

<body>

<script type="text/javascript">

var a,b;

a=10.20;

b="20.60";

document.write("Minimum value : "+Number.MIN_VALUE+"<br>");

document.write("Maximum value : "+Number.MAX_VALUE+"<br>");

document.write("isInteger Method : "+Number.isInteger(a)+"<br>");

document.write("parseFloat convert string in floating point number : "+parseFloat(b)+"<br>");

document.write("parseInt convert string in whole number : "+parseInt(b)+"<br>");

document.write("isFixed Method : "+isFixed(a)+"<br>");

</script>

</body>

</html>

Array Object –

·        Javascript program to create an array.

Ans –

Array.html

<html>

<head>

<title> Array Object </title>

<h2> An Array of Object </h2>

<script language="javascript">

var fruit= new Array(6)  // create array object

//var fruit=["Apple","Mango","Pineapple"]

fruit[0] = "Apple"

fruit[1] = "Mango"

fruit[2] = "Banana"

fruit[3] = "Pineapple"

fruit[4] = "Orange"

fruit[5] = "Grapes"

</script>

</head>

<body bgcolor="lime">

<script language="javascript">

//document.write("<h3>");

for(var i in fruit)

{

document.write("Fruit[" + i + "]" + fruit[i] + "<br>");

}

</script>

</body>

</html>

·        Javascript program to demonstrate properties and methods of array object.

Ans –

Arraymethod.html

<!DOCTYPE html>

<html>

<head>

<title>Array Methods</title>

</head>

<body>

<script type="text/javascript">

var flowers=["Lotus","Marigold","Rose","Jasmine","Hibiscus","Dahlia"];

document.write("<h3> Array emelents are :- </h3>"+flowers+"<br>");

document.write("Copy elements at end to the beginning :- "+flowers.copyWithin(0,3)+"<br>");

var flowers=["Lotus","Marigold","Rose","Jasmine","Hibiscus","Dahlia"];

document.write("Copy elements at beginning to the end :- "+flowers.copyWithin(3,0)+"<br>");

var flowers=["Lotus","Marigold","Rose","Jasmine","Hibiscus","Dahlia"];

document.write("Copy first 3 elements at the middle  :- "+flowers.copyWithin(2,0,3)+"<br>");

document.write("Adding an element to an array :- "+flowers.push("Mogara")+"<br>");

document.write("Printing the position of element :- "+flowers.indexOf("Rose")+"<br>");

var result=Array.isArray(flowers);

document.write("flower is an array object :- "+result+"<br>");

document.write("Removing the last element from an array :- "+flowers.pop()+"<br>");

document.write("Array emelents are :- "+flowers+"<br>");

document.write("Reversing an array element :- "+flowers.reverse()+"<br>");

document.write("Sorting the array elements :- "+flowers.sort());

</script>

</body>

</html>

0 comments:

Post a Comment