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

CSS Practical 1 To 4

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

CSS Practical 1 To 4

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

PRACTICAL NO.

1
AIM : Develop JavaScript to use decision making and looping statements.

THEORY:

Conditional Statements:

1. The if Statement
Use the if statement to specify a block of JavaScript code to be executed if a
condition is true.
Syntax :
if (condition) {
// block of code to be executed if the condition is true
}
2.The else Statement
Use the else statement to specify a block of code to be executed if the condition
is false.
Syntax:
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
3.The else if Statement
Use the else if statement to specify a new condition if the first condition is false.
Syntax if
(condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is
false }
4.The Switch Statement
Use the switch statement to select one of many code blocks to be executed.
Syntax:
switch(expression) {
case x:
//code block
Break
case y:
//code block
Break;
default:
//code block
}
JavaScript Loops
1. for loop
Loops are handy, if you want to run the same code over and over again, each time
with a different value.
Syntax:-
for (initialization condition; testing condition; increment/decrement)
{
statement(s)
}
Or for objects
for (variable_Name in Object)
{
statement(s)
}
2. do while:
do while loop is similar to while loop with only difference that it checks for condition
after executing the
statements, and therefore is an example of Exit Control Loop.
Syntax:-
do
{
statements..
}while (condition);
3. While loop
A while loop is a control flow statement that allows code to be executed repeatedly
based on a given
Boolean condition. The while loop can be thought of as a repeating if statement.
Syntax :-
while (boolean condition)
{
loop statements...
}

PROGRAM CODE :

<html>
<head>
<title> Practical 1</title>
<script type = "text/javascript">
document.write("<b> Even Number between 1 to 20 </b> ");
for(var i =1; i<=20;i++)
{
if(i%2==0)
{
document.write("<br>" +i);
}

}
</script>
</head>
</html>

OUTPUT :

Marks Obtained Dated Signature of


Teacher

Process Product Total


Related (35) Related (15) (50)
PRACTICAL NO. 2
AIM : Develop javascript to implements array functionalities.

THEORY :

What is an Array?
An array is a special variable, which can hold more than one value at a time.

Creating an Array
Using an array literal is the easiest way to create a JavaScript Array.

Syntax:
var array_name = [item1, item2, ...];

JavaScript Array directly (new keyword)

The syntax of creating array directly is given below:


var arrayname=new Array();

Here, new keyword is used to create instance of array.

JavaScript Array Methods

find() : It returns the value of the first element in the given array that satisfies
the specified condition.
findIndex() : It returns the index value of the first element in the given array that
satisfies the specified condition.
indexOf() : It searches the specified element in the given array and returns the
index of the first match.
lastIndexOf() : It searches the specified element in the given array and returns
the index of the last match.
pop() : It removes and returns the last element of an array.
push() : It adds one or more elements to the end of an array.
reverse(): It reverses the elements of given array.
shift() : It removes and returns the first element of an array.
unshift() : It add one or more elements at the first element of an array.
sort() : It returns the element of the given array in a sorted order.
PROGRAM CODE :
<html>
<head>
<title> Practical 2</title>
<h2> Array Functionalities</h2>
<script type = "text/javascript">

var arr = [10,60,30,70,50];

document.write("<b> Length of Array is: </b> " +arr.length);

document.write("<br><br> <b> Element of Array is: </b> " +arr);

arr.push(20,80);
document.write("<br><br> <b> Element of Array after push operation is: </b> "
+arr);

arr.pop();
document.write("<br><br> <b> Element of Array after pop operation is: </b> "
+arr);

arr.unshift(5,40);
document.write("<br><br> <b> Element of Array after unshift operation is:
</b> " +arr);

arr.shift();
document.write("<br><br> <b> Element of Array after shift operation is: </b> "
+arr);

arr.sort();
document.write("<br><br> <b> Sorted Array is: </b> " +arr);

arr.reverse();
document.write("<br><br> <b> Reverse Array is: </b> " +arr);

</script>
</head>
</html>
OUTPUT :

Marks Obtained Dated Signature of


Teacher

Process Product Total


Related (35) Related (15) (50)
PRACTICAL NO. 3
AIM : Develop javascript to implement functions.

THEORY :
Function
JavaScript functions are used to perform operations. We can call JavaScript
function many times to reuse the code.
Advantage of JavaScript function:
There are mainly two advantages of JavaScript functions.
1. Code reusability: We can call a function several times so it save coding.
2. Less coding: It makes our program compact. We don’t need to write
many lines of code each time to perform a common task.
JavaScript Function Syntax :-
function function_Name([arg1, arg2, ...argN])
{
//code to be executed
}
JavaScript Functions can have 0 or more arguments..

PROGRAM CODE :
<html>
<head>
<title> Practical 3 </title>
<script type = "text/javascript">
var n = parseInt(prompt("Enter a Number"));
function factorial(num)
{
var fact= 1;
for(var i = 1; i<=num; i++)
{
fact = fact * i;
}
return(fact);
}
document.write(" <b> Factorial is : <b>" +factorial(n));
</script>
</head>
</html>

OUTPUT :

Marks Obtained Dated Signature of


Teacher

Process Product Total


Related (35) Related (15) (50)

You might also like