Q1.
Compare GET & POST Method
HTTP GET HTTP POST
In GET method we can not send large
amount of data rather limited data of In POST method large amount of data can be
some number of characters is sent sent because the request parameter is
because the request parameter is appended into the body.
appended into the URL.
GET request is comparatively better than POST request is comparatively less better
Post so it is used more than the Post than Get method, so it is used less than the
request. Get request.
GET requests are only used to request POST requests can be used to create and
data (not modify) modify data.
GET request is comparatively less secure POST request is comparatively more secure
because the data is exposed in the URL because the data is not exposed in the URL
bar. bar.
Request made through GET method are Request made through POST method is not
stored in Browser history. stored in Browser history.
GET method request can be saved as POST method request can not be saved as
bookmark in browser. bookmark in browser.
Request made through GET method are Request made through POST method are not
stored in cache memory of Browser. stored in cache memory of Browser.
Data passed through GET method can be
easily stolen by attackers as the data is Data passed through POST method can not
visible to [Link] requests should be easily stolen by attackers as the URL Data
never be used when dealing with is not displayed in the URL
sensitive data
In GET method only ASCII characters
In POST method all types of data is allowed.
are allowed.
In POSTmethod, the encoding type
In GET method, the Encoding type is application/x-www-form-urlencoded or mu
is application/x-www-form-urlencoded ltipart/form-data. Use multipart encoding for
binary data
Q4. Describe Event Handling in Java Script .
Event handling in JavaScript is a crucial concept that allows
developers to create interactive web applications by responding to
user actions or other events that occur in the browser. Here's a
detailed overview:
1. What are Events?
Events are actions or occurrences that happen in the browser, which
can be triggered by the user (like clicks, keyboard input, mouse
movements) or by the browser itself (like page loading, resizing, etc.).
JavaScript can listen for these events and execute specific code when
they occur.
2. Types of Events
JavaScript supports a wide range of events, including:
Mouse Events: click, dblclick, mouseover, mouseout,
mousemove, etc.
Keyboard Events: keydown, keyup, keypress.
Form Events: submit, change, focus, blur.
Window Events: load, resize, scroll, unload.
3. Event Listeners
To respond to events, JavaScript uses event listeners. An event
listener is a function that waits for a specific event to occur on a
particular element. You can attach an event listener to an element
using the addEventListener method.
Syntax:
[Link](event, function, useCapture);
event: A string that specifies the event to listen for (e.g., 'click').
function: The function to be executed when the event occurs.
useCapture (optional): A boolean that specifies whether the
event should be captured in the capturing phase or the bubbling
phase (default is false).
Example:
javascript
Copy code
const button = [Link]('myButton');
[Link]('click', function() {
alert('Button was clicked!');
});
Q5. List & Explain windows Event. with example.
At the browser level, window events happen and hold association with the window object; this
global object represents the web browser's window. Frequently employed to oversee the overall
state of a browser window or manage global interactions are these types of events.
Event Name Description
load Triggered when the entire web page, including all its resources, has finished loading.
unload Fired when the user is leaving the page or closing the browser window or tab.
resize Activated when the size of the browser window is changed.
scroll Fired when the user scrolls the page.
Example: Demonstrating Window Events
<!DOCTYPE html>
<html>
<head>
<title>Window Events Example</title>
<style>
body {
height: 2000px; /* Adding some content just to enable scrolling */
}
#resizeInfo {
position: fixed;
top: 10px;
left: 10px;
background-color: #fff;
padding: 10px;
border: 1px solid #ccc;
}
</style>
<script>
[Link]('load', function() {
var initialSizeInfo = 'Initial window size: ' + [Link] + ' x ' + [Link];
[Link]('resizeInfo').innerText = initialSizeInfo;
alert('The page has finished loading!');
});
[Link]('resize', function() {
var newSizeInfo = 'New window size: ' + [Link] + ' x ' + [Link];
[Link]('resizeInfo').innerText = newSizeInfo;
alert("Page has been resized");
});
[Link]('scroll', function() {
alert('You have scrolled on this page.');
},{once:true});
</script>
</head>
<body>
<div id="resizeInfo">Initial window size: ${[Link]} x ${[Link]}</div>
</body>
</html>
QWrite a simple program in JavaScript to validate the email-
id.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Email Validation</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 50px;
}
.error {
color: red;
}
</style>
</head>
<body>
<h2>Email Validation</h2>
<form id="emailForm">
<label for="email">Enter your email:</label>
<input type="text" id="email" required>
<button type="submit">Submit</button>
</form>
<p id="message" class="error"></p>
<script>
// Email validation function
function validateEmail(email) {
// Regular expression for validating email
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return [Link](email);
}
// Form submission event listener
[Link]('emailForm').addEventListener('submit', function(event) {
[Link](); // Prevent form submission
const emailInput = [Link]('email').value;
const message = [Link]('message');
// Validate the email
if (validateEmail(emailInput)) {
[Link] = 'green';
[Link] = 'Email is valid!';
} else {
[Link] = 'red';
[Link] = 'Please enter a valid email address.';
}
});
</script>
</body>
</html>
Q7 Analyze a JavaScript to find factorial of a given number
Finding the factorial of a given number in JavaScript can be achieved through both iterative
and recursive approaches. Below, I will provide a detailed analysis of both methods.
What is Factorial?
The factorial of a non-negative integer nnn is the product of all positive integers less than or
equal to nnn. It is denoted by n!n!n!.
For example:
o 5!=5×4×3×2×1=120
o 0!=1
1. Iterative Approach
The iterative method uses a loop to calculate the factorial.
JavaScript Code
function factorialIterative(n) {
if (n < 0) return "Factorial is not defined for negative numbers.";
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i; // Multiply result by the current number
}
return result; // Return the final result
}
// Example usage
[Link](factorialIterative(5)); // Output: 120
Analysis
Time Complexity: O(n)O(n)O(n) – The loop runs n−1n-1n−1 times.
Space Complexity: O(1)O(1)O(1) – Only a constant amount of space is used for
variables.