css pract answers
css pract answers
If
you get a negative result of subtraction then display an alert box with a message as “Negative
Subtraction Result”.
<html>
<body>
<h3> Calculator </h3>
<br><br>
<script>
function calc(){
</script>
</body>
</html>
Ǫ2
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).
<html>
<body>
<script>
function calcgrade(){
if(marks >=75 ){
alert(" Congratulation u got Distinction ");
}
else if( marks>=60){
alert(" You got First Class");
}
else if(marks>=50){
alert(" You got Second Class");
}
else if(marks>=35){
alert(" Passed ");
}
</script>
</body>
</html>
Ǫ3 Accept the day from the user and display the scheduled meeting of that day in tabular form
using switch statements
<!DOCTYPE html>
<html>
<body>
<button id="btn" onclick="schedule()">Show Schedule</button>
<br><br>
<div id="output"></div>
<script>
let day = Number(prompt("Enter the day (1 for Monday, 2 for Tuesday, ..., 7 for Sunday):"));
let output = "";
function schedule() {
switch (day) {
case 1:
output = "Monday Schedule<br>" +
"<table>" +
"<tr><td>Client 1</td><td>Client 2</td><td>Client 3</td></tr>" +
"<tr><td>9 am</td><td>12 pm</td><td>3 pm</td></tr>" +
"</table>";
break;
case 2:
output = "Tuesday Schedule<br>" +
"<table>" +
"<tr><td>Client 1</td><td>Client 2</td><td>Client 3</td></tr>" +
"<tr><td>9 am</td><td>12 pm</td><td>3 pm</td></tr>" +
"</table>";
break;
case 3:
output = "Wednesday Schedule<br>" +
"<table>" +
"<tr><td>Client 1</td><td>Client 2</td><td>Client 3</td></tr>" +
"<tr><td>9 am</td><td>12 pm</td><td>3 pm</td></tr>" +
"</table>";
break;
case 4:
output = "Thursday Schedule<br>" +
"<table>" +
"<tr><td>Client 1</td><td>Client 2</td><td>Client 3</td></tr>" +
"<tr><td>9 am</td><td>12 pm</td><td>3 pm</td></tr>" +
"</table>";
break;
case 5:
output = "Friday Schedule<br>" +
"<table>" +
"<tr><td>Client 1</td><td>Client 2</td><td>Client 3</td></tr>" +
"<tr><td>9 am</td><td>12 pm</td><td>3 pm</td></tr>" +
"</table>";
break;
case 6:
case 7:
output = "Enjoy your weekend!";
break;
default:
output = "Enter a valid day from 1 to 7.";
}
document.getElementById("output").innerHTML = output;
}
</script>
</body>
</html>
Ǫ4
Write a program
<html>
<body>
<script>
if(a%2==0){
document.write(a+" ");
}
a++;
} while(a<=40);
if(b%2!=0){
document.write(b+" ");
}
b++;
}
</script>
</body>
</html>
Ǫ 5. Write a program to implement array methods for adding and removing elements from an
array. Display the array of elements after every method
<html>
<body>
<div id="output"></div>
<script>
function displayArray(){
document.getElementById("output").innerHTML = arr;
function addAtEnd(){
arr.push(num);
displayArray();
}
function removeFromEnd(){
arr.pop();
displayArray();
function addAtStart(){
let num2 = Number(prompt("Enter number to Add at Start:"));
arr.unshift(num2);
displayArray();
}
function removeFromStart(){
arr.shift();
displayArray();
}
</script>
</body>
</html>
Ǫ7 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
<html>
<body>
<script>
let subjects={
CSS : 22519,
AJP : 22517,
OSY : 22516,
STE : 22518,
EST : 22447
};
document.write(" Value of Subject with Index (SUbject name) using dot syntax: <br>");
document.write(" CSS : "+subjects.CSS +"<br>");
document.write(" AJP : "+subjects.AJP +"<br>");
document.write(" OSY : "+subjects.OSY +"<br>");
document.write(" STE : "+subjects.STE +"<br>");
document.write(" EST : "+subjects.EST +"<br>");
</script>
</body>
</html>
Ǫ8 Write a program to create a function to find out the perfect number between 1 and 100
Perfect number ase number asto jya number che divisors mahnjech as tya number la complete divide
karnare sagle number except that number itself cha sum ani to number jr equal asel tar te perfct
number astat
6 is a perfect number because its divisors (excluding 6) are 1, 2, and 3, and 1+2+3=61 + 2 + 3 =
61+2+3=6.
28 is also a perfect number because 1+2+4+7+14=281 + 2 + 4 + 7 + 14 = 281+2+4+7+14=28.
<html>
<body>
<button id="btn" onclick="perfectNum()"> Click to Display Perfect Numbers from 1 to 100 </button>
<script>
function perfectNum(){
let sum=0; // after every number check sum = 0 as it should chdck for sum of other numbers
to
for(let i=1 ; i<num ; i++){
if(num%i==0){
sum = sum + i;
}
}
if(sum==num){
document.write(num+" ");
}
}
}
</script>
</body>
</html>
Ǫ9 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
<html>
<body>
<script src="jsx.js"> </script>
<script>
muln(2,3,4,5);
</script>
</body>
</html>
Js file:
function muln(a,b,c,d){
console.log(" Multiplication of 2 Numbers: "+(a*b));
console.log(" Multiplication of 3 Numbers: "+(a*b*c));
console.log(" Multiplication of 4 Numbers: "+(a*b*c*d));
}
Ǫ10 Write a program to print sum of digits of user entered value using recursive function.
<html>
<body>
<script>
function calc(){
let num = Number(document.getElementById("num").value);
function sumOfDigit(num){
if(num===0){
return 0;
}
</script>
</body>
</html>
Remember this:
return num % 10 + sumOfDigit(Math.floor(num/10));
Ǫ11 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”)
( string.includes() to check if word exist in string or not and method return true or false
string.indexOf() to find the chrarcter at that index considering index starting from 0
string.charAt() to find the index of the any character we want )
<html>
<body>
<script>
if(likepresent){
document.write("like is present in string");
}
else document.write("like is not present in string");
</script>
</body>
</html>
/body>
</html>
// remember every method or character of string starts from index 0 so if u want to display answer for
normal user add 1 while displaying the character
// space is also conssiderd as a character in string
Ǫ12 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”)
<html>
<body>
<script>
</script>
</body>
</html>
Ǫ13
Ans
<html>
<body>
<style>
.highlight {
background-color: limegreen;
}
</style>
<script>
const str = "I like JavaScript programming";
const newstr = ogstr.replace("JavaScript",` JAVASCRIPT`);
document.write(`<span class="highlight"><b><i>${newstr}</i></b></span>`);
</script>
</body>
</html>
Ǫ14 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)
<html>
<body>
<form>
</form>
<script>
function register(){
if(pass1 != pass2){
alert(" Password Doesnt Match");;
}
else alert("Password MATCHED! Regsiteration sucesfull");
</script>
</body>
</html>
Q.15. Write a JavaScript program to demonstrate form events with output (e.g. focus, blur,
submit, reset, change, input, select)
<html>
<body>
<button type="submit">Submit</button>
<button type="reset">Reset</button>
<p id="output"></p>
</form>
<script>
function handleEvent(eventType,e){
e.preventDefault();
document.getElementById("output").innerHTML=eventType ;
</script>
</body>
</html>
Ǫ16 Write a JavaScript program to demonstrate mouse events with output (e.g. click, dblclick,
mouseup, mousedown, mousemove, mouseout, mouseover)
<html>
<body>
<button type="submit">Submit</button>
<p id="output"></p>
<script>
function showMsg(eventType){
document.getElementById("output").innerHTML=eventType;
}
</script>
</body>
</html>
Ǫ17 Write a JavaScript program to demonstrate window methods with output (e.g. load, unload,
open, close, resize)
<html>
<body>
<script>
let popupWindow;
window.onload = () => {
alert("Window loaded");
};
</body>
</html>
Ǫ18 Write a JavaScript program to demonstrate key events with output (e.g. keyup, keydown,
keypress)
<html>
<body>
function showMsg(str){
document.getElementById("output").innerHTML=str;
</script>
</body>
</html>
Ǫ19 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
<!DOCTYPE html>
<html>
<body>
<h2>Address Form</h2>
<form>
<!-- Middle Name -->
<label for="mname"> Name:</label>
<input type="text" id="myname" name="myname"><br><br>
<script>
function togglePermanentAddress(disable) {
document.getElementById("permAddress").disabled =
disable;
}
</script>
</body>
</html>
Ǫ20Create 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’
<html>
<body>
<form>
function createPersistentCookie(){
let un=document.getElementById("un").value;
document.cookie=`username=${un} ; path=/ ; expires= Wed, 25
Dec 2024 23:23:05 GMT ; Secure ; SameSite=LAX`;
alert("persistent cookie created");
}
</script>
</body>
</html>
Ǫ22Create 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)
<!DOCTYPE html>
<html>
<body>
<h2>Employee Form</h2>
<form onsubmit="return validateForm()">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<button type="submit">Submit</button>
</form>
<script>
function validateForm() {
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 (!nameRegex.test(name)) {
alert("Invalid Name. Use letters and spaces only.");
return false;
}
if (!empIdRegex.test(empId)) {
alert("Invalid Employee ID. Format should be EMP followed by 6 digits (e.g.,
EMP191120).");
return false;
}
if (!emailRegex.test(email)) {
alert("Invalid Email ID.");
return false;
}
if (!mobileRegex.test(mobile)) {
alert("Invalid Mobile Number. Enter a 10-digit number.");
return false;
}
</body>
</html>
Ǫ23 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.
<html>
<body>
<script>
// Update subjects based on selected year
function updateSubjects() {
var subjects = {
SYCO: ['DSU', 'OOP', 'DTE'],
TYCO: ['OSY', 'CSS', 'AJP']
};
if (subjects[year]) {
subjects[year].forEach(function(subject) {
var option = document.createElement('option');
option.text = subject;
subjectSelect.add(option);
});
}
}
</body>
</html>
Ǫ24
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
<html>
<body>
<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, intervalId;
function startSlideshow() {
intervalId = setInterval(nextImage, 3000);
}
function stopSlideshow() {
clearInterval(intervalId);
document.querySelectorAll('button').forEach(b => b.disabled =
false);
}
function nextImage() {
currentIndex = (currentIndex + 1) % images.length;
updateBanner();
}
function prevImage() {
currentIndex = (currentIndex - 1 + images.length) %
images.length;
updateBanner();
}
function updateBanner() {
document.getElementById('bannerImage').src =
images[currentIndex].src;
document.getElementById('prevButton').disabled = currentIndex
=== 0;
document.getElementById('nextButton').disabled = currentIndex
=== images.length - 1;
}
function openLink() {
window.open(images[currentIndex].link, '_blank');
}
startSlideshow();
</script>
</body>
</html>
Ǫ25
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>