Lab-JS
Lab-JS
24MCA0201
JavaScript Practice Questions
1. Design a web page which display digital clock as shown below.
<!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>
<style>
#clock {
font-size: 24px;
font-family: Arial, sans-serif;
color: #333;
background-color: yellow;
padding: 15px;
border-radius: 10px;
text-align: center;
display: inline-block;
margin-top: 20px;
}
body {
font-family: Arial, sans-serif;
margin-top: 50px;
}
</style>
</head>
<body>
<h1>Digital Clock</h1>
<div id="clock"></div>
<script>
function updateClock() {
const now = new Date();
let hours = now.getHours();
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
const amPm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12 || 12;
const timeString = `${hours}:${minutes}:${seconds} ${amPm}`;
document.getElementById('clock').textContent = timeString;
}
updateClock();
setInterval(updateClock, 1000);
</script>
</body>
</html>
2. Design form which contains text box and button. Once you click the
button the message is displayed in the header tag, the text message
whatever entered by the user in the textbox.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display Message</title>
<style>
#output {
margin-top: 10px;
font-size: 18px;
color: #333;
}
</style>
</head>
<body>
<form action="" method="post" onsubmit="displayMessage(event)">
<label for="txt">Write Text:</label>
<input type="text" id="txt" name="txt" placeholder="Enter your
message here"><br><br>
<button type="submit">Submit</button>
</form>
<div id="output"></div>
<script>
function displayMessage(event) {
event.preventDefault();
const userInput = document.getElementById('txt').value;
const output = document.getElementById('output');
if (userInput.trim() !== "") {
output.textContent = `Your message: ${userInput}`;
output.style.color = "green";
} else {
output.textContent = "Please enter a message!";
output.style.color = "red";
}
}
</script>
</body>
</html>
3. Design a web page which contains three buttons with labelled as red,
blue and green. On clicking the button, the respective color should
display as background color.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Change Background Colours</title>
<style>
body {
margin: 0;
font-family: Arial, sans-serif;
display: flex;
height: 100vh;
}
button {
margin: 10px;
padding: 15px 30px;
font-size: 16px;
border: none;
cursor: pointer;
border-radius: 5px;
}
.red {
background-color: white;
color: red;
}
.blue {
background-color: whitesmoke;
color: blue;
}
.green {
background-color: wheat;
color: green;
}
</style>
</head>
<body>
<div>
<button class="red" onclick="changeBgColour('red')">Red</button>
<button class="blue" onclick="changeBgColour('blue')">Blue</button>
<button class="green"
onclick="changeBgColour('green')">Green</button>
</div>
<script>
function changeBgColour(color)
{
document.body.style.backgroundColor = color;
}
</script>
</body>
</html>
.slideshow-container {
max-width: 600px;
margin: 50px auto;
position: relative;
}
img {
width: 100%;
border-radius: 10px;
display: none;
}
img.active {
display: block;
}
.controls {
margin-top: 20px;
}
button {
padding: 10px 20px;
margin: 5px;
border: none;
border-radius: 5px;
background-color: #007BFF;
color: white;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<h1>Image Slideshow</h1>
<button onclick="startSlideshow()">Start Slideshow</button>
<div class="slideshow-container">
<img src="2_1.jpg" alt="Image 1" class="active">
<img src="2_2.jpg" alt="Image 2">
<img src="2_3.jpg" alt="Image 3">
<img src="sad.png" alt="Image 4">
<img src="smile.jpg" alt="Image 5">
</div>
<div class="controls">
<button onclick="prevImage()"><<</button>
<button onclick="nextImage()">>></button>
</div>
<script>
const images = document.querySelectorAll('.slideshow-container
img');
let currentIndex = 0;
let slideshowInterval;
function showImage(index) {
images.forEach((img, i) => {
img.classList.toggle('active', i === index);
});
}
function nextImage() {
currentIndex = (currentIndex + 1) % images.length;
showImage(currentIndex);
}
function prevImage() {
currentIndex = (currentIndex - 1 + images.length) %
images.length;
showImage(currentIndex);
}
function startSlideshow() {
clearInterval(slideshowInterval);
slideshowInterval = setInterval(nextImage, 5000);
}
</script>
</body>
</html>
5. Develop an Online Greetings Designer using JavaScript and CSS.
a. Add options to
i. change the image
ii. Position the image (left, background, right)
iii. Edit text
iv. Change font size
v. Change font color
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Greetings Designer</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f4f4f4;
}
.container {
width: 600px;
padding: 20px;
background: #fff;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
.controls {
display: flex;
flex-direction: column;
gap: 10px;
}
.greeting-card {
width: 100%;
height: 300px;
margin-top: 20px;
border: 1px solid #ccc;
position: relative;
overflow: hidden;
text-align: center;
background-size: cover;
background-position: center;
}
.greeting-text {
position: absolute;
width: 100%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 24px;
color: black;
}
input, select, button {
padding: 10px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
background-color: #007BFF;
color: white;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<h2>Greetings Designer</h2>
<div class="controls">
<label for="imageSelector">Change Image:</label>
<select id="imageSelector">
<option value="" id="changeImage1">Image 1</option>
<option value="" id="changeImage2">Image 2</option>
<option value="" id="changeImage3">Image 3</option>
</select>
<script>
function updateGreetingCard()
{
const imageSelector = document.getElementById('imageSelector');
const imagePosition = document.getElementById('imagePosition');
const greetingText = document.getElementById('greetingText');
const fontSize = document.getElementById('fontSize');
const fontColor = document.getElementById('fontColor');
const greetingCard = document.getElementById('greetingCard');
const greetingTextDisplay =
document.getElementById('greetingTextDisplay');
greetingCard.style.backgroundImage =
`url('${imageSelector.value}')`;
greetingCard.style.backgroundPosition = imagePosition.value;
greetingTextDisplay.textContent = greetingText.value;
greetingTextDisplay.style.fontSize = `${fontSize.value}px`;
greetingTextDisplay.style.color = fontColor.value;
}
</script>
</body>
</html>
<script>
function generateResume() {
const name = document.getElementById('name').value;
const email = document.getElementById('email').value;
const phone = document.getElementById('phone').value;
const address = document.getElementById('address').value;
const education = document.getElementById('education').value;
const experience = document.getElementById('experience').value;
<script>
let totalReceipts = 0;
function calculateCharges() {
const hours =
parseFloat(document.getElementById('hoursParked').value);
const resultDiv = document.getElementById('result');
const totalReceiptsDiv =
document.getElementById('totalReceipts');
<script>
function validateCard() {
const cardNumber =
document.getElementById("cardNumber").value.trim();
const cardType = document.getElementById("credit_card").value;
const result = document.getElementById("result");
const patterns = {
"American Express": /^3[47][0-9]{13}$/,
"Diners Club": /^3(?:0[0-5]|[68])[0-9]{11}$/,
"Discover": /^6011[0-9]{12}|65[0-9]{14}$/,
"JCB": /^(?:2131|1800)[0-9]{11}$|^35[0-9]{14}$/,
"MasterCard": /^5[1-5][0-9]{14}$/,
"Visa": /^4(?:[0-9]{12}|[0-9]{15})$/
};
if (patterns[cardType].test(cardNumber)) {
result.textContent = `Valid: ${cardType}`;
result.style.color = "green";
} else {
result.textContent = "Invalid credit card number";
result.style.color = "red";
}
}
</script>
</body>
</html>
<script>
function encryptData() {
const input = document.getElementById('inputNumber').value;
const resultDiv = document.getElementById('result');
if (!/^[0-9]{4}$/.test(input)) {
resultDiv.textContent = 'Please enter a valid 4-digit
number.';
resultDiv.style.color = 'red';
return;
}
let digits = input.split('').map(Number);
<script>
document.getElementById('grievanceForm').addEventListener('submit',
function(event) {
event.preventDefault();
if (!grievance) {
const noGrievanceMessage = document.createElement('div');
noGrievanceMessage.className = 'no-grievance';
noGrievanceMessage.textContent = 'No grievances';
result.appendChild(noGrievanceMessage);
return;
}