hemant css practicals 1 to 4 (1)
hemant css practicals 1 to 4 (1)
Subject: CSS
Enrollment :23310250269
PRACTICAL NO 1
AIM : Develop Javascript to use decision making and looping statements.
Basic concept:
Decision Making Statements
• Decision Making in programming is similar to decision making in real life.
• A programming language uses control statements to control the flow of execution of
the program based on certain conditions.
2) if-else
3) if…else…if
4) switch
1) if statement
• Syntax:
if(condition)
{
statements;
}
2) if…else statement
• The if-else statement has two parts if block and else block.
• If the condition is true then if block (true block) will get executed and if the condition
is false then else block (false block) will get executed.
• Syntax:
if(condition)
{
statement 1: //true block
}
e
l
s
e
{
statement 2; //false block
}
3) if…else ladder
• If any one of the given condition is satisfied then the related statement are executed,
if all the condition does not satisfied then default statement will be executed.
• Syntax:
if(condition 1)
{
statement 1;
}
else if(condition 2)
{
statement 2;
}
else if(condition
n)
{
statement n;
}
else
{
statement m;
}
4) switch statement
• The interpreter checks each case against the value of the expression until a match is
found. If nothing matches, a default condition will be used.
• Syntax:
switch(expression)
{
case
constant_expression1;state
ment;
break;
case constant_expression2;
statement; break;
case constant_expressionN;
statement; break;
default; statement
m;
}
Looping Statements
• For example, suppose we want to print “Hello World” 10 times this is possible with
the help of loops.
1) Entry Controlled loops: In this type of loops the test condition is tested
before entering the loop body. For Loop and While Loop are entry controlled loops.
2) Exit Controlled Loops: In this type of loops the test condition is tested or
evaluated at the end of loop body. Therefore, the loop body will execute atleast once,
irrespective of whether the test condition is true or false. do-while loop is exit
controlled loop.
Following are the types of loops in JavaScript:
1) while loop
2) do-while loop
3) for loop
Program:
<html>
<title>Decision Making and Looping Statement</title>
<head>
<script language= "javascript" type="text/javascript">
var ch=parseInt(prompt("1.Even-Odd 2.Factorial"));
var a =parseInt(prompt("Enter a Number"));
switch(ch)
{
case 1:
if(a%2==0)
{
alert(a+" is even Number");
}
else
{
alert(a+" is Odd Number");
}
break;
case 2:
var fact=1;
for(var i=1;i<=a;i++)
{
fact=fact*i;
}
alert(fact+" is Factorial");
break;
default:
alert("Error 405");
}
</script>
</head>
</html>
OUTPUT :-
.
Basic concept :
JavaScript Arrays : An array is a special variable, which can hold more than one value
at a time. JavaScript arrays are used to store multiple values in a single variable.
Declaring an array There are 2 ways to construct an array in JavaScript 1) By array
literal
1) var keyword
2) Array name
3) Assignment operator
4) new operator
Program Code:-
<html>
<title>Array Functionalities</title>
<head>
<script language= “javascript” type=”text/javascript”>
Switch(ch){
Case 1:
Var rev=a.reverse();
Alert(“Reverse array is “+rev);
Break;
Case 2:
a.pop();
alert(“(last element is deleted)\n Array is “+a);
break;
case 3:
var so=a.sort();
alert(“Sorted Array is “+so);
break;
case 4:
a.shift();
alert(“(First Element is Deleted)\n Array is “+a);
break;
default:
alert(“Error 405”);
}
</script>
</head>
</html>
Output:-
Marks Obtained Dated signature of
Teacher
Process Related(15) Product Related(10) Total(25)
PRACTICAL NO 3
Basic Concept:
Functions in Javascript :- function is a group of reusable code which can be called
anywhere in the program. This eliminates the need to rewrite the same code.
Functions allow a programmer to divide a big program into a smaller and
manageable function. JavaScript provides functions just like other programming
languages. A JavaScript function can be defined using the function keyword.
There are mainly two advantages of JavaScript functions.
1. Code reusability: We can call a func on several mes whenever we need it.
2. Less coding: It reduces the line of code and makes our program compact.
Program:-
<html>
<head>
<title>Fuction Demo</title>
<script>
Function add(a,b)
{
Return a+b;
}
Function sub(a,b)
{
Return a-b;
}
Function mul(a,b)
{
Return a*b;
}
Function div(a,b)
{
Return a/b;
}
Var a=parseInt(prompt(“Enter First Number”));
Var b=parseInt(prompt(“Enter Second Number”));
Var n1=add(a,b);
Var n2=sub(a,b);
Var n3=mul(a,b);
Var n4=div(a,b);
Document.write(“<br>Addition is:- “+n1);
Document.write(“<br>Subtraction is:- “+n2);
Document.write(“<br>Multiplication is:- “+n3);
Document.write(“<br>Division is:- “+n4);
</script>
</head>
</html>
Output:
Program:-
<!DOCTYPE html>
<html>
<head>
< tle> Form Event </ tle>
<script>
func on changeColor(color)
{
var panel = document.getElementById('panel');
document.body.style.backgroundColor = color;
panel.innerHTML = "Background color is set to: " + color.toUpperCase();
}
</script>
</head>
<body onload="changeColor('red')">
<p>Select an op on to change the background color of the page</p>
<form name="myform">
<input type="radio" name="color" value="red" onchange="changeColor(this.value)" checked="false">RED<br />
<input type="radio" name="color" value="GREEN" onchange="changeColor(this.value)">GREEN<br />
<input type="radio" name="color" value="blue" onchange="changeColor(this.value)">BLUE<br />
</form>
<p id="panel"></p>
</body>
</html>
</html>
Output:-