You are on page 1of 7

Theory: -

Practical related questions:


1. Explain For…in loop?

Ans.
The JavaScript for in loop is used to
iterate the properties of an object.
The block of code inside the loop will be
executed once for each property.

Example: -
<script>
var numbers = [10,20,30,40,50];
var i;
for(i in numbers) {
document.write(numbers[i]+"<br>");
}
</script>

2. Difference between if…else and switch statement.

Ans.

If else Switch
In the case of if-else statement, either In the case of the switch statement, one
the 'if' block or the 'else' block will be case after another will be executed until
executed based on the condition. the break keyword is not found, or the
default statement is executed.
If the condition is not true, then by If the value does not match with any
default, else block will be executed. case, then by default, default statement
is executed
It evaluates a condition to be true or A switch statement compares the value
false. of the variable with multiple cases.

19202C0058
3. Difference between while and do while.
Ans.
While Do While
In While loop, the condition tested at the In do While loop, the condition tested at
beginning of the loop, and if the the end of the loop
condition is True, statements inside the
loop will execute.
While loop executes the code block only Do While executes the statements in the
if the condition is True. code block at least once even if the
condition Fails.

Ex:- Ex:-
<script> <script>
var x = 1; var x = 1;
while (x > 10) do
{ {
document.write("<br\> <b> X Is document.write("<br\> <b> X Is
greater Than 10 </b>"); greater Than 10 </b>");
} }while (x > 10);
</script> </script>

Exercise:
1. Write a JavaScript program to print the factorial of a given number.

Code

<html>
<head>
<title>
factorial of a given number
</title>
</head>
<body>
<center>

19202C0058
<h2>Javascript Factorial</h2>
<button onclick="myFunction()">Promt Button</button>
<script>
function myFunction() {
var i;
var fact = 1;
var number = prompt("Please enter a number")
for(i = 1;i <= number;i++) {
fact = fact * i;
}
document.write("Factorial of "+number+" is "+fact);
}
</script>
</body>
</html>

Output

19202C0058
2. Write a JavaScript program to print the Fibonacci series till 10 number.

Code

<html>
<head>
<title>
Fibonacci series
</title>
</head>
<body>
<center>
<h2>Javascript Fibonacci Series </h2>
<button onclick="myFunction()">Promt Button</button>
<script>
function myFunction() {
var i;
var num1 = 0;
var num2 = 1;
var num3;
var number = prompt("Please enter a number you want for Fibonacci
Series")
for(i = 1;i <= number;i++) {
document.write("<br>"+num1);
num3 = num1 + num2;
num1 = num2;
num2 = num3;
}
}
</script>
</body>
</html>

Output

19202C0058
3. Write a code to find out whether the year is leap year or not.

Code

<html>
<head>
<title>
Leap Year or Not
</title>
</head>
<body>
<center>
<h2>Javascript Leap Year Checking</h2>
<button onclick="myFunction()">Promt Button</button>
<script>
function myFunction() {

19202C0058
var leap;
var year = prompt("Please enter a Year")
if(year % 100 == 0) {
if(year % 400 == 0) {
leap = true;
}
else {
leap = false;
}
}
if(leap) {
document.write("You have Entered "+year+" which is a Leap Year");
}
else {
document.write("You have Entered "+year+" which is Not a Leap
Year");
}

}
</script>
</body>
</html>

Output

19202C0058
19202C0058

You might also like