Syntax:
switch (expression)
{
case value1: block1
break;
case value1: block2
break;
…
case value1: blockn
break;
default: default block
}
Here, the expression is checked
against the case values and the corresponding block is evaluated. If the
expression does not match any of the case value then default block is
evaluated.
against the case values and the corresponding block is evaluated. If the
expression does not match any of the case value then default block is
evaluated.
Q. Write javascript code
to display a prompt dialog box for entering an integer. Use switch case
structure to check whetehr the entered integer is even or odd.
to display a prompt dialog box for entering an integer. Use switch case
structure to check whetehr the entered integer is even or odd.
<html> <head>
<title>HTML Program </title> </head>
<title>HTML Program </title> </head>
<body>
<font color=blue size=16
face=candara>
face=candara>
<script >
var num=prompt("Enter an integer");
switch(num%2)
{
case 0:alert("Entered integer is
EVEN.");
case 0:alert("Entered integer is
EVEN.");
break;
case 1:alert("Entered
integer is ODD.");
integer is ODD.");
break;
}
For loop:
Syntax:
for (expression: condition: expression2)
{
block
}
Here, expression sets up a
counter variable and assign it an initial value counter variable can also be
declared along with “for” condition refers to statements returning Boolean
value true or false. Expression2 specifies whether the counter variable is
incremented or decremented in every iteration.
counter variable and assign it an initial value counter variable can also be
declared along with “for” condition refers to statements returning Boolean
value true or false. Expression2 specifies whether the counter variable is
incremented or decremented in every iteration.
Q. Write JavaScript code
which allows user to enter an integer using prompt dialog box. Display the
multiplication table the entered integer.
which allows user to enter an integer using prompt dialog box. Display the
multiplication table the entered integer.
<html>
<head>
<title>HTML Program</title>
</head>
<body>
<font color=blue size=7 face=candara>
<script >
var nm=prompt("Enter an integer");
document.write("<font color=red size=9><u>Multiplication Table of " + nm + "</u></font><br>");
for(var i=1;i<11;i++)
{
document.write(nm + " * " + i+" = " + (nm*i) + "<br>");
}
</script> </font> </body> </html>
Output:
Q. Write JavaScript code
which displays a question to the user. The user should be given 3 chances for
answering the question. If the user enters a correct answer, display an
appropriate message. If the user enter incorrect answer then display how many
chances are left with the user?
which displays a question to the user. The user should be given 3 chances for
answering the question. If the user enters a correct answer, display an
appropriate message. If the user enter incorrect answer then display how many
chances are left with the user?
<html>
<head>
<title>HTML Program</title>
</head>
<body>
<font color=blue size=16
face=candara>
face=candara>
<script >
for(var attempts=1;attempts<=3;attempts++)
{ var num=prompt("What is the answer for (5-5) ?");
if(num==0)
{
document.write("Correct answer !!!
<br> <br><img src=congrats.jpg>");
document.write("Correct answer !!!
<br> <br><img src=congrats.jpg>");
attempts=4;
}
else
{
alert("Wrong answer.\n You have
" + (3-attempts) +"
chances left.");
" + (3-attempts) +"
chances left.");
}
}
if(attempts==4)
document.write("<br><img
src=sorry.jpg>");
src=sorry.jpg>");
</script> </font>
</body> </html>
</body> </html>
While loop:-
Syntax:
while(condition)
{
block
}
Here, condition is a valid
javascript expression which result in a Boolean value true or false here the
block gets executed as long as the condition is true.
javascript expression which result in a Boolean value true or false here the
block gets executed as long as the condition is true.
Q. Design a webpage
which displays factorial of the entered integer using while loop?
which displays factorial of the entered integer using while loop?
<html> <head> <title>HTML Program</title> </head>
<body>
<font color=blue size=16
face=candara>
face=candara>
<script >
var num=prompt("Enter an integer");
var fact=1;
while(num>1)
{ fact*=num;
num--;
}
document.write("Factorial is " + fact);
</script> </font> </body> </html>
Do…while loop:
Syntax:
do
{
block
while(condition);
}
In do while loop the block gets executed at least one.
Q. Write javascript code
using a do…while loop to display a prompt dialog box for user to enter his
login name. If the user enters a correct login name then display an approximate
message otherwise prompt the user the entering his login name again.
using a do…while loop to display a prompt dialog box for user to enter his
login name. If the user enters a correct login name then display an approximate
message otherwise prompt the user the entering his login name again.
<html> <head> <title>HTML Program </title> </head>
<body>
<font color=blue size=16
face=candara>
face=candara>
<script >
var loginname;
do
{ loginname=prompt("Enter LOGIN NAME");
if (loginname=="FYIT")
document.write("Valid
User !! <br><img src=welcome3.png>");
User !! <br><img src=welcome3.png>");
else
alert("Incorrect
Login name");
Login name");
}while(loginname!="FYIT");
Q. Write Javascript code
which displays prompt dialog boxes for entering 2 integers. Display their sum
using an alert dialog box.
which displays prompt dialog boxes for entering 2 integers. Display their sum
using an alert dialog box.
<script >
var n1=prompt("Enter first
integer");
integer");
var n2=prompt("Enter second
integer");
integer");
alert("Sum
is " + (parseInt(n1)+parseInt(n2)));
is " + (parseInt(n1)+parseInt(n2)));
</script>
0 Comments