<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lab-8 JavaScript Tasks</title>
</head>
<body>
<h1>Lab-8 JavaScript Tasks</h1>
<!-- Task 8: Button with onclick event -->
<button onclick="myFunction()">Click Me</button>
<!-- Task 18: Modal on page load -->
<script>
alert("JavaScript is awesome!");
</script>
<!-- Task 19: Prompt for birth year on page load -->
<script>
let birthYear = prompt("Enter the year of your birth:");
alert(`You entered: ${birthYear}`);
</script>
<!-- JavaScript code for other tasks -->
<script>
// Task 1: Display userName using alert
let userName = "John Doe";
alert(userName);
// Task 2: Declare a variable without assigning a value
let unassignedVar;
console.log(unassignedVar); // Output: undefined
// Task 3: Declare a constant
const PI = 3.14;
console.log(PI);
// Task 4: Declare a constant in uppercase
const MAX_USERS = 100;
console.log(MAX_USERS);
// Task 5: Create a number variable and perform operations
let num1 = 10;
let num2 = 20;
console.log(num1 + num2); // Output: 30
console.log(num1 * num2); // Output: 200
// Task 6: Use template strings
let firstName = "John";
let lastName = "Doe";
console.log(`Full Name: ${firstName} ${lastName}`);
// Task 7: Create an object and access its properties
let user = {
firstName: "John",
lastName: "Doe",
age: 30
};
console.log(user.firstName); // Output: John
console.log(user.age); // Output: 30
// Task 8: Function triggered by button click
function myFunction() {
alert("You clicked on the button");
}
// Task 9: Use console.log to output data
console.log("Hello, students!");
// Task 10: Use multiple console.log commands
console.log("First message");
console.log("Second message");
// Task 11: Use alert to display expressions
alert(`The sum of 10 and 20 is ${10 + 20}`);
// Task 12: Use alert to display expressions
alert(`The product of 5 and 6 is ${5 * 6}`);
// Task 13: Debugging errors
let result = 10 + 20;
console.log(result); // Output: 30
// Task 14: Function to display departure and arrival
function displayTravelInfo(departure, arrival) {
console.log(`Departure: ${departure}, Arrival: ${arrival}`);
}
displayTravelInfo("New York", "Los Angeles");
// Task 15: Function to calculate and display expression
function calculateExpression(num1, num2) {
console.log((num1 + num2) * 2);
}
calculateExpression(10, 20); // Output: 60
// Task 16: Use prompt to get user input
let age = prompt("How old are you?", 20);
console.log(age);
// Task 17: Use confirm to get user confirmation
let isConfirmed = confirm("Do you agree?");
console.log(isConfirmed);
// Questions
// Question 1: What will be displayed in the console?
let num3 = 10;
let num4 = 20;
console.log((num3 + num4) / 3); // Output: 10
// Question 2: What will be displayed in the console?
let num5 = 10;
let num6 = 20;
num5 = 30;
num6 = num5 - 15;
console.log(2 * num5 + num6); // Output: 75
// Question 3: What will be displayed in the console?
let userName2 = "Alex";
let userSurname = "Smith";
console.log(`${userName2} ${userSurname} is an advocate`); // Output: Alex Smith is an advocate
</script>
</body>
</html>
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容