WC 2
WC 2
setInterval(updateClock, 1000);
updateClock(); // Initial call to display clock immediately
• HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Digital Clock</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="clock" class="clock"></div>
<script src="script.js"></script>
</body>
</html>
• CSS
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #282c34;
color: #61dafb;
font-family: 'Arial', sans-serif;
}
.clock {
font-size: 3rem;
background: #000;
padding: 20px;
border-radius: 10px;
text-align: center;
}
2. Compare ES 5 and ES 6. Give an example of the Anonymous and Arrow function inES6
Example
const numbers = [1, 2, 3, 4, 5];
Example
const numbers = [1, 2, 3, 4, 5];
Both types of functions can be useful depending on the situation, but arrow functions often provide
a cleaner and more concise way to handle short functions and callbacks.
1) To change the background color of the web page automatically after every 5seconds.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Change Background Color</title>
<style>
body {
transition: background-color 1s; /* Smooth transition for color change */
}
</style>
</head>
<body>
<script>
const colors = ['#FF6347', '#4682B4', '#32CD32', '#FFD700', '#FF69B4'];
let currentIndex = 0;
function changeBackgroundColor() {
document.body.style.backgroundColor = colors[currentIndex];
currentIndex = (currentIndex + 1) % colors.length;
}
setInterval(changeBackgroundColor, 5000);
</script>
</body>
</html>
2) To display three radio buttons on the web page, namely, “Red”, “Blue” and “Green”.
Selecting any button changes the background color as per the name of the button.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Change Background Color</title>
</head>
<body>
<h2>Select a color:</h2>
<input type="radio" id="red" name="color" value="red">
<label for="red">Red</label><br>
<input type="radio" id="blue" name="color" value="blue">
<label for="blue">Blue</label><br>
<input type="radio" id="green" name="color" value="green">
<label for="green">Green</label>
<script>
function changeBackgroundColor(event) {
document.body.style.backgroundColor = event.target.value;
}
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br>
<span id="passwordError" class="error"></span><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br>
<span id="emailError" class="error"></span><br>
<script>
document.getElementById('myForm').addEventListener('submit', function(event) {
event.preventDefault(); // Prevent form submission
// Validation flags
let isValid = true;
// Validate Username
if (username === '') {
document.getElementById('usernameError').textContent = 'Username cannot be
blank.';
isValid = false;
}
// Validate Password
if (password === '') {
document.getElementById('passwordError').textContent = 'Password cannot be
blank.';
isValid = false;
} else if (password.length < 8) {
document.getElementById('passwordError').textContent = 'Password must be at least
8 characters long.';
isValid = false;
}
// Validate Email
if (email === '') {
document.getElementById('emailError').textContent = 'Email cannot be blank.';
isValid = false;
} else if (!email.includes('@')) {
document.getElementById('emailError').textContent = 'Email must contain "@"
character.';
isValid = false;
}
if (isValid) {
// If valid, submit the form or perform desired action
alert('Form submitted successfully!');
}
});
</script>
</body>
</html>
6. Write a JavaScript to accept two numbers and display their sum using pop up box.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sum of Two Numbers</title>
</head>
<body>
<script>
// Function to prompt user for two numbers and display their sum
function calculateSum() {
// Prompt user for the first number
const num1 = parseFloat(prompt("Enter the first number:"));
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Alumni Information Form</title>
<style>
.error {
color: red;
font-size: 0.875rem;
}
</style>
</head>
<body>
<h2>Alumni Information Form</h2>
<form id="alumniForm">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br>
<span id="nameError" class="error"></span><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br>
<span id="emailError" class="error"></span><br>
<fieldset>
<legend>Hobbies:</legend>
<input type="checkbox" id="hobby1" name="hobbies" value="Reading">
<label for="hobby1">Reading</label><br>
<input type="checkbox" id="hobby2" name="hobbies" value="Traveling">
<label for="hobby2">Traveling</label><br>
<input type="checkbox" id="hobby3" name="hobbies" value="Sports">
<label for="hobby3">Sports</label>
</fieldset><br>
<fieldset>
<legend>Branch:</legend>
<input type="radio" id="branch1" name="branch" value="Computer Science">
<label for="branch1">Computer Science</label><br>
<input type="radio" id="branch2" name="branch" value="Electrical">
<label for="branch2">Electrical</label><br>
<input type="radio" id="branch3" name="branch" value="Mechanical">
<label for="branch3">Mechanical</label>
</fieldset><br>
<input type="submit" value="Submit">
</form>
<script>
document.getElementById('alumniForm').addEventListener('submit', function(event) {
event.preventDefault(); // Prevent form submission
// Validate Name
if (name === '') {
document.getElementById('nameError').textContent = 'Name is required.';
isValid = false;
}
// Validate Email
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailPattern.test(email)) {
document.getElementById('emailError').textContent = 'Please enter a valid email
address.';
isValid = false;
}