Lab assignment:Javascript
Book exercise page 150 number 4
Lab
Assignment
1.
Write a program
in Java Script to display the product of three numbers.
<html>
<head>
<title>Product of Three
Numbers</title>
</head>
<body>
<script>
var a = parseInt(prompt("Enter
1st number"));
var b = parseInt(prompt("Enter
2nd number"));
var c = parseInt(prompt("Enter
3rd number"));
var product = a * b * c;
document.write("The product
is: " + product);
</script>
</body>
</html>
2.
Write a program
in Java Script to calculate the area of a room.
<html>
<head>
<title> area of a room </title>
</head>
<body>
<script>
var l = parseInt(prompt("Enter
length"));
var b = parseInt(prompt("Enter
breadth"));
var area = l * b ;
document.write("area of room: " + area);
</script>
</body>
</html>
3.
Write a program
in Java Script to display whether the given number is even or odd.
<html>
<head>
<title>Even or Odd</title>
</head>
<body>
<script>
var num =
parseInt(prompt("Enter a number"));
if(num % 2 == 0){
document.write(num + " is
Even");
} else {
document.write(num + " is
Odd");
}
</script>
</body>
</html>
4.
Write a program
in Java Script to display whether the given number is positive, negative or
neutral.
<html>
<head>
<title>Positive or
Negative</title>
</head>
<body>
<script>
var n = parseInt(prompt("Enter
a number"));
if(n > 0){
document.write("Positive");
} else if(n < 0){
document.write("Negative");
} else {
document.write("Neutral");
}
</script>
</body>
</html>
5.
Write a program
in Java Script to display all the even numbers from 2 to 100
<html>
<head>
<title>Even Numbers 2 to
100</title>
</head>
<body>
<script>
for (var i = 2; i <= 100; i +=
2) {
document.write(i +
"<br>");
}
</script>
</body>
</html>
6.
Write a program
in Java Script to display the sum of all the numbers from 1 to 20
<html>
<head>
<title>Sum 1 to 20</title>
</head>
<body>
<script>
var sum = 0;
for(var i = 1; i <= 20; i++){
sum += i;
}
document.write("Sum of numbers
from 1 to 20 is: " + sum);
</script>
</body>
</html>
7.
Write a a
program in Java Script to display the sum of two numbers using function
<html>
<head>
<title>Sum Using
Function</title>
</head>
<body>
<script>
function add(a, b){
return a + b;
}
var x = parseInt(prompt("Enter
1st number"));
var y = parseInt(prompt("Enter
2nd number"));
var result = add(x, y);
document.write("The sum is:
" + result);
</script>
</body>
</html>
8.
Write a program
in Java Script to display the name of the day according to the value given by
the user using switch case. (For example, the program should display Sunday if
user enters 1, Monday if user enters 2 and so on)
<html>
<head>
<title>Day Name</title>
</head>
<body>
<script>
var day =
parseInt(prompt("Enter a number (1–7)"));
var name;
switch(day) {
case 1: name =
"Sunday"; break;
case 2: name =
"Monday"; break;
case 3: name =
"Tuesday"; break;
case 4: name =
"Wednesday"; break;
case 5: name = "Thursday";
break;
case 6: name =
"Friday"; break;
case 7: name =
"Saturday"; break;
default: name = "Invalid
input";
}
document.write("Day: " +
name);
</script>
</body>
</html>
Using if..else
<html>
<head>
<title>Day Name </title>
</head>
<body>
<script>
var day =
parseInt(prompt("Enter a number (1–7)"));
var name;
if (day == 1)
name = "Sunday";
else if (day == 2)
name = "Monday";
else if (day == 3)
name = "Tuesday";
else if (day == 4)
name = "Wednesday";
else if (day == 5)
name = "Thursday";
else if (day == 6)
name = "Friday";
else if (day == 7)
name = "Saturday";
else
name = "Invalid
Input";
document.write("Day: " +
name);
</script>
</body>
</html>
9.
Write a program
in Java Script that illustrates even handling.
<html>
<head>
<title>Event
Handling</title>
</head>
<body>
<button
onclick="showMessage()">Click Me</button>
<script>
function showMessage() {
alert("Button was
clicked!");
}
</script>
</body>
</html>
10. Write a program in Java Script to display all the
numbers from 100 to 1 using while loop
<html>
<head>
<title>Numbers 100 to
1</title>
</head>
<body>
<script>
var i = 100;
while (i >= 1) {
document.write(i +
"<br>");
i--;
}
</script>
</body>
</html>