FSD-7m
FSD-7m
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Customer Employee Registration Form </title>
</head>
<body>
<h2>Customer Registration</h2>
<form>
<label>Full Name:</label>
<input type="text" name="customer_name" required><br><br>
<label>Email:</label>
<input type="email" name="customer_email" required><br><br>
<label>Phone:</label>
<input type="tel" name="customer_phone" required><br><br>
<label>Address:</label>
<textarea name="customer_address" required></textarea><br><br>
<label>Department:</label>
<input type="text" name="employee_department" required><br><br>
<button type="submit">Register</button>
</form>
</body>
</html>
2. Weather data and display the temperature.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weather App</title>
</head>
<body>
<h2>Current Weather</h2>
<p id="weather">Loading...</p>
<script>
const apiKey = "YOUR_API_KEY";
const city = "Salem"; // Change to your desired city
fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&units=
metric&appid=${apiKey}`)
.then(response => response.json())
.then(data => {
const temperature = data.main.temp;
const condition = data.weather[0].description;
document.getElementById("weather").innerText = `Temperature:
${temperature}°C, Condition: ${condition}`;
})
.catch(error => {
document.getElementById("weather").innerText = "Failed to load
weather data";
console.error("Error fetching weather data:", error);
});
</script>
</body>
</html>
3. Key ES6 features (like arrow functions, destructuring, and template literals)
that can improve React development, and how would refactor an existing
ES5-based component using ES6.
Key ES6 Features for React Development
ES6 (also known as ECMAScript 2015) introduces several features that
improve React development. Some key ones include:
Arrow Functions
● Shorter syntax for functions, reducing boilerplate code.
● Lexical this binding, which eliminates the need for .bind(this) in class
components.
Example:
const greet = name => `Hello, ${name}!`;
console.log(greet("React"));
Destructuring Assignment
● Makes it easy to extract values from arrays or properties from
objects.
● Useful for working with props and state in React components.
Example:
const user = { name: "Alice", age: 25 };
const { name, age } = user;
console.log(name, age);
Template Literals
● Allows embedding expressions inside string templates using backticks
(`).
● Useful for dynamically generating class names or inline styles in JSX.
Example:
const name = "React";
console.log(`Welcome to ${name}!`);
Refactoring an ES5 Component to ES6
Here’s an example of an ES5 React component refactored using ES6
features:
ES5 Version
var React = require('react');
var Greeting = React.createClass({
getInitialState: function() {
return { name: "World" };
},
render: function() {
return React.createElement('h1', null, 'Hello, ' + this.state.name + '!');
}
});
module.exports = Greeting;
2. QueueMicrotask() in JavaScript
queueMicrotask() is used to queue a function in the Microtask Queue,
which executes after the currently executing script but before any
next Macrotask (like setTimeout).
Example:
console.log("Start");
queueMicrotask(() => {
console.log("Inside Microtask Queue");
});
console.log("End");
Output:
Start
End
Inside Microtask Queue
6. Define JavaScript Generators and Parameter Passing.
1. JavaScript Generator Functions
Generator functions in JavaScript allow you to pause and resume
function execution using the yield keyword. They are defined using
an asterisk (*) after the function keyword.
Example:
function* simpleGenerator() {
yield "First";
yield "Second";
yield "Third";
}
Explanation:
gen.next().value starts the function and pauses at yield.
gen.next(5) resumes execution, replacing yield with 5.
gen.next(10) assigns 10 to num2 and returns the sum.
7. JWT Token Generation and Verification in a MERN Stack Application
JSON Web Tokens (JWT) are used for authentication in MERN stack
applications, ensuring secure communication between the client and
server.
1. Installing Dependencies
To generate and verify JWTs in a Node.js (Express) backend, install
the json web token package:
npm install jsonwebtoken dotenv
jsonwebtoken: For creating and verifying tokens.
dotenv: For managing environment variables securely.
2. Generating a JWT Token
In the backend, create an authentication route to generate a JWT:
Back end Code:
const express = require('express');
const jwt = require('jsonwebtoken');
const cors = require('cors');
require('dotenv').config();
const app = express();
app.use(express.json());
app.use(cors());
req.user = decoded;
next();
});
};
4.Frontend (React) Overview for JWT Authentication
The frontend is responsible for:
● User Login → Sends email & password to the backend.
● Storing the JWT Token → Saves the token in localStorage after a
successful login.
● Sending Authenticated Requests → Adds the token in the request
header for protected API calls.
● Handling Responses → Displays a success message if access is granted,
otherwise shows an error.
items.forEach(item => {
const li = document.createElement("li"); // Create <li>
li.textContent = item; // Set text
ul.appendChild(li); // Append <li> to <ul>
});
document.body.appendChild(ul); // Append <ul> to the page
Output:
Apple
Banana
Cherry
Mutation Observer in JavaScript
A Mutation Observer allows tracking changes to the DOM (like node
insertions, removals, or attribute changes). This is useful for dynamically
updating content.
Event Description
window.addEventListener("load", function() {
console.log("Page fully loaded!");
});
window.addEventListener("resize", function() {
console.log("Window resized: ", window.innerWidth, "x",
window.innerHeight);
});
Scroll Event:
window.addEventListener("scroll", function() {
console.log("User scrolled the page!");
});
Detecting Online/Offline Status:
window.addEventListener("online", function() {
console.log("Back online!");
});
window.addEventListener("offline", function() {
console.log("You are offline!");
});
Event Description
Click Event:
document.getElementById("myButton").addEventListener("click",
function() {
alert("Button Clicked!");
});
Keydown Event:
document.addEventListener("keydown", function(event) {
console.log("Key Pressed:", event.key);
});
Mouseover Event:
document.getElementById("myDiv").addEventListener("mouseover",
function() {
this.style.backgroundColor = "yellow";
});
Event Delegation:
document.getElementById("parentList").addEventListener("click",
function(event) {
if (event.target.tagName === "LI") {
alert("List item clicked: " + event.target.textContent);
}
});