CSSX
CSSX
Q1 Accept two values from the user. Perform addition, Subtraction, Multiplication and Division
on it. If you get a negative result of subtraction then display an alert box with a message as
“Negative Subtraction Result”.
Ans <html>
<body>
<h3>Enter two numbers:</h3>
<input type="number" id="num1">
<input type="number" id="num2">
<button onclick="performOperations()">Calculate</button>
<script>
function performOperations()
{
let num1 = parseInt(document.getElementById("num1").value);
let num2 = parseInt(document.getElementById("num2").value);
let add = num1 + num2;
let subtract = num1 - num2;
let multiply = num1 * num2;
let divide = num1 / num2;
alert("Addition: " + add + "\nSubtraction: " + subtract + "\nMultiplication: " + multiply +
"\nDivision: " + divide);
if (subtract < 0)
alert("Negative Subtraction Result");
}
</script>
</body>
</html>
Q2 Accept marks from the user and display his/her grade. Congratulate him/her on the alert
box if he/she got distinction (Use if-else-if).
Ans <html>
<body>
<h3>Enter your marks:</h3>
<input type="number" id="marks">
<button onclick="calculateGrade()">Get Grade</button>
<script>
function calculateGrade()
{
let marks = parseInt(document.getElementById("marks").value);
let grade;
Q3 Accept the day from the user and display the scheduled meeting of that day in tabular form
using switch statements
Ans <html>
<head>
<script>
let day = parseInt(prompt("Enter a day:"));
switch (day)
{
case 1:
document.write("Monday Schedule<br>");
document.write("<table> <tr> <td> Client 1 </td> <td> Client 2 </td>
<td> Client 3 </td> </tr> <tr><td>9:00 am</td><td>12:00 pm</td><td>4:00
pm</td></tr></table>");
break;
case 2:
document.write("Tuesday Schedule<br>");
document.write("<table><tr><td>Client 1</td><td>Client
2</td><td>Client 3</td></tr><tr><td>9:00 am</td><td>12:00 pm</td><td>4:00
pm</td></tr></table>");
break;
case 3:
document.write("Wednesday Schedule<br>");
document.write("<table><tr><td>Client 1</td><td>Client
2</td><td>Client 3</td></tr><tr><td>9:00 am</td><td>12:00 pm</td><td>4:00
pm</td></tr></table>");
break;
case 4:
document.write("Thursday Schedule<br>");
document.write("<table><tr><td>Client 1</td><td>Client
2</td><td>Client 3</td></tr><tr><td>9:00 am</td><td>12:00 pm</td><td>4:00
pm</td></tr></table>");
break;
case 5:
document.write("Friday Schedule<br>");
document.write("<table><tr><td>Client 1</td><td>Client
2</td><td>Client 3</td></tr><tr><td>9:00 am</td><td>12:00 pm</td><td>4:00
pm</td></tr></table>");
break;
case 6:
document.write("<br>Enjoy Your Weekend<br>");
break;
case 7:
document.write("<br>Enjoy Your Weekend<br>");
break;
default:
document.write("Provide a valid Input.");
}
</script>
</head>
<body>
</body>
</html>
Q4 Write a program
Q6 Write a program to combine three different arrays into one array and display it and also
display another array with “–“between every element
Ans <html>
<body>
<script>
const array1 = [1, 2, 3];
const array2 = [4.5, 6.7, 8.9];
const array3 = ["a", "b", "c"];
var result = array1.concat(array2, array3);
document.write(result.join("-"));
</script>
</body>
</html>
Q7 Write a program to set the index of the array as CSS, AJP, OSY, STE, EST and values for this
index as 22519, 22517, 22516, 22518 and 22447 respectively. And display array elements
with index using dot syntax
Ans <html>
<body>
<script>
const values =
{
CSS: 22519,
AJP: 22517,
OSY: 22516,
STE: 22518,
EST: 22447
};
document.write("CSS: "+values.CSS +"<br>");
document.write("AJP: "+values.AJP +"<br>");
document.write("OSY: "+values.OSY +"<br>");
document.write("STE: "+values.STE +"<br>");
document.write("EST: "+values.EST +"<br>");
</script>
</body>
</html>
Q8 Write a program to create a function to find out the perfect number between 1 and 100
Ans <html>
<body>
<script>
function findPerfectNumbersInRange() {
for (let num = 1; num <= 100; num++) {
let sum = 0;
for (let i = 1; i < num; i++) {
if (num % i === 0) {
sum += i;
}
}
if (sum === num) {
console.log(num + " is a perfect number.");
}
}
}
findPerfectNumbersInRange();
</script>
</body>
</html>
Q9 Write a program to create one function to multiply 2 numbers, 3 numbers, and 4 numbers.
Write function definition in external JavaScript file and call this function from html file
Ans <html>
<body>
<script src="mullt.js">
</script>
<script>
mul(2,3,4,5);
</script>
</body>
</html>
--- js file---
function mul(a,b,c,d)
{
console.log("Ans of "+a+" * "+b+" = "+(a*b));
console.log("Ans of "+a+" * "+b+" * "+c+" = "+(a*b*c));
console.log("Ans of "+a+" * "+b+" * "+c+" * "+d+" = "+(a*b*c*d));
}
Q10 Write a program to print sum of digits of user entered value using recursive function.
Ans <html>
<body>
<h1>Sum of Digits Calculator</h1>
let sum = 0;
while (num > 0) {
sum += num % 10;
num = Math.floor(num / 10);
}
document.getElementById("result").textContent = sum;
}
</script>
</body>
</html>
Q11 Write a program to retrieve characters from position 3, 15, 8 and retrieve position of
character J, p, a, t. Search for ‘like’ and display results. (Given string = “I like JavaScript
programming”)
Ans <html>
<body>
<script>
let s = "I like JavaScript programming";
console.log("Given String : ",s);
let c3 = s.charAt(3);
let c15 = s.charAt(15);
let c8 = s.charAt(8);
let pJ = s.indexOf('J');
let pP = s.indexOf('p');
let pA = s.indexOf('a');
let pT = s.indexOf('t');
if(s.includes('like')){
console.log("'like' word does exist in given string .");
}else{
console.log("'like' word don't exist in given string .");
}
</script>
</body>
</html>
Q12 Write a program to replace “like” by “know” and divide the string on the basis of space from
the 7th index. (Given string = “I like JavaScript programming”)
Ans <html>
<body>
<script>
let s = "I like JavaScript programming";
console.log("Given String : ",s);
<script>
const ogstr = "I like JavaScript programming";
const highstr = ogstr.replace("JavaScript",` JAVASCRIPT`);
document.write(`<span class="highlight"><b><i>${highstr}</i></b></span>`);
</script>
</body>
</html>
Q14 Create an employee registration form with fields ‘name’, ‘Employee-ID’, ‘Email’, ‘Password’,
‘Confirm Password’ and 'Register' button. When the user clicks on the ‘Register’ button it
checks whether ‘Password’ and ‘Confirm Password’ have the same values. If the same then
display a message as “Registration is successful”, else display “Passwords don’t match”
(Add output for both conditions)
Ans <html>
<body>
<script>
function register(e) {
e.preventDefault();
let pass = document.getElementById("pwd").value;
let confirmPass = document.getElementById("cpwd").value;
</body>
</html>
Q15 Write a JavaScript program to demonstrate form events with output (e.g. focus, blur,
submit, reset, change, input, select)
Ans
<html >
<body>
<label for="name">Name:</label>
<input type="text" id="name" onfocus="handleEvent('focus' ,event)"
onblur="handleEvent('blur',event)" oninput="handleEvent('input' ,event)"
onselect="handleEvent('select',event)" onchange="handleEvent('change',event)">
<br><br>
<button type="submit">Submit</button>
<button type="reset">Reset</button>
</form>
<script>
function handleEvent(eventType, e) {
e.preventDefault();
console.log(`Event: ${eventType}`);
}
</script>
</body>
</html>
Q16 Write a JavaScript program to demonstrate mouse events with output (e.g. click, dblclick,
mouseup, mousedown, mousemove, mouseout, mouseover)
Ans <html>
<body>
<label>click me:</label>
<input type="text"
onclick="showMessage('Click')"
ondblclick="showMessage('Double Click')"
onmousemove="showMessage('Mouse Move')"
onmouseout="showMessage('Mouse Out')">
<br><br>
<label>click me:</label>
<input type="text"
onmousedown="showMessage('Mouse Down')"
onmouseup="showMessage('Mouse Up')"
onmouseover="showMessage('Mouse Over')" >
<br><br>
<p id="output"></p>
<script>
function showMessage(message) {
document.getElementById('output').textContent = message;
}
</script>
</body>
</html>
Q17 Write a JavaScript program to demonstrate window methods with output (e.g. load, unload,
open, close, resize)
Ans <html>
<body>
<h1>Window Methods Demo</h1>
<button onclick="openWindow()">Open Pop-up Window</button>
<button onclick="closeWindow()">Close Pop-up Window</button>
<script>
let popupWindow;
function openWindow() {
popupWindow = window.open("", "newWindow", "width=400,height=300");
popupWindow.document.write("<p>This is a new pop-up window!</p>");
}
function closeWindow() {
popupWindow?.close();
}
window.onresize = () => {
console.log(`Window resized to: ${window.innerWidth} x ${window.innerHeight}`);
alert("Window Resized");
};
</script>
</body>
</html>
Q18 Write a JavaScript program to demonstrate key events with output (e.g. keyup, keydown,
keypress)
ans
<html >
<head>
<title>Key Event Demo</title>
</head>
<body>
<div id="OP"></div>
<script>
function logEvent(eventType, key, keyCode) {
const output = document.getElementById('OP');
const message = document.createElement('p');
message.textContent = `${eventType} event triggered - Key: ${key} (Code:
${keyCode})`;
output.appendChild(message);
}
document.addEventListener('keydown', function(event) {
logEvent('keydown', event.key, event.keyCode);
});
document.addEventListener('keyup', function(event) {
logEvent('keyup', event.key, event.keyCode);
});
document.addEventListener('keypress', function(event) {
logEvent('keypress', event.key, event.keyCode);
});
</script>
</body>
</html>
Q19 Create the following form. Make the first text field read only. When the user clicks on the
YES radio button, disable the Permanent Address input element
Ans <html>
<body>
<h2>Disable and Read-Only Elements</h2>
<form>
<input type="text" value="This Form is to demonstrate disable and read only elements"
readonly>
<br><br>
Name:
<input type="text"><br><br>
Temporary Address:
<textarea id="temp_address"></textarea><br><br>
Permanent Address:
<textarea id="perm_address"></textarea><br><br>
Are both addresses same?
<input type="radio" id="yes"> YES
<input type="radio" id="no"> NO<br><br>
</form>
<script>
document.getElementById('yes').addEventListener('change', () => {
const tempAddress = document.getElementById('temp_address').value;
const permAddress = document.getElementById('perm_address');
if (document.getElementById('yes').checked) {
permAddress.value = tempAddress;
permAddress.disabled = true;
}
});
document.getElementById('no').addEventListener('change', () => {
const permAddress = document.getElementById('perm_address');
if (document.getElementById('no').checked) {
permAddress.value = '';
permAddress.disabled = false;
}
});
</script>
</body>
</html>
Q20 Create a form containing name and username as input fields. Form should contain two
buttons as ‘Set Session Cookie’ and ‘Set Persistent Cookie’. When the user clicks on ‘Set
session Cookie’, create a session cookie for ‘name’ and when the user clicks on ‘Set
Persistent Cookie’, create a persistent cookie for ‘username’
Ans <html>
<body>
<h2>Cookie Form</h2>
<form>
<label>Name:</label>
<input type="text" id="name"><br><br>
<label>Username:</label>
<input type="text" id="username"><br><br>
<button type="button" onclick="setSessionCookie()">Set Session Cookie</button>
<button type="button" onclick="setPersistentCookie()">Set Persistent Cookie</button>
</form>
<script>
function setSessionCookie() {
const name = document.getElementById('name').value;
document.cookie = `name=${name}; path=/`;
alert("Session cookie set for name!");
}
function setPersistentCookie() {
const username = document.getElementById('username').value;
const expires = new Date();
expires.setTime(expires.getTime() + (30*24*60*60*1000)); // 30 days
document.cookie = `username=${username}; expires=${expires.toUTCString()};
path=/`;
alert("Persistent cookie set for username!");
}
</script>
</body>
</html>
Q21 Write javascript code to demonstrate setTimeout(), setInterval() functions of window object
Ans <html>
<body>
<script>
function startDemo() {
</body>
</html>
Q22 Create a form with input fields as Name, Employee Id, Email Id, Mobile Number and one
submit button. When the user clicks on the submit button, validate all data entered by
using regular expressions. (Note: Format of Employee Id is EMP191120)
Ans <html>
<body>
<form id="myForm">
<input placeholder="Name" id="name" required><br><br>
<input placeholder="Employee ID" id="empId" required><br><br>
<input placeholder="Email ID" id="email" required><br><br>
<input placeholder="Mobile Number" id="mobile" required><br><br>
<button type="submit">Submit</button>
</form>
<script>
document.getElementById('myForm').onsubmit = function(e) {
e.preventDefault();
const name = document.getElementById('name').value;
const empId = document.getElementById('empId').value;
const email = document.getElementById('email').value;
const mobile = document.getElementById('mobile').value;
if (!name) {
alert(' enter a valid name.');
} else if (!empIdPattern.test(empId)) {
alert('Invalid EmployeeID format.');
} else if (!emailPattern.test(email)) {
alert('Invalid email format.');
} else if (!mobilePattern.test(mobile)) {
alert('Mobile number must be 10 digits.');
} else {
alert('Form submitted successfully!');
}
};
</script>
</body>
</html>
Q23 Create two drop down menus. In the first menu create options as SYCO and TYCO. In the
second menu create a subject list. Change options of second menu dynamically according
to option selected in first menu (e.g When user selects SYCO then display DSU, OOP, DTE,
CGR, DMS and when user selects TYCO then display OSY, CSS, AJP, STE, EST). Form should
contain one button for submit. When the user clicks on the submit button then displays the
selected option.
Ans <html>
<body>
<h1>Dynamic Dropdown Menu</h1>
<form>
<label>Select Year:</label>
<select id="year" onchange="updateSubjects()">
<option value="">--Select--</option>
<option value="SYCO">SYCO</option>
<option value="TYCO">TYCO</option>
</select><br><br>
<label>Select Subject:</label>
<select id="subject">
<option value="">--Select--</option>
</select><br><br>
function submitForm() {
const year = document.getElementById('year').value;
const subject = document.getElementById('subject').value;
alert(`Selected Year: ${year}\nSelected Subject: ${subject}`);
}
</script>
</body>
</html>
Q24 Write a JavaScript program to create a rotating banner of five images. When a user clicks on
any image then open a link for that advertisement. Web Page should contain three buttons
as Stop slideshow, Previous and Next. Initially Previous and Next buttons should be
disabled, when user clicks on Stop slideshow buttons then Previous and Next buttons
should be enabled and slideshow will be going on using these buttons
Ans <html>
<body>
<div class="banner">
<img id="bannerImage" src="chip1.png" onclick="openLink()">
</div>
<div class="controls">
<button id="stopButton" onclick="stopSlideshow()">Stop Slideshow</button>
<button id="prevButton" onclick="prevImage()">Previous</button>
<button id="nextButton" onclick="nextImage()">Next</button>
</div>
<script>
const images = [
{ src: 'chip1.png', link: 'http://example.com/ad1' },
{ src: 'chip2.png', link: 'http://example.com/ad2' },
{ src: 'c1.jpeg', link: 'http://example.com/ad3' },
{ src: 'c2.jpeg', link: 'http://example.com/ad4' },
{ src: 'c3.jpeg', link: 'http://example.com/ad5' }
];
let currentIndex = 0;
let intervalId;
function startSlideshow() {
intervalId = setInterval(nextImage, 3000);
}
function stopSlideshow() {
clearInterval(intervalId);
}
function nextImage() {
currentIndex = (currentIndex + 1) % images.length;
updateBanner();
}
function prevImage() {
currentIndex = (currentIndex - 1 + images.length) % images.length;
updateBanner();
}
function updateBanner() {
bannerImage.src = images[currentIndex].src;
bannerImage.onload = () => console.log('Image loaded:', bannerImage.src);
bannerImage.onerror = () => bannerImage.src = '';
prevButton.disabled = currentIndex === 0;
nextButton.disabled = currentIndex === images.length - 1;
}
function openLink() {
window.open(images[currentIndex].link, '_blank');
}
startSlideshow();
</script>
</body>
</html>
Q25 Create the following frame. When the user clicks on the Frame 2 link then content of Frame
2 should be displayed in Frame 1. When User clicks on the Frame 3 link then redirect Frame
3 to google page
Ans <html>
<frameset cols="50%, 50%">
<frame name="frame1" src="frame1.html">
<frameset rows="50%, 50%">
<frame name="frame2" src="frame2.html">
<frame name="frame3" src="frame3.html">
</frameset>
</frameset>
@suppliers