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

Abc

The document contains 28 code snippets demonstrating various JavaScript concepts like printing messages, arithmetic operations, functions, arrays, events, and regular expressions. Some key concepts covered include: 1. Printing messages to the screen and performing basic math operations like addition, subtraction, etc. 2. Accepting user input using prompts and alerts. 3. Defining and calling functions. 4. Working with arrays - accessing elements, changing sizes, sorting. 5. Using events like onclick, onmouseover, onkeydown to trigger actions. 6. Regular expressions for pattern matching.

Uploaded by

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

Abc

The document contains 28 code snippets demonstrating various JavaScript concepts like printing messages, arithmetic operations, functions, arrays, events, and regular expressions. Some key concepts covered include: 1. Printing messages to the screen and performing basic math operations like addition, subtraction, etc. 2. Accepting user input using prompts and alerts. 3. Defining and calling functions. 4. Working with arrays - accessing elements, changing sizes, sorting. 5. Using events like onclick, onmouseover, onkeydown to trigger actions. 6. Regular expressions for pattern matching.

Uploaded by

xapalek559
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

1.

Message printing

<html>

<head>

<title>Message</title>

</head>

<body>

<script>

document.write("Welcome to JavaScript");

</script>

</body>

</html>

2.Arithmetic

<html>

<head>

<title>Arithmetic operation</title>

</head>

<body>

<script>

var a=20;

var b=30;

var res;

res=a+b;

document.write("Addition="+res+"<br>");

res=a-b;

document.write("Substraction="+res+"<br>");

res=a*b;
{

document.write("Multiplication="+res+"<br>");

res=a/b;

document.write("Division="+res+"<br>");

res=a%b;

document.write("Remainder="+res+"<br>");

</script>

</body>

</html>

3.addition input from user

<html>

<head>

<title>input from user addition</title>

</head>

<body>

<script>

var a=Number(prompt("Enter 1st Number"));

var b=Number(prompt("Enter 2nd Number"));

res=a+b;

document.write("Addition="+res+"<br>");

</script>

</body>

</html>
4.largest number

<html>

<head>

<title>Largest number</title>

</head>

<body>

<script>

var a=20;

var b=15;

if(a>b)

document.write("Largest number is="+a);

else

document.write("Largest number is="+b);

</script>

</body>

</html>

5.gratest among 3

<html>

<head>

<title>greatest among 3</title>

</head>

<body>

<script>

var a=20;

var b=15;

var c=10;
if(a>b && a>c)

document.write("Greatest number is="+a);

elseif(b>a && b>c)

document.write("Greatest number is="+b);

else

document.write("Greatest number is="+c);

</script>

</body>

</html>

6.Switch case

<html>

<head>

<title>Switch case</title>

</head>

<body>

<script>

var ch=Number(prompt("Enter number from 1 to 7"));

switch(ch)

case 1:

document.write("Monday");

break;
case 2:

document.write("Tuesday");

break;

case 3:

document.write("Wednesday");

break;

case 4:

document.write("Thursday");

break;

case 5:

document.write("Friday");

break;

case 6:

document.write("Saturday");

break;

case 7:

document.write("Sunday");

break;

default:

document.write("Invalid number");

</script>

</body>

</html>
7.Array

<html>

<head>

<title>Array</title>

</head>

<body>

<script>

var item=new Array(5);

item[0]="Apple";

item[1]="Banana";

item[2]="Mango";

item[3]="Kiwi";

item[4]="Orange";

document.write("item is="+item[0]+"<br>");

document.write("item is="+item[1]+"<br>");

document.write("item is="+item[2]+"<br>");

document.write("item is="+item[3]+"<br>");

document.write("item is="+item[4]+"<br>");

</script>

</body>

</html>

8.Length of array

<html>

<head>

<title>Length of array</title>

</head>

<body>
<script>

var l1=new Array();

document.write("Length of Array="+l1.length+"<br>");

var l2=new Array(4);

document.write("Length of Array="+l2.length+"<br>");

var l3=new Array(1,2,3,4,5);

document.write("Length of Array="+l3.length+"<br>");

</script>

</body>

</html>

9.Sorting

<html>

<head>

<title>Sorting</title>

</head>

<body>

<script>

var a=[12,23,44,15,54,32];

document.write("Before sorting"+a+"<br>");

a.sort();

document.write("Sorted Array"+a);

</script>

</body>

</html>
10.changing element of an array

<html>

<head>

<title>Changing elements of an array</title>

</head>

<body>

<script>

var a=["Abc","Lmn","Opq","Rst","Uvw"];

a.push("xyz");

document.write("array after push method="+a+"<br>");

a.pop();

document.write("array after pop method="+a+"<br>");

a.shift();

document.write("array after pop method="+a+"<br>");

a.reverse();

document.write("array after pop method="+a+"<br>");

</script>

</body>

</html>

11.addition using function

<html>

<head>

<title>function</title>

<script>

function add(a,b)

{
document.write("Addition is"+(a+b));

</script>

</head>

<body>

<script>

add(4,8);

</script>

</body>

</html>

12.return from function

<html>

<head>

<title>return value from function</title>

<script>

function add(a,b)

var add=a+b;

return add;

c=add(5,6);

document.write("Addition="+c);

</script>

</head>

<body>

<script>

add();

</script>

</body>

</html>
13.calling from html

<html>

<head>

<title>Calling fun from html</title>

<script>

funtion open()

alert("Welcome");

function colse()

alert("Thank you");

</script>

</head>

<body onload="open()", onunload="close()">

</body>

</html>

14.convert

<html>

<head>

<title>Convert</title>

</head>

<body>

<script>

<p>By integer</p>

var a=12.3;

document.write(parseInt(a));

</script>

</body>

</html>
15.uppercase lowercase

<html>

<head>

<title>Upper and Lower</title>

</head>

<body>

<script>

var str="ABC";

document.write(str.toLowercase());

var str1="abc";

document.write(str1.toUppercase());

</script>

</body>

</html>

16.retrive positon

<html>

<head>

<title>Retrive the position of String</title>

<body>

<script>

var str="SIT POLYTECHNIC";

document.write("character is ="+str1.CharAt(5));

</script>

</body>

</html>
17retrive index

<html>

<head>

<title>Retrive the Index of String</title>

<body>

<script>

var str="SIT POLYTECHNIC";

document.write("character is ="+str1.indexOf('I'));

</script>

</body>

</html>

18 student registration

<html>

<head>

<title>Student Registration</title>

</head>

<body>

<script>

<form>

Name=<input type="text" name="t1" size="50" maxlength="22">

Address=<textarea name="t2" cols="10" rows="10" wrap="hard"></textarea>

<P>Gender</p>

<input type="radio" name="Female" value="Female">

<input type="radio" name="Male" value="Male">

<br><br>

<h3>Subjects</h3>

<input type="checkbox" name="c1" value="AJP">

<input type="checkbox" name="c2" value="CSS">

<input type="checkbox" name="c3" value="OSY">

<input type="checkbox" name="c4" value="EST">


<br><br>

Submit<input type="button" name="b" value="Submit">

</form>

</script>

</html>

19.online shopping

<html>

<head>

<title>Online Shoping</title>

</head>

<body>

<script>

<form>

Customer name=<input type="text" name="t1">

Address=<textarea name="t2" cols="10" rows="10" wrap="hard"></textarea>

Gmail=<input type="text" name="t2">

<input type="button" name="b1" value="Submit">

</form>

</script>

</body>

</html>

20.onclick

<html>

<head>

<title>Onclick</title>

<script>

function fy()

alert("Welcome to FYIT");

}
function sy()

alert("Welcome to SYIT");

function ty()

alert("Welcome to TYIT");

</script>

</head>

<body>

<form>

<input type="button" name="b1" value="FY" onclick="fy()">FYIT

<input type="button" name="b2" value="SY" onclick="sy()">SYIT

<input type="button" name="b3" value="TY" onclick="ty()">TYIT

</form>

</body>

</html>

21.ondblclick

<html>

<head>

<title>Ondblclick</title>

<script>

function msg()

alert("Welcome to CSS");

</script>

</head>

<body>
<form>

<input type="button" name="b1" value="click" onclick="msg()">

</form>

</body>

</html>

22.onmouseover and out

<html>

<head>

<title>Onmouseover and Onmouseout</title>

<script>

function mover()

document.Myform.b1.value="Mouseover";

function mout()

document.Myform.b1.value="Mouseout";

</script>

</head>

<body>

<form name="Myform">

<input type="button" name="b1" value="click" onmouseover="mover()" onmouseout="mout()">

</form>

</body>

</html>
23.keyevents

<html>

<head>

<title>Keyevents</title>

<script>

function down()

alert("Key is pressed");

function up()

alert("key is released");

function press()

alert("key is pressed ");

</script>

</head>

<body>

<form>

<input type="button" name="b1" value="down" onkeydown="down()">

<input type="button" name="b2" value="up" onkeyup="up()">

<input type="button" name="b3" value="press" onpress="press()">

</form>

</body>

</html>
24.Intrinsic function

<html>

<head>

<title>Intrinsic function</title>

</head>

<body>

<form name="Myform">

Name=<input type="text" name="t1">

Roll No=<input type="text" name="t2">

<img src="g8.png" height="100" width="100" onclick="document.forms.Myform.submit()">

<img src="g9.png" height="100" width="100" onclick="document.forms.Myform.reset()">

</form>

</body>

</html>

25.Disable

<html>

<head>

<title>Disable</title>

<script>

function enable()

document.forms.Myform.name.disabled=false

function disable()

document.forms.Myform.name.disabled=true

</script>

</head>
<body>

<form name="Myform">

User Name<input type="text" name="name">

<input type="button" value="enable" onclick="enable()">

<input type="button" value="disable" onclick="disable()">

</form>

</body>

</html>

26.Readonly

<html>

<head>

<title>ReadOnly</title>

<script>

function readonly()

document.forms.Myform.name.readOnly=true

</script>

</head>

<body>

<form name="Myform">

User Name<input type="text" name="name">

<input type="button" value="ReadOnly" onclick="readonly()">

</form>

</body>

</html>
27.changing window

<html>

<head>

<title>working with Window</title>

<script>

function openw()

window.open("","","width=300 height=400");

this.focus();

window.open("","","top=100 left=50 width=300 height=400");

function openmul()

for(var i=0;i<3;i++)

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

function changecontent()

document.write("<h3>After clicking the button</h3>");

function close()

a.close();

</script>

</head>

<body>

<form>

<input type="button" value="open" onclick="openw()">


<input type="button" value="open" onclick="openmul()">

<input type="button" value="chnage" onclick="changecontent()">

<input type="button" value="close" onclick="close()">

</form>

</body>

</html>

28.metacharacter

<html>

<head>

<title>Metacharacter</title>

<script>

var a=/\d/

function test()

var str=textfield.value;

if(a.test(str))

alert("Valid")

else

alert("Invalid")

</script>

</head>

<body>

Enter name<input type="text" id="textfield">

<input type="button" value="click" onclick="test()">


</body>

</html>

29.Banner

<html>

<head>

<title>Banner</title>

<script>

Mybanner=new Array('g1.png','g2.png','g4.png','g5.png');

var banner=0;

function display()

document.getElementById("BannerChange").src=Mybanner(banner);

if(banner<(Mybanner.length-1))

banner++;

else

banner=0;

</script>

</head>

<body>

<center>

<img src="g1.png" width="300" height="300" id="BannerChange">

</center>

</body>

</html>

30.Banner link

<html>

<head>

<title>Bannerlink</title>
<script>

Mybanner=new Array('g1.png','g2.png','g4.png',);

Mybannerlink=new Array('https://www.google.com','https://www.gmail.com','https://yahoo.com');

var banner=0;

function displayLink()

document.location.href=Mybannerlink(banner);

function display()

document.getElementById("BannerChange").src=Mybanner(banner);

if(banner<(Mybanner.length-1));

banner++;

else

banner=0;

setInterval("display()" 2000);

</script>

</head>

<body onload="display()">

<center>

<a href="displaylink()">

<img src="g1.png" width="300" id="BannerChange">

</center>

</body>

</html>
31.slidshow

<html>

<head>

<title>slideshow</title>

<script>

slide=new Array('g1.png','g2.png','g4.png','g5.png');

var i=0;

function display(SlideNumber)

i=i+SlideNumber;

if(i>slide.length-1)

i=0;

if(i<0)

i=slide.length-1;

document.SlideID.src=slide[i];

</script>

</head>

<body>

<img src="g1.png" name="SlideID" width="200" height="200"><br>

<input type="button" value="Back" onclick="display(-1)">

<input type="button" value="Forword" onclick="display(1)">

</body>

</html>

You might also like