Javascript Practical File Work
Javascript Practical File Work
1. Basic Variables and Output: Write a JavaScript program to display a welcome message in an alert
box when the page is loaded.
html
Copy code
<!DOCTYPE html>
<html>
<head>
<title>Basic Variables</title>
<script>
window.onload = function() {
};
</script>
</head>
<body>
</body>
</html>
2. User Input: Create a script that prompts the user to enter their name, and then display a greeting
message using their name.
2. User Input
html
Copy code
<!DOCTYPE html>
<html>
<head>
<title>User Input</title>
<script>
function greetUser() {
</script>
</head>
<body>
</body>
</html>
3. Conditional Statements: Write a program that checks if a number entered by the user is even or
odd and displays the result in an alert box. 3. Conditional Statements
html
Copy code
<!DOCTYPE html>
<html>
<head>
<title>Even or Odd</title>
<script>
function checkEvenOdd() {
if (num % 2 === 0) {
} else {
</script>
</head>
<body>
</body>
</html>
4. Loops: Write a JavaScript program that uses a for loop to display numbers from 1 to 10 on the
webpage. 4. Loops
JAVASCRIPT
html
Copy code
<!DOCTYPE html>
<html>
<head>
<script>
function displayNumbers() {
output += i + "<br>";
document.getElementById("result").innerHTML = output;
</script>
</head>
<body>
<div id="result"></div>
</body>
</html>
5. Array Manipulation: Create an array of 5 numbers and display the sum of all numbers using
JavaScript.5. Array Manipulation
html
Copy code
<!DOCTYPE html>
<html>
<head>
<title>Array Sum</title>
<script>
function calculateSum() {
JAVASCRIPT
let sum = 0;
sum += numbers[i];
</script>
</head>
<body>
</body>
</html>
6. Functions: Write a JavaScript function that accepts two numbers as parameters and returns the
greater of the two numbers. 6. Functions
html
Copy code
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Functions</title>
<script>
function displayGreater() {
}
JAVASCRIPT
</script>
</head>
<body>
</body>
</html>
7. Event Handling: Create a button on a webpage that, when clicked, changes the background color
of the page.7. Event Handling
html
Copy code
<!DOCTYPE html>
<html>
<head>
<title>Event Handling</title>
<script>
function changeBackgroundColor() {
document.body.style.backgroundColor = "lightblue";
</script>
</head>
<body>
</body>
</html>
8. DOM Manipulation: Create a webpage with two paragraphs. Write JavaScript to change the text
content of the second paragraph when the first paragraph is clicked. 8. DOM Manipulation
html
Copy code
<!DOCTYPE html>
<html>
<head>
<title>DOM Manipulation</title>
JAVASCRIPT
<script>
function changeText() {
</script>
</head>
<body>
</body>
</html>
9. Form Validation: Write a JavaScript program that validates a form. Ensure that the user enters text
in the "Name" field and a valid email address in the "Email" field. 9. Form Validation
html
Copy code
<!DOCTYPE html>
<html>
<head>
<title>Form Validation</title>
<script>
function validateForm() {
return false;
if (!email.includes("@")) {
return false;
}
JAVASCRIPT
return true;
</script>
</head>
<body>
<label for="name">Name:</label>
<label for="email">Email:</label>
<button type="submit">Submit</button>
</form>
</body>
</html>
10. Timers and Delays: Write a JavaScript program that shows an alert box after 3 seconds of page
load, using the setTimeout function.10. Timers and Delays
html
Copy code
<!DOCTYPE html>
<html>
<head>
<script>
window.onload = function() {
setTimeout(function() {
}, 3000);
};
JAVASCRIPT
</script>
</head>
<body>
</body>
</html>