JS Practice
JS Practice
Manipulation
Question: How can a JavaScript program take input from the user?
Answer: JavaScript uses the prompt() function to take user input. Example:
let name = prompt("Enter your name:");
alert("Hello, " + name);
3. Arithmetic Operations
Question: List the basic arithmetic operations in JavaScript and provide an example.
Answer: The basic arithmetic operations are Addition (+), Subtraction (-),
Multiplication (*), Division (/), and Modulus (%). Example:
let sum = 5 + 3;
console.log("Sum: " + sum);
4. Control Statements
5. Looping Statements
Question: What are the differences between while, for, and do...while loops?
Answer:
o while loop runs as long as the condition is true.
o for loop is used when the number of iterations is known.
o do...while runs at least once before checking the condition.
6. Switch Statement
8. Logical Operators
Question: Explain load, mousemove, and form submit events with examples.
Answer:
window.onload = () => console.log("Page loaded");
document.addEventListener("mousemove", (event) =>
console.log(event.clientX, event.clientY));
document.getElementById("form").addEventListener("submit", (event) =>
event.preventDefault());
// Using document.write
document.write("Hello World");
// Using console.log
console.log("Hello World");
// Using alert
alert("Hello World");
3. Arithmetic Operations
let sum = 5 + 3;
console.log("Sum: " + sum);
let diff = 10 - 4;
console.log("Difference: " + diff);
let prod = 6 * 7;
console.log("Product: " + prod);
let div = 20 / 4;
console.log("Division: " + div);
let mod = 10 % 3;
console.log("Modulus: " + mod);
4. Control Statements
5. Looping Statements
// While loop
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
// For loop
for (let i = 0; i < 5; i++) {
console.log(i);
}
// Do...while loop
let j = 0;
do {
console.log(j);
j++;
} while (j < 5);
6. Switch Statement
let day = 2;
switch(day) {
case 1: console.log("Monday"); break;
case 2: console.log("Tuesday"); break;
case 3: console.log("Wednesday"); break;
default: console.log("Other day");
}
// Continue Example
for (let i = 0; i < 10; i++) {
if (i === 5) continue;
console.log(i);
}
8. Logical Operators
function localScope() {
// Local Scope
let localVar = "I am local";
console.log(localVar);
}
localScope();
console.log(globalVar); // Accessible
// console.log(localVar); // Error: localVar is not defined
// DOM Object
let element = document.getElementById("myElement");
console.log(element);
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-
scale=1.0" />
<title>Number Checker</title>
</head>
<body>
<script>
// Get a number from the user
let num = prompt("Enter a number:");
</body>
</html>
Event Handling in Java script
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<form id="form">
<label for="name">Name:</label>
<button type="submit">Submit</button>
</form>
<script>
// Load event
// Mousemove event
});
// Form submit event
event.preventDefault();
});
</script>
</body>
</html>
The load event, which logs a message when the page is fully loaded.
The mousemove event, which logs the mouse coordinates in the console.
The submit event, which prevents the form from submitting and logs a message.
Output:
DOM manipulation
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>DOM Manipulation Example</title>
</head>
<body>
<h1>DOM Manipulation</h1>
<p id="message">Original Content</p>
<button onclick="changeContent()">Change Content</button>
<script>
function changeContent() {
document.getElementById("message").innerHTML = "Content
Changed!";
}
</script>
</body>
</html>
Output
Event Bubbling
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Event Bubbling Example</title>
<style>
#outerDiv {
width: 300px;
height: 200px;
background-color: lightblue;
display: flex;
align-items: center;
justify-content: center;
}
#innerDiv {
width: 150px;
height: 100px;
background-color: lightcoral;
text-align: center;
line-height: 100px;
}
</style>
</head>
<body>
<h1>Event Bubbling Example</h1>
<div id="outerDiv">
<div id="innerDiv">Click Me</div>
</div>
<script>
// Event Bubbling example
document.getElementById("outerDiv").addEventListener("click",
() => {
alert("Outer div clicked");
});
document.getElementById("innerDiv").addEventListener("click",
(event) => {
alert("Inner div clicked");
event.stopPropagation(); // Stops the bubbling
});
</script>
</body>
</html>