SlideShare a Scribd company logo
1 of 18
1. Write a program to display“HelloWorld!”.
<script type="text/javascript">
document.write("HelloWorld!");
</script>
Output
HelloWorld!
2. Write a program to findthe sumof any twonumbers
<script type="text/javascript">
var a=5, b=6, c=0;
c=a+b;
document.write("Firstvalue is="+a+"<br/>");
document.write("Secondvalueis="+b+"<br/>");
document.write("Total value is="+c+"<br/>");
</script>
Output
Firstvalue is=5
Secondvalue is=6
Total value is=11
3. Write a program to findthe total and percentage of anyfive subjects.
<script type="text/javascript">
var hindi=50,english=70,math=60, science=64,sst=65;
var total=0,percentage=0;
total=hindi+english+math+science+sst;
percentage=total*100/500;
document.write("Marksof englishis=",english,"<br/>");
document.write("Marksof mathis=",math,"<br/>");
document.write("Marksof science is=",science,"<br/>");
document.write("Marksof hindi is=",hindi,"<br/>");
document.write("Marksof sstis=",sst,"<br/>");
document.write("Total marksis=",total,"<br/>");
document.write("Percentage is=",percentage,"%<br/>");
</script>
Output
Marks of englishis=70
Marks of math is=60
Marks of science is=64
Marks of hindi is=50
Marks of sst is=65
Total marksis=309
Percentage is=61.8%
4. Write a programto findthe Area,
<script type="text/javascript">
var lenght=60,width=40,area=0;
area=lenght*width;
document.write("Lengthis=",length,"<br/>");
document.write("Widthis=",width,"<br/>");
document.write("Areais=",area,"<br/>");
</script>
Output
Lenghtis=60
Widthis=40
Areais=2400
5. Write a program to findthe (a+b)2
.
<script type="text/javascript">
var a=2, b=2, c=0;
c=a*a+b*b+2*a*b;
document.write("Valueof ais=",a,"<br/>");
document.write("Valueof bis=",b,"<br/>");
document.write("(a+b)<sup>2</sup>is=",c,"<br/>");
</script>
Output
Value of a is=2
Value of b is=2
(a+b)2
is=16
6. Write a program to findthe simple Interest.
<script type="text/javascript">
var amount=5000,rate=3,time=5,si=0,total=0;
si=amount*rate*time/100;
total=amount+si;
document.write("Amountis=",amount,"<br/>");
document.write("Rate is=",rate,"<br/>");
document.write("Time is=",time,"<br/>");
document.write("Simple Interestis=",si,"<br/>");
document.write("Total amountis=",total,"<br/>");
</script>
Output
Amountis=5000
Rate is=3
Time is=5
Simple Interestis=750
Total amountis=5750
7. Write a program to findthe givennumberisEvenorOdd.
<script type="text/javascript">
var number=8;
document.write("Givennumberis=",number,"<br/>");
if(number%2==0)
{
document.write("Even");
}
else
{
document.write("Odd");
}
</script>
Output
Givennumberis=8
Even
8. Write a program to findthe givennumberisEvenorOdd and Zero.
<script type="text/javascript">
var number=8;
document.write("Givennumberis=",number,"<br/>");
if(number>0)
{
document.write("+ve");
}
else
if(number<0)
{
document.write("-ve");
}
else
{
document.write("zero");
}
</script>
Output
Givennumberis=8
+ve
9. Write a program to findthe largestbetweentwonumbers.
<script type="text/javascript">
var a=9,b=10;
document.write("Valueof ais=",a,"<br/>");
document.write("Valueof bis=",b,"<br/>");
if(a>b)
{
document.write("A isgreaternumber");
}
else
if(b>a)
{
document.write("Bisgreaternumber");
}
else
{
document.write("Bothare equal");
}
</script>
Output
Value of a is=9
Value of b is=10
B is greaternumber
10. Write a program to findthe largestbetweenthreenumbers.
<script type="text/javascript">
var a=9,b=6,c=2;
document.write("Valueof ais=",a,"<br/>");
document.write("Valueof bis=",b,"<br/>");
document.write("Valueof cis=",c,"<br/>");
if(a>b&& a>c)
{
document.write("A isgreaternumber");
}
else
if(b>a&& b>c)
{
document.write("Bisgreaternumber");
}
else
if(c>a&& c>b)
{
document.write("Cisgreaternumber");
}
else
{
document.write("Equal");
}
</script>
Output
Value of a is=9
Value of b is=6
Value of c is=2
A isgreaternumber
11. Write a program to findthe ticketcostaccording to age:if age<=1) ticketcost is50 Rupees ,if
age>15 and age <=30 ticketcostis 100 Rupees,if age >30 ticketcost is150 Rupees.
<script type="text/javascript">
var age=14, tc=0;
document.write("Ageis=",age,"<br/>");
if(age<=15)
{
tc=50;
}
else
if(age>15&&age<=30)
{
tc=100;
}
else
if(age>30)
{
tc=150;
}
document.write("Ticketcostis=",tc,"<br/>");
</script>
Output
Ticketcost is=50
12. Write a program to findthe ProfitandLose according to the Costprice and Sale price.
<script type="text/javascript">
var cp=80,sp=80;
document.write("Costprice is=",cp,"<br/>");
document.write("Sale price is=",sp,"<br/>");
if(sp>cp)
{
amount=sp-cp;
document.write("Profitof rupees",amount,"/-<br/>");
}
else
if(cp>sp)
{
amount=cp-sp;
document.write("Lossof rupees",amount,"/-<br/>");
}
else
{
document.write("Noprofitnoloss");
}
</script>
Output
Cost price is=80
Sale price is=80
No profitnoloss
13. Write a program to findthe givenseries usingwhile loop ->12 3 4 5.
<script type="text/javascript">
var i=0;
while(i<5)
{
i=i+1;
document.write(i,"<br/>")
}
</script>
Output
14. Write a program to findthe Evenseries usingwhileloop ->2 4 6 8 10.
<script type="text/javascript">
var i=0;
while(i<10)
{
i=i+2;
document.write(i,"<br/>")
}
</script>
15. Write a program to findthe Oddseries usingwhile loop ->1,3,5,7,9,
<script type="text/javascript">
var i=0;
while(i<10)
{
Output
2
4
6
8
10
Output
1
2
3
4
5
Output
1
3
5
7
9
i=i+2;
document.write(i-1,"<br/>")
}
</script>
16. Write a program to findthe series 1/1, 1/2, 1/3, 1/4, 1/5.
<script type="text/javascript">
var i=0;
while(i<5)
{
i=i+1;
document.write("1/",i,"<br/>")
}
</script>
17. Write a program to findthe givenseriesusingforloop12 3 4 5
<script type="text/javascript">
var i=0;
for(i=1;i<=5;i++)
{
document.write(i,"<br/>");
}
</script>
18. Write a program to findthe givenseriesusingforloop24 6 8 10
<script type="text/javascript">
var i=0;
for(i=2;i<=10;i=i+2)
{
document.write(i,"<br/>");
}
</script>
19. Write a program to findthe givenseriesusingfor loop13 5 7 9
<script type="text/javascript">
var i=0;
for(i=1;i<=10;i=i+2)
{
document.write(i,"<br/>");
}
</script>
20. Write a program to findthe givenseriesusingforloop1/1,1/2, 1/3, 1/4, 1/5
<script type="text/javascript">
var i=0;
for(i=1;i<=5;i=i+1)
{
Output
1/1
1/2
1/3
1/4
1/5
Output
1
2
3
4
5
Output
2
4
6
8
10
Output
1
3
5
7
9
Output
1/1
1/2
1/3
1/4
1/5
document.write("1/",i,"<br/>");
}
</script>
21. Write a program to findthe table of any givennumber.
<script type="text/javascript">
var i=0,n=5,t=0;
for(i=1;i<=10;i=i+1)
{
t=i*n;
document.write(n,"*",i," = ",t,"<br/>");
}
</script>
Output
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
22. Write a program to findthe array of any5 student’smarks.
<script type="text/javascript">
var marks=newArray(45,76,78,98,56),i=0;
for(i=0;i<5;i=i+1)
{
document.write(marks[i],"<br/>");
}
</script>
23. Write a program to findthe array of any5 student’smarksandfindthere sumand average.
<script type="text/javascript">
var marks=newArray(45,76,78,98,56);
var i=0,sum=0,avg=0;
for(i=0;i<5;i=i+1)
{
document.write(marks[i],"<br/>");
sum=sum+marks[i];
}
avg=sum/5;
document.write("Sumis=",sum,"<br/>");
document.write("Averageis=",avg,"<br/>");
</script>
Output
45
76
78
98
56
Sumis=353
Average is=70.6
Output
45
76
78
98
56
24. Write a program to findthe array of any5 student’smarksandfindthere minimumand
maximumvalue.
<script type="text/javascript">
var marks=new Array(45,76,78,98,56);
var i=0,min=0,max=0;
min=marks[1];
max=marks[1];
for(i=0;i<5;i=i+1)
{
document.write(marks[i],"<br/>");
if(min>marks[i])
{
min=marks[i];
}
if(max<marks[i])
{
max=marks[i];
}
}
document.write("Minimumis=",min,"<br/>");
document.write("Maximumis=",max,"<br/>");
</script>
25. Write a program to findthe array of any10 numbersandfindthe sumof evennumbers.
<script type="text/javascript">
var no=new Array(45,76,78,98,56,76,79,43,45,78),i=0,sum=0;
for(i=0;i<10;i=i+1)
{
document.write(no[i],"<br/>");
if(no[i]%2==0)
{
sum=sum+no[i];
}}
document.write("Total of evennumbersis=",sum,"<br/>");
</script>
Output
45
76
78
98
56
Minimumis=45
Maximumis=98
Output
45
76
78
98
56
76
79
43
45
78
Total of evennumbersis=462
26. Write a program to findthe array of any10 numbersandfindthe sumof evennumbers.
<script type="text/javascript">
var no=newArray(45,76,78,98,56,76,79,43,45,78),i=0,sum=0;
document.write("Withoutsort<br/>");
for(i=0;i<10;i=i+1)
{
document.write(no[i],"<br/>");
}
for(i=0;i<10;i=i+1)
{
for(j=i+1;j<10;j++)
{
if(no[i]>no[j])
{
a=no[i];
no[i]=no[j];
no[j]=a;
}}}
document.write("Aftersortinaccendingorder<br/>");
for(i=0;i<10;i=i+1)
{
document.write(no[i],"<br/>");
}
</script>
27. Write a program to findthe factorial number.
<script type="text/javascript">
var i=0,n=4,f=1;
for(i=1;i<=n;i++)
{
f=f*i
}
document.write("Factorial numberis=",f,"<br/>");
</script>
Output
Factorial numberis=24
28. Write a program to showto givenpattern
<script type="text/javascript">
var i=0,j=0;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
Output
Withoutsort
45
76
78
98
56
76
79
43
45
78
Aftersortin accendingorder
43
45
45
56
76
76
78
78
79
98
document.write(i,"");
}
document.write("<br/>");
}
</script>
29. Write a program to showto givenpattern
<script type="text/javascript">
var i=0,j=0;
for(i=8;i>=1;i--)
{
for(j=1;j<=i;j++)
{
document.write(i,"");
}
document.write("<br/>");
}
</script>
30. Write a program to showto givenpattern
<script type="text/javascript">
var i=0,j=0,n=0;
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
n=n+2;
document.write(n,"");
}
document.write("<br/>");
}
</script>
31. Write a program to displaythe factorial numberusingfunction“fact();”
<html>
<head>
<title>Factorial number</title>
<script type="text/javascript">
functionfact()
Output
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Output
8 8 8 8 8 8 8 8
7 7 7 7 7 7 7
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
Output
2
4 6
8 10 12
14 16 18 20
{
var i=0,f=1;
var n=parseInt(document.getElementById("n").value);
while(i<n)
{
i=i+1;
f=f*i;
}
alert("Factorial numberis="+f);
}
</script>
</head>
<body>
Enter anynumber: <inputtype="text"value="4"id="n">
<inputtype="button"value="Factorial"onclick="fact();">
</body>
</html>
Output
32. Write a program to show the febnocci series
<html>
<head>
<title>Febnocci series</title>
<script type="text/javascript">
var a,b,c,n=10;
document.write("Givennumberis=",n,"<br/>");
a=1;
b=1;
document.write(a,"<br/>");
document.write(b,"<br/>");
for(c=1;c<=n;c++)
{
c=a+b;
a=b;
b=c;
document.write(c,"<br/>");
}
</script>
</head>
<body>
</body>
</html>
The JavaScript Switch Statement
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var day=2;
switch(day) {
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
break;
case 7:
day="Sunday";
break;
default:
day="Wrongday";
break;
}
document.getElementById("demo").innerHTML
= "Todayis " + day;
</script>
<body>
</body>
</html>
In javascriptwrite aprogram using“switchcase”to accept two numbersfromuserand perform
addition,subtractionandproducttothe acceptednumber.
<!DOCTYPE html>
<html>
<body>
<script>
var a=0,b=0,ans=0,ch=0;
a=Number(prompt("Enterfirstvalue="));
b=Number(prompt("Entersecondvalue="));
ch=Number(prompt("Press1.(addition) 2.
(subtraction),3.(product)="));
switch(ch)
{
case 1:
ans=a+b;
break;
case 2:
Output
Givennumberis=10
1
1
2
3
5
8
13
ans=a-b;
break;
case 3:
ans=a*b;
break;
default:
alert("Wrongchoice");
break;
}
document.write("Firstvalue is=",a,"<br/>");
document.write("Secondvalueis=",b,"<br/>");
document.write("Resultis= ",ans,"<br/>");
</script>
<body>
</body>
</html>
Write a program inhtml usingjava scriptto accept anytwo numbersfromuseran performfollowing
operationinthe same program.
 Sumof the numbers
 Difference betweenthe numbers(subtraction)
 Productsof the numbers
<!DOCTYPE html>
<html>
<body>
<script>
var a=0,b=0,sum=0,sub=0,pro=0;
a=Number(prompt("Enterfirstvalue="));
b=Number(prompt("Entersecondvalue="));
sum=a+b;
sub=a-b;
pro=a*b;
document.write("Firstvalue is=",a,"<br/>");
document.write("Secondvalue
is=",b,"<br/><br/>");
document.write("Sumis=",sum,"<br/>");
document.write("Subtractionis=",sub,"<br/>");
document.write("Productis=",pro,"<br/>");
</script>
<body>
</body>
</html>
Write a program inhtml usingjavascriptto declare afunction‘fact()’calculate factorial of entered
numberusingthisfunction.
<!DOCTYPE html>
<html>
<body>
<script>
function fact()
{
var i=0,f=1;
var
n=parseInt(document.getElementById("n").valu
e);
var ans=document.getElementById("ans");
while(i<n)
{
i=i+1;
f=f*i;
}
ans.value=f;
}
</script>
<body>
Enter anynumber: <inputtype="text"value=""
id="n">Factorial numberis: <inputtype="text"
value=""id="ans"><inputtype="button"
value="Result"onclick="fact();">
</body>
</html>
Output
Break
The break statement breaks the loop and continues executing the code after the
loop
<script>
var i=0;
for (i = 1; i <=10; i++) {
if (i == 5)
{
break;
}
document.write(i,"<br/>");
}
</script>
Output
1
2
3
4
Data typesinjavascript
o Boolean
o Null
o Undefined
o Number
o String
Boolean
Boolean represents a logical entity and can have two values: true, and false.
var x = 5;
var y = 5;
var z = 6;
(x == y) // Returns true
(x == z) // Returns false
Null
The Null type has exactly one value: null.
<script>
var n;
if(n==null)
{
document.write('Not value found');
}
</script>
Undefined
In JavaScript, a variable without a value, has the value undefined. The typeof is
also undefined.
var car; // Value is undefined, type is undefined
Number
JavaScript has only one type of numbers.
Numbers can be written with, or without decimals:
var x1 = 34.00; // Written with decimals
var x2 = 34; // Written without decimals
String
o A string (or a text string) is a series of characters like "John Doe".
o Strings are written with quotes. You can use single or double quotes:
var carName = "Volvo XC60"; // Using double quotes
var carName = 'Volvo XC60'; // Using single quotes
There are 3 JavaScript methods that can be used to convert variables to numbers:
 The Number() method
 The parseInt() method
 The parseFloat() method
Method Description
Number() Returns a number, converted from its
argument.
parseFloat() Parses its argument and returns a floating
point number
parseInt() Parses its argument and returns an integer
Math.round()
Math.round(x) returns the value of x rounded to its nearest integer:
Math.round(4.7); // returns 5
Math.round(4.4); // returns 4
Math.pow()
Math.pow(x, y) returns the value of x to the power of y:
Math.pow(8, 2); // returns 64
Math.sqrt()
Math.sqrt(x) returns the square root of x:
Math.sqrt(64); // returns 8
Math.abs()
Math.abs(x) returns the absolute (positive) value of x:
Math.abs(-4.7); // returns 4.7
Math.ceil()
Math.ceil(x) returns the value of x rounded up to its nearest integer:
Math.ceil(4.4); // returns 5
Math.ceil()
Math.ceil(x) returns the value of x rounded up to its nearest integer:
Math.ceil(4.4); // returns 5
Math.min() and Math.max()
Math.min() and Math.max() can be used to find the lowest or highest value in a list
of arguments:
Math.min(0, 150, 30, 20, -8, -200); // returns -200
Math.max(0, 150, 30, 20, -8, -200); // returns 150
Math.random()
Math.random() returns a random number between 0 (inclusive), and 1 (exclusive):
Math.random(); // returns a random number
Using String search() With a Regular Expression
Use a regular expression to do a case-insensitive search for "w3schools" in a string:
var str = "Visit W3Schools";
var n = str.search(/w3schools/i);
6
Use String replace() With a Regular Expression
Use a case insensitive regular expression to replace Microsoft with W3Schools in a
string:
var str = "Visit Microsoft!";
var res = str.replace(/microsoft/i, "W3Schools");
Visit W3Schools!
var str = "Visit Microsoft!";
var res = str.replace("Microsoft", "W3Schools");
Modifier Description
i Perform case-insensitive matching
g Perform a global match (find all matches rather than stopping
after the first match)
m Perform multiline matching

More Related Content

Similar to Javascript notes

Bca sem 6 php practicals 1to12
Bca sem 6 php practicals 1to12Bca sem 6 php practicals 1to12
Bca sem 6 php practicals 1to12
Hitesh Patel
 
Java.script
Java.scriptJava.script
Java.script
g Nama
 

Similar to Javascript notes (20)

Legacy Code and Refactoring Workshop - Session 1 - October 2019
Legacy Code and Refactoring Workshop - Session 1 - October 2019Legacy Code and Refactoring Workshop - Session 1 - October 2019
Legacy Code and Refactoring Workshop - Session 1 - October 2019
 
Java script Examples by Som
Java script Examples by Som  Java script Examples by Som
Java script Examples by Som
 
Bca sem 6 php practicals 1to12
Bca sem 6 php practicals 1to12Bca sem 6 php practicals 1to12
Bca sem 6 php practicals 1to12
 
PHP Tutorial (funtion)
PHP Tutorial (funtion)PHP Tutorial (funtion)
PHP Tutorial (funtion)
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTs
 
Web lab programs
Web lab programsWeb lab programs
Web lab programs
 
07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards
 
Java script basic
Java script basicJava script basic
Java script basic
 
Webdesing lab part-b__java_script_
Webdesing lab part-b__java_script_Webdesing lab part-b__java_script_
Webdesing lab part-b__java_script_
 
I, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 OverlordsI, For One, Welcome Our New Perl6 Overlords
I, For One, Welcome Our New Perl6 Overlords
 
Hargun
HargunHargun
Hargun
 
Serverless Functions and Vue.js
Serverless Functions and Vue.jsServerless Functions and Vue.js
Serverless Functions and Vue.js
 
Refresher 2
Refresher 2Refresher 2
Refresher 2
 
Java.script
Java.scriptJava.script
Java.script
 
Html
HtmlHtml
Html
 
Groovy kind of test
Groovy kind of testGroovy kind of test
Groovy kind of test
 
Groovy kind of test
Groovy kind of testGroovy kind of test
Groovy kind of test
 
Tt subtemplates-caching
Tt subtemplates-cachingTt subtemplates-caching
Tt subtemplates-caching
 
FYBSC IT Web Programming Unit III Javascript
FYBSC IT Web Programming Unit III JavascriptFYBSC IT Web Programming Unit III Javascript
FYBSC IT Web Programming Unit III Javascript
 
Js mod1
Js mod1Js mod1
Js mod1
 

Recently uploaded

Formation of low mass protostars and their circumstellar disks
Formation of low mass protostars and their circumstellar disksFormation of low mass protostars and their circumstellar disks
Formation of low mass protostars and their circumstellar disks
Sérgio Sacani
 
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bAsymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Sérgio Sacani
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Sérgio Sacani
 

Recently uploaded (20)

Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)
 
Botany krishna series 2nd semester Only Mcq type questions
Botany krishna series 2nd semester Only Mcq type questionsBotany krishna series 2nd semester Only Mcq type questions
Botany krishna series 2nd semester Only Mcq type questions
 
CELL -Structural and Functional unit of life.pdf
CELL -Structural and Functional unit of life.pdfCELL -Structural and Functional unit of life.pdf
CELL -Structural and Functional unit of life.pdf
 
Formation of low mass protostars and their circumstellar disks
Formation of low mass protostars and their circumstellar disksFormation of low mass protostars and their circumstellar disks
Formation of low mass protostars and their circumstellar disks
 
Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )
 
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCRStunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
 
Botany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdfBotany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdf
 
Zoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdfZoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdf
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based Nanomaterials
 
Physiochemical properties of nanomaterials and its nanotoxicity.pptx
Physiochemical properties of nanomaterials and its nanotoxicity.pptxPhysiochemical properties of nanomaterials and its nanotoxicity.pptx
Physiochemical properties of nanomaterials and its nanotoxicity.pptx
 
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bAsymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
 
Biological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfBiological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdf
 
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
 
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
 
GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)
 
Isotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoIsotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on Io
 
Forensic Biology & Its biological significance.pdf
Forensic Biology & Its biological significance.pdfForensic Biology & Its biological significance.pdf
Forensic Biology & Its biological significance.pdf
 
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
 
Chemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdfChemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdf
 

Javascript notes

  • 1. 1. Write a program to display“HelloWorld!”. <script type="text/javascript"> document.write("HelloWorld!"); </script> Output HelloWorld! 2. Write a program to findthe sumof any twonumbers <script type="text/javascript"> var a=5, b=6, c=0; c=a+b; document.write("Firstvalue is="+a+"<br/>"); document.write("Secondvalueis="+b+"<br/>"); document.write("Total value is="+c+"<br/>"); </script> Output Firstvalue is=5 Secondvalue is=6 Total value is=11 3. Write a program to findthe total and percentage of anyfive subjects. <script type="text/javascript"> var hindi=50,english=70,math=60, science=64,sst=65; var total=0,percentage=0; total=hindi+english+math+science+sst; percentage=total*100/500; document.write("Marksof englishis=",english,"<br/>"); document.write("Marksof mathis=",math,"<br/>"); document.write("Marksof science is=",science,"<br/>"); document.write("Marksof hindi is=",hindi,"<br/>"); document.write("Marksof sstis=",sst,"<br/>"); document.write("Total marksis=",total,"<br/>"); document.write("Percentage is=",percentage,"%<br/>"); </script> Output Marks of englishis=70 Marks of math is=60 Marks of science is=64 Marks of hindi is=50 Marks of sst is=65
  • 2. Total marksis=309 Percentage is=61.8% 4. Write a programto findthe Area, <script type="text/javascript"> var lenght=60,width=40,area=0; area=lenght*width; document.write("Lengthis=",length,"<br/>"); document.write("Widthis=",width,"<br/>"); document.write("Areais=",area,"<br/>"); </script> Output Lenghtis=60 Widthis=40 Areais=2400 5. Write a program to findthe (a+b)2 . <script type="text/javascript"> var a=2, b=2, c=0; c=a*a+b*b+2*a*b; document.write("Valueof ais=",a,"<br/>"); document.write("Valueof bis=",b,"<br/>"); document.write("(a+b)<sup>2</sup>is=",c,"<br/>"); </script> Output Value of a is=2 Value of b is=2 (a+b)2 is=16 6. Write a program to findthe simple Interest. <script type="text/javascript"> var amount=5000,rate=3,time=5,si=0,total=0; si=amount*rate*time/100; total=amount+si; document.write("Amountis=",amount,"<br/>"); document.write("Rate is=",rate,"<br/>"); document.write("Time is=",time,"<br/>"); document.write("Simple Interestis=",si,"<br/>"); document.write("Total amountis=",total,"<br/>"); </script> Output Amountis=5000 Rate is=3
  • 3. Time is=5 Simple Interestis=750 Total amountis=5750 7. Write a program to findthe givennumberisEvenorOdd. <script type="text/javascript"> var number=8; document.write("Givennumberis=",number,"<br/>"); if(number%2==0) { document.write("Even"); } else { document.write("Odd"); } </script> Output Givennumberis=8 Even 8. Write a program to findthe givennumberisEvenorOdd and Zero. <script type="text/javascript"> var number=8; document.write("Givennumberis=",number,"<br/>"); if(number>0) { document.write("+ve"); } else if(number<0) { document.write("-ve"); } else { document.write("zero"); } </script> Output Givennumberis=8 +ve 9. Write a program to findthe largestbetweentwonumbers. <script type="text/javascript">
  • 4. var a=9,b=10; document.write("Valueof ais=",a,"<br/>"); document.write("Valueof bis=",b,"<br/>"); if(a>b) { document.write("A isgreaternumber"); } else if(b>a) { document.write("Bisgreaternumber"); } else { document.write("Bothare equal"); } </script> Output Value of a is=9 Value of b is=10 B is greaternumber 10. Write a program to findthe largestbetweenthreenumbers. <script type="text/javascript"> var a=9,b=6,c=2; document.write("Valueof ais=",a,"<br/>"); document.write("Valueof bis=",b,"<br/>"); document.write("Valueof cis=",c,"<br/>"); if(a>b&& a>c) { document.write("A isgreaternumber"); } else if(b>a&& b>c) { document.write("Bisgreaternumber"); } else if(c>a&& c>b) { document.write("Cisgreaternumber"); } else { document.write("Equal"); }
  • 5. </script> Output Value of a is=9 Value of b is=6 Value of c is=2 A isgreaternumber 11. Write a program to findthe ticketcostaccording to age:if age<=1) ticketcost is50 Rupees ,if age>15 and age <=30 ticketcostis 100 Rupees,if age >30 ticketcost is150 Rupees. <script type="text/javascript"> var age=14, tc=0; document.write("Ageis=",age,"<br/>"); if(age<=15) { tc=50; } else if(age>15&&age<=30) { tc=100; } else if(age>30) { tc=150; } document.write("Ticketcostis=",tc,"<br/>"); </script> Output Ticketcost is=50 12. Write a program to findthe ProfitandLose according to the Costprice and Sale price. <script type="text/javascript"> var cp=80,sp=80; document.write("Costprice is=",cp,"<br/>"); document.write("Sale price is=",sp,"<br/>"); if(sp>cp) {
  • 6. amount=sp-cp; document.write("Profitof rupees",amount,"/-<br/>"); } else if(cp>sp) { amount=cp-sp; document.write("Lossof rupees",amount,"/-<br/>"); } else { document.write("Noprofitnoloss"); } </script> Output Cost price is=80 Sale price is=80 No profitnoloss 13. Write a program to findthe givenseries usingwhile loop ->12 3 4 5. <script type="text/javascript"> var i=0; while(i<5) { i=i+1; document.write(i,"<br/>") } </script> Output 14. Write a program to findthe Evenseries usingwhileloop ->2 4 6 8 10. <script type="text/javascript"> var i=0; while(i<10) { i=i+2; document.write(i,"<br/>") } </script> 15. Write a program to findthe Oddseries usingwhile loop ->1,3,5,7,9, <script type="text/javascript"> var i=0; while(i<10) { Output 2 4 6 8 10 Output 1 2 3 4 5 Output 1 3 5 7 9
  • 7. i=i+2; document.write(i-1,"<br/>") } </script> 16. Write a program to findthe series 1/1, 1/2, 1/3, 1/4, 1/5. <script type="text/javascript"> var i=0; while(i<5) { i=i+1; document.write("1/",i,"<br/>") } </script> 17. Write a program to findthe givenseriesusingforloop12 3 4 5 <script type="text/javascript"> var i=0; for(i=1;i<=5;i++) { document.write(i,"<br/>"); } </script> 18. Write a program to findthe givenseriesusingforloop24 6 8 10 <script type="text/javascript"> var i=0; for(i=2;i<=10;i=i+2) { document.write(i,"<br/>"); } </script> 19. Write a program to findthe givenseriesusingfor loop13 5 7 9 <script type="text/javascript"> var i=0; for(i=1;i<=10;i=i+2) { document.write(i,"<br/>"); } </script> 20. Write a program to findthe givenseriesusingforloop1/1,1/2, 1/3, 1/4, 1/5 <script type="text/javascript"> var i=0; for(i=1;i<=5;i=i+1) { Output 1/1 1/2 1/3 1/4 1/5 Output 1 2 3 4 5 Output 2 4 6 8 10 Output 1 3 5 7 9 Output 1/1 1/2 1/3 1/4 1/5
  • 8. document.write("1/",i,"<br/>"); } </script> 21. Write a program to findthe table of any givennumber. <script type="text/javascript"> var i=0,n=5,t=0; for(i=1;i<=10;i=i+1) { t=i*n; document.write(n,"*",i," = ",t,"<br/>"); } </script> Output 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50 22. Write a program to findthe array of any5 student’smarks. <script type="text/javascript"> var marks=newArray(45,76,78,98,56),i=0; for(i=0;i<5;i=i+1) { document.write(marks[i],"<br/>"); } </script> 23. Write a program to findthe array of any5 student’smarksandfindthere sumand average. <script type="text/javascript"> var marks=newArray(45,76,78,98,56); var i=0,sum=0,avg=0; for(i=0;i<5;i=i+1) { document.write(marks[i],"<br/>"); sum=sum+marks[i]; } avg=sum/5; document.write("Sumis=",sum,"<br/>"); document.write("Averageis=",avg,"<br/>"); </script> Output 45 76 78 98 56 Sumis=353 Average is=70.6 Output 45 76 78 98 56
  • 9. 24. Write a program to findthe array of any5 student’smarksandfindthere minimumand maximumvalue. <script type="text/javascript"> var marks=new Array(45,76,78,98,56); var i=0,min=0,max=0; min=marks[1]; max=marks[1]; for(i=0;i<5;i=i+1) { document.write(marks[i],"<br/>"); if(min>marks[i]) { min=marks[i]; } if(max<marks[i]) { max=marks[i]; } } document.write("Minimumis=",min,"<br/>"); document.write("Maximumis=",max,"<br/>"); </script> 25. Write a program to findthe array of any10 numbersandfindthe sumof evennumbers. <script type="text/javascript"> var no=new Array(45,76,78,98,56,76,79,43,45,78),i=0,sum=0; for(i=0;i<10;i=i+1) { document.write(no[i],"<br/>"); if(no[i]%2==0) { sum=sum+no[i]; }} document.write("Total of evennumbersis=",sum,"<br/>"); </script> Output 45 76 78 98 56 Minimumis=45 Maximumis=98 Output 45 76 78 98 56 76 79 43 45 78 Total of evennumbersis=462
  • 10. 26. Write a program to findthe array of any10 numbersandfindthe sumof evennumbers. <script type="text/javascript"> var no=newArray(45,76,78,98,56,76,79,43,45,78),i=0,sum=0; document.write("Withoutsort<br/>"); for(i=0;i<10;i=i+1) { document.write(no[i],"<br/>"); } for(i=0;i<10;i=i+1) { for(j=i+1;j<10;j++) { if(no[i]>no[j]) { a=no[i]; no[i]=no[j]; no[j]=a; }}} document.write("Aftersortinaccendingorder<br/>"); for(i=0;i<10;i=i+1) { document.write(no[i],"<br/>"); } </script> 27. Write a program to findthe factorial number. <script type="text/javascript"> var i=0,n=4,f=1; for(i=1;i<=n;i++) { f=f*i } document.write("Factorial numberis=",f,"<br/>"); </script> Output Factorial numberis=24 28. Write a program to showto givenpattern <script type="text/javascript"> var i=0,j=0; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { Output Withoutsort 45 76 78 98 56 76 79 43 45 78 Aftersortin accendingorder 43 45 45 56 76 76 78 78 79 98
  • 11. document.write(i,""); } document.write("<br/>"); } </script> 29. Write a program to showto givenpattern <script type="text/javascript"> var i=0,j=0; for(i=8;i>=1;i--) { for(j=1;j<=i;j++) { document.write(i,""); } document.write("<br/>"); } </script> 30. Write a program to showto givenpattern <script type="text/javascript"> var i=0,j=0,n=0; for(i=1;i<=4;i++) { for(j=1;j<=i;j++) { n=n+2; document.write(n,""); } document.write("<br/>"); } </script> 31. Write a program to displaythe factorial numberusingfunction“fact();” <html> <head> <title>Factorial number</title> <script type="text/javascript"> functionfact() Output 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Output 8 8 8 8 8 8 8 8 7 7 7 7 7 7 7 6 6 6 6 6 6 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1 Output 2 4 6 8 10 12 14 16 18 20
  • 12. { var i=0,f=1; var n=parseInt(document.getElementById("n").value); while(i<n) { i=i+1; f=f*i; } alert("Factorial numberis="+f); } </script> </head> <body> Enter anynumber: <inputtype="text"value="4"id="n"> <inputtype="button"value="Factorial"onclick="fact();"> </body> </html> Output 32. Write a program to show the febnocci series <html> <head> <title>Febnocci series</title> <script type="text/javascript"> var a,b,c,n=10; document.write("Givennumberis=",n,"<br/>"); a=1; b=1; document.write(a,"<br/>"); document.write(b,"<br/>"); for(c=1;c<=n;c++) { c=a+b; a=b; b=c; document.write(c,"<br/>");
  • 13. } </script> </head> <body> </body> </html> The JavaScript Switch Statement <!DOCTYPE html> <html> <body> <p id="demo"></p> <script> var day=2; switch(day) { case 1: day = "Monday"; break; case 2: day = "Tuesday"; break; case 3: day = "Wednesday"; break; case 4: day = "Thursday"; break; case 5: day = "Friday"; break; case 6: day = "Saturday"; break; case 7: day="Sunday"; break; default: day="Wrongday"; break; } document.getElementById("demo").innerHTML = "Todayis " + day; </script> <body> </body> </html> In javascriptwrite aprogram using“switchcase”to accept two numbersfromuserand perform addition,subtractionandproducttothe acceptednumber. <!DOCTYPE html> <html> <body> <script> var a=0,b=0,ans=0,ch=0; a=Number(prompt("Enterfirstvalue=")); b=Number(prompt("Entersecondvalue=")); ch=Number(prompt("Press1.(addition) 2. (subtraction),3.(product)=")); switch(ch) { case 1: ans=a+b; break; case 2: Output Givennumberis=10 1 1 2 3 5 8 13
  • 14. ans=a-b; break; case 3: ans=a*b; break; default: alert("Wrongchoice"); break; } document.write("Firstvalue is=",a,"<br/>"); document.write("Secondvalueis=",b,"<br/>"); document.write("Resultis= ",ans,"<br/>"); </script> <body> </body> </html> Write a program inhtml usingjava scriptto accept anytwo numbersfromuseran performfollowing operationinthe same program.  Sumof the numbers  Difference betweenthe numbers(subtraction)  Productsof the numbers <!DOCTYPE html> <html> <body> <script> var a=0,b=0,sum=0,sub=0,pro=0; a=Number(prompt("Enterfirstvalue=")); b=Number(prompt("Entersecondvalue=")); sum=a+b; sub=a-b; pro=a*b; document.write("Firstvalue is=",a,"<br/>"); document.write("Secondvalue is=",b,"<br/><br/>"); document.write("Sumis=",sum,"<br/>"); document.write("Subtractionis=",sub,"<br/>"); document.write("Productis=",pro,"<br/>"); </script> <body> </body> </html> Write a program inhtml usingjavascriptto declare afunction‘fact()’calculate factorial of entered numberusingthisfunction. <!DOCTYPE html> <html> <body> <script> function fact() { var i=0,f=1; var n=parseInt(document.getElementById("n").valu e); var ans=document.getElementById("ans"); while(i<n) { i=i+1; f=f*i; } ans.value=f; } </script> <body> Enter anynumber: <inputtype="text"value="" id="n">Factorial numberis: <inputtype="text" value=""id="ans"><inputtype="button" value="Result"onclick="fact();"> </body> </html> Output
  • 15. Break The break statement breaks the loop and continues executing the code after the loop <script> var i=0; for (i = 1; i <=10; i++) { if (i == 5) { break; } document.write(i,"<br/>"); } </script> Output 1 2 3 4 Data typesinjavascript o Boolean o Null o Undefined o Number o String Boolean Boolean represents a logical entity and can have two values: true, and false. var x = 5; var y = 5; var z = 6; (x == y) // Returns true (x == z) // Returns false Null The Null type has exactly one value: null. <script> var n; if(n==null) { document.write('Not value found'); } </script>
  • 16. Undefined In JavaScript, a variable without a value, has the value undefined. The typeof is also undefined. var car; // Value is undefined, type is undefined Number JavaScript has only one type of numbers. Numbers can be written with, or without decimals: var x1 = 34.00; // Written with decimals var x2 = 34; // Written without decimals String o A string (or a text string) is a series of characters like "John Doe". o Strings are written with quotes. You can use single or double quotes: var carName = "Volvo XC60"; // Using double quotes var carName = 'Volvo XC60'; // Using single quotes There are 3 JavaScript methods that can be used to convert variables to numbers:  The Number() method  The parseInt() method  The parseFloat() method Method Description Number() Returns a number, converted from its argument. parseFloat() Parses its argument and returns a floating point number parseInt() Parses its argument and returns an integer Math.round() Math.round(x) returns the value of x rounded to its nearest integer: Math.round(4.7); // returns 5 Math.round(4.4); // returns 4 Math.pow() Math.pow(x, y) returns the value of x to the power of y:
  • 17. Math.pow(8, 2); // returns 64 Math.sqrt() Math.sqrt(x) returns the square root of x: Math.sqrt(64); // returns 8 Math.abs() Math.abs(x) returns the absolute (positive) value of x: Math.abs(-4.7); // returns 4.7 Math.ceil() Math.ceil(x) returns the value of x rounded up to its nearest integer: Math.ceil(4.4); // returns 5 Math.ceil() Math.ceil(x) returns the value of x rounded up to its nearest integer: Math.ceil(4.4); // returns 5 Math.min() and Math.max() Math.min() and Math.max() can be used to find the lowest or highest value in a list of arguments: Math.min(0, 150, 30, 20, -8, -200); // returns -200 Math.max(0, 150, 30, 20, -8, -200); // returns 150 Math.random() Math.random() returns a random number between 0 (inclusive), and 1 (exclusive): Math.random(); // returns a random number Using String search() With a Regular Expression
  • 18. Use a regular expression to do a case-insensitive search for "w3schools" in a string: var str = "Visit W3Schools"; var n = str.search(/w3schools/i); 6 Use String replace() With a Regular Expression Use a case insensitive regular expression to replace Microsoft with W3Schools in a string: var str = "Visit Microsoft!"; var res = str.replace(/microsoft/i, "W3Schools"); Visit W3Schools! var str = "Visit Microsoft!"; var res = str.replace("Microsoft", "W3Schools"); Modifier Description i Perform case-insensitive matching g Perform a global match (find all matches rather than stopping after the first match) m Perform multiline matching