WEB DEVOLPMENT
WEB DEVOLPMENT
22951A6255
1.Build an HTML page to form a table to show the below values in a tabular
form with heading as Roll No., Student name, Subject Name, and values as Ram,
Physics Shyam, Math Murli, Chemistry
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Student Table</title>
<style>
table {
width: 50%;
border-collapse: collapse;
margin: 20px 0;
th, td {
text-align: left;
th {
background-color: #f2f2f2;
td {
text-align: center;
</style>
</head>
<body>
<h2>Student Information</h2>
<table>
<thead>
<tr>
<th>Roll No.</th>
<th>Student Name</th>
<th>Subject Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Ram</td>
<td>Physics</td>
</tr>
<tr>
<td>2</td>
<td>Shyam</td>
<td>Math</td>
</tr>
<tr>
<td>3</td>
<td>Murli</td>
<td>Chemistry</td>
</tr>
</tbody>
</table>
</body>
</html>
2.Constract the following input components in HTML forms with proper syntax
of the corresponding HTML tags. a) Text Input b) Selectable list with multiple
selection option c) Radio Buttons
a) Text Input
html
Copy code
<label for="name">Name:</label>
The <select> tag is used to create dropdown lists, and the multiple attribute
allows multiple selections.
html
Copy code
<option value="math">Math</option>
<option value="chemistry">Chemistry</option>
<option value="biology">Biology</option>
</select>
c) Radio Buttons
The <input> tag with type="radio" is used to create radio buttons. Radio
buttons are grouped by using the same name attribute.
html
Copy code
<label for="male">Male</label><br>
<label for="female">Female</label><br>
<label for="other">Other</label>
Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<form>
<label for="name">Name:</label>
<br><br>
<option value="math">Math</option>
<option value="chemistry">Chemistry</option>
<option value="biology">Biology</option>
</select>
<br><br>
<label for="male">Male</label><br>
<label for="female">Female</label><br>
<label for="other">Other</label>
<br><br>
</form>
</body>
</html>
Explanation:
Text Input: The user can enter a name using the text input field.
Selectable List (Multiple Selection): The user can select multiple subjects from a
list.
Radio Buttons: The user can select one gender from the available options.
This form can be used as a starting point to gather user input. When submitted,
the form data will be sent to a server (if action and method attributes are
defined on the <form> tag).
Bootstrap uses a 12-column grid layout, meaning the screen space is divided
into 12 equal-width columns. The content inside the grid is placed within these
columns, and you can control how many columns each element should span.
The grid system is based on containers, rows, and columns. Here's a breakdown
of each:
Container: A wrapper for the grid system. Containers are used to hold the grid
and align the content within it. There are two types of containers:
Row: A horizontal group of columns that are used to create a new line of
content.
Column: The individual content blocks within the grid. Columns are placed
inside rows. The width of the column is specified by the number of columns it
should span out of the 12 available columns.
Containers
html
Copy code
<div class="container">
</div>
Rows
Inside the container, you create a row with the .row class. Rows are used to
group columns.
html
Copy code
<div class="container">
<div class="row">
</div>
</div>
Columns
Inside the row, you create columns. To define the size of a column, you use
classes like .col-{breakpoint}-{size}. The breakpoint refers to different screen
sizes (e.g., sm, md, lg, xl), and the size is the number of columns the element
should span (from 1 to 12).
For example:
html
Copy code
xs: Extra small (screens smaller than 576px, this is the default, no need to add xs
in the class name)
You can specify how many columns an element should span at different
breakpoints using the following syntax:
.col-{breakpoint}-{size}
In the simplest form, if you want to create a grid where the content is evenly
divided across 12 columns:
html
Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.
css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
</div>
</div>
</body>
</html>
In this example, there are 3 columns, and each column spans 4 columns of the
total 12-column grid (4 + 4 + 4 = 12).
html
Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.
css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
</div>
</div>
</body>
</html>
On extra small screens (xs): All columns will take up the full width (12 columns).
On medium screens (md): Each column will span 6 columns (50% of the width).
On large screens (lg): Each column will span 4 columns (33.33% of the width).
This approach makes the layout responsive to different devices, ensuring a user-
friendly experience.
Offsets: Sometimes, you may want to create space on the left side of a column.
This can be done using the .offset-{size} class.
html
Copy code
<div class="row">
</div>
Nesting Columns: You can also nest rows and columns inside other columns,
allowing for more complex layouts.
html
Copy code
<div class="row">
<div class="col-6">
<div class="row">
</div>
</div>
</div>
1. Bootstrap Jumbotron
Basic Syntax:
Bootstrap provides the .jumbotron class to create this component. You can also
use other utility classes to customize the content and styling.
html
Copy code
<div class="jumbotron">
<hr class="my-4">
<p>It uses utility classes for typography and spacing to space content out
within the larger container.</p>
</div>
Explanation:
.btn btn-primary: A Bootstrap button for a call to action, styled with the .btn-
primary class.
Example:
html
Copy code
<p class="lead">We are glad to have you here. Explore our amazing content
and services.</p>
</div>
In this example, the jumbotron uses the .text-center class to center-align the
text, creating a visually balanced presentation.
Customization:
You can modify the jumbotron's appearance by changing the background color,
text color, or adding images. You can also make it responsive by using
Bootstrap's grid system.
Basic Syntax:
The .page-header class is used to create a page header. However, this class was
deprecated in Bootstrap 4 and later versions, so it's recommended to use a
simple combination of heading tags with utility classes for similar effects.
html
Copy code
<div class="page-header">
<h1>Welcome to My Website</h1>
</div>
Explanation:
.page-header: This class was used in older versions of Bootstrap to style a page
header with padding and margin. However, in Bootstrap 4 and beyond, it's
better to use plain heading elements like <h1>, <h2>, etc., combined with
Bootstrap’s utility classes for spacing and typography.
<h1>: The heading that serves as the title or header for the page or section.
Instead of using .page-header, you can use heading tags with utility classes.
Here’s how you can create a page header without using the deprecated .page-
header class:
html
Copy code
<div class="mb-4">
<p class="lead">This page header introduces the content in a clean and simple
manner.</p>
</div>
In this example, the <h1> acts as the page header and the .mb-4 class adds
margin to the bottom, giving it spacing from the following content.
5.What are the different ways an HTML element can be accessed in a JavaScript
code?
In JavaScript, you can access and manipulate HTML elements in several ways,
allowing you to interact with and modify the DOM (Document Object Model).
Here are the different methods used to access HTML elements in JavaScript:
1. Using getElementById()
This method allows you to select an HTML element by its id attribute. It returns
the first element with the specified ID.
html
Copy code
<script>
</script>
Use case: When you want to select a specific element with a unique id.
2. Using getElementsByClassName()
This method returns a collection of elements with the specified class name. It
returns a live HTMLCollection (updated automatically when the DOM changes).
html
Copy code
</script>
Use case: When you want to select all elements with the same class.
3. Using getElementsByTagName()
This method selects all elements with the specified tag name (e.g., div, p,
button). It returns a live HTMLCollection.
html
Copy code
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<script>
</script>
Use case: When you want to select all elements of a specific tag type (e.g., all
paragraphs or all divs).
4. Using querySelector()
This method allows you to select the first element that matches a CSS selector.
It returns a single element.
html
Copy code
<script>
</script>
Use case: When you want to select an element using a CSS selector (classes, IDs,
attributes, etc.). It allows more flexible querying than the previous methods.
5. Using querySelectorAll()
This method returns all matching elements as a NodeList (which is static, unlike
the live HTMLCollection). It allows you to use any CSS selector to select
elements.
html
Copy code
<script>
</script>
Use case: When you want to select multiple elements matching a CSS selector,
such as multiple elements with a specific class.
6. Using getAttribute()
While not a direct method of accessing an element, you can use getAttribute()
to retrieve attributes of elements after accessing them.
html
Copy code
<script>
</script>
Use case: After selecting an element, you can use this method to retrieve values
of specific attributes.
You can also access elements by navigating through their relationship in the
DOM, such as accessing parent, child, or sibling elements.
html
Copy code
<div id="parent">
</div>
<script>
</script>
Use case: Useful when you need to access elements based on their relationship
with other elements (parent-child relationships).
If you're working with form elements, you can access them via the
document.forms collection by form name or index.
html
Copy code
<form name="myForm">
</form>
<script>
</script>
Use case: When you are working with forms and want to access form elements
by their name or index.
You can also access custom data attributes defined using the data-* attributes.
This is useful for accessing and storing extra information on DOM elements.
html
Copy code
<script>
var product = document.getElementById("product");
</script>
Use case: To store and retrieve custom data on elements, particularly useful for
dynamic web applications.
6.Build a Javascript program to define a user defined function for sorting the
values in an array.
Here's how you can create a user-defined function to sort an array using Bubble
Sort:
javascript
Copy code
// If the current element is greater than the next element, swap them
arr[j] = arr[j + 1]; // Swap the current element with the next element
// Example usage
Explanation:
bubbleSort function: This is the user-defined function that sorts the array. It
accepts an array arr as its parameter.
The outer loop runs through all the elements of the array, while the inner loop
compares each element with the next one and swaps them if the current
element is greater than the next.
After each complete iteration of the inner loop, the largest element "bubbles"
to the end of the array. This is why the outer loop reduces the number of
comparisons in each pass (n - i - 1).
Example Usage:
The array numbers is passed to the bubbleSort() function, and the sorted array
is returned and printed.
Example Output:
plaintext
Copy code
If you want a simpler, built-in approach to sorting the array (though it's not
custom), you can use JavaScript's built-in sort() method:
javascript
Copy code
React is a popular JavaScript library for building user interfaces, particularly for
single-page applications where you need to manage dynamic content efficiently.
React has a number of features that make it powerful and widely used by
developers. Below are the key features of React:
1. Component-Based Architecture
Benefit: This modularity allows for easier code maintenance, testing, and
reusability. Developers can build complex UIs by combining smaller
components.
Example: A button component, form component, or a card component that can
be reused across different parts of an application.
Description: JSX is a syntax extension for JavaScript that looks similar to HTML
but is actually a syntax for creating React elements. It allows you to write HTML-
like code in your JavaScript files, making it easier to visualize the UI.
Benefit: JSX provides a more intuitive and readable way to structure the UI
compared to traditional JavaScript.
Example:
jsx
Copy code
3. Virtual DOM
Description: React uses a virtual DOM to optimize the rendering process. The
virtual DOM is a lightweight in-memory representation of the actual DOM.
When the state of an object changes, React updates the virtual DOM first, then
compares it with the previous version (a process called reconciliation) and
applies only the necessary changes to the actual DOM.
Example: If a user clicks a button to change a state, React will only update the
relevant DOM nodes rather than the entire UI.
Benefit: This makes it easier to understand how data changes in the application
and helps to avoid side effects. It also makes debugging easier since the flow of
data is predictable.
Example:
jsx
Copy code
};
5. State Management
Description: React components can maintain their own local state (data that
affects how a component behaves or renders). React’s useState hook (for
functional components) and setState method (for class components) are used to
manage and update state.
Example:
jsx
Copy code
Description: React Hooks are functions that let you use state and other React
features in functional components. Common hooks include useState, useEffect,
useContext, useReducer, etc.
Benefit: Hooks simplify code and allow functional components to have the same
capabilities as class components (e.g., local state, side effects).
Example:
jsx
Copy code
useEffect(() => {
console.log('Component mounted');
Example:
jsx
Copy code
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
</Routes>
</BrowserRouter>
Description: The Context API allows you to pass data through the component
tree without having to manually pass props down at every level.
Benefit: This is especially useful for global state management or when you need
to share data (such as user authentication or theme preferences) across many
components.
Example:
jsx
Copy code
return <ThemeContext.Provider
value="dark">{children}</ThemeContext.Provider>;
};
9. React DevTools
Benefit: Developers can easily inspect component hierarchies, state, props, and
much more, which helps in debugging and optimizing React apps.
Example: Using React DevTools to inspect the state and props of components in
real time while the app is running.
Benefit: This allows developers to execute code at specific points (e.g., when the
component mounts, updates, or unmounts).
Example:
jsx
Copy code
componentDidMount() {
console.log('Component mounted');
render() {
return <div>Hello</div>;
Description: React has several features that help optimize performance, such as
shouldComponentUpdate (in class components), React.memo (in functional
components), and lazy loading.
Benefit: These features help avoid unnecessary re-renders and improve the
efficiency of rendering large applications.
jsx
Copy code
return <div>{props.value}</div>;
});
Description: React can be rendered on the server side using frameworks like
Next.js, which enables server-side rendering (SSR) and static site generation
(SSG).
Benefit: This improves SEO (search engine optimization), as search engines can
crawl server-rendered pages more effectively. It also enhances initial load time.
In React, lifecycle methods are special methods that are invoked at specific
stages of a component's life, such as when it's being created, updated, or
removed from the DOM. These methods are only available in class components
(not in functional components, unless using Hooks in React).
Purpose: The constructor is called when the component is created. It's used for
initializing state, binding methods, and setting up initial values.
Usage: You can use this method to initialize the state or bind event handlers.
jsx
Copy code
constructor(props) {
this.state = {
count: 0
};
render() {
return <h1>{this.state.count}</h1>;
2. componentDidMount()
jsx
Copy code
componentDidMount() {
fetch('https://api.example.com/data')
render() {
3. shouldComponentUpdate(nextProps, nextState)
jsx
Copy code
shouldComponentUpdate(nextProps, nextState) {
return true;
render() {
return <h1>{this.state.count}</h1>;
4. componentDidUpdate(prevProps, prevState)
Purpose: This method is called after the component has been updated (i.e.,
when the state or props have changed). It's a good place for performing
operations after a component has re-rendered, like fetching new data based on
updated props or state.
jsx
Copy code
componentDidUpdate(prevProps, prevState) {
render() {
return <h1>{this.state.count}</h1>;
5. componentWillUnmount()
jsx
Copy code
componentWillUnmount() {
render() {
return <h1>Goodbye</h1>;
6. getDerivedStateFromProps(nextProps, nextState)
Purpose: This is a static method that is called before each render, both during
the initial mount and on updates. It's used to update the state based on changes
in props.
jsx
Copy code
return {
value: nextProps.someValue
};
render() {
return <h1>{this.state.value}</h1>;
7. getSnapshotBeforeUpdate(prevProps, prevState)
Purpose: This method is called right before the changes are applied to the DOM.
It allows you to capture information (like scroll position or other visual
properties) from the DOM before it is updated.
Usage: Often used for capturing scroll positions, media queries, or other DOM-
related info.
jsx
Copy code
getSnapshotBeforeUpdate(prevProps, prevState) {
return null;
render() {
return <h1>{this.state.count}</h1>;
8. render()
Purpose: The render() method is required in all class components. It's the
method where you define the JSX to be displayed by the component.
Usage: The render() method should return JSX or null. It’s invoked every time
the state or props change.
jsx
Copy code
render() {
Copy code
constructor(props) {
super(props);
this.state = {
count: 0
};
componentDidMount() {
shouldComponentUpdate(nextProps, nextState) {
return true;
componentDidUpdate(prevProps, prevState) {
if (prevState.count !== this.state.count) {
componentWillUnmount() {
increment = () => {
};
render() {
return (
<div>
<h1>{this.state.count}</h1>
<button onClick={this.increment}>Increment</button>
</div>
);
}
9.Apply the concept of redux with real time example t?
Redux is a state management library commonly used with React (although it can
be used with other libraries too). It provides a predictable state container that
helps manage the state of your application in a centralized and consistent
manner. Redux is particularly useful for managing state in large-scale
applications where multiple components need access to the same data or need
to communicate with each other.
Store: The central place where the state of your application lives.
Actions: Plain JavaScript objects that describe an event or action that has
occurred.
Dispatch: A function used to send actions to the Redux store to update the
state.
Let’s build a simple to-do list application using React and Redux.
Create a reducer to handle how the tasks state changes based on those actions.
Use connect from react-redux to connect the Redux store with React
components.
Action Types:
javascript
Copy code
// actions/types.js
Actions:
javascript
Copy code
// actions/todoActions.js
return {
type: ADD_TODO,
payload: todo
};
};
return {
type: REMOVE_TODO,
payload: id
};
};
Reducers:
javascript
Copy code
// reducers/todoReducer.js
const initialState = {
todos: []
};
switch (action.type) {
case ADD_TODO:
return {
...state,
};
case REMOVE_TODO:
return {
...state,
};
default:
return state;
};
Store Setup:
javascript
Copy code
// store.js
// Wrap your React application with the Redux provider to pass the store
Now that we have set up the actions, reducers, and store, let’s connect Redux
with our React components.
To-Do List Component:
javascript
Copy code
// components/TodoList.js
if (task.trim()) {
addTodo(task);
setTask('');
};
return (
<div>
<h2>To-Do List</h2>
<input
type="text"
value={task}
/>
<ul>
{todos.map(todo => (
<li key={todo.id}>
{todo.task}
</li>
))}
</ul>
</div>
);
};
});
const mapDispatchToProps = {
addTodo,
removeTodo
};
3. App Component
The App component is where we will connect the Redux store to the entire
application using the Provider component from react-redux.
javascript
Copy code
// App.js
return (
<Provider store={store}>
<div>
<TodoList />
</div>
</Provider>
);
};
Actions:
addTodo creates an action to add a new task. It takes the task as a parameter
and adds it to the payload of the action.
Reducers:
todoReducer manages the state of the to-do list. It listens for ADD_TODO and
REMOVE_TODO actions and updates the state accordingly.
Redux Store:
createStore is used to create the Redux store, which holds the state. The
Provider component from react-redux wraps the application to provide the
store to all components.
React Components:
The TodoList component uses connect from react-redux to connect to the Redux
store and dispatch actions (addTodo and removeTodo) based on user
interaction.
The mapStateToProps function maps the Redux state (the list of todos) to the
component's props, while mapDispatchToProps maps the dispatch functions
(addTodo and removeTodo) to the component's props.
Adding a Task:
When the user enters a task and clicks "Add Task", the addTodo action is
dispatched to the Redux store with the task.
The todoReducer listens for the ADD_TODO action, updates the state, and the
new task is added to the list in the Redux store.
Removing a Task:
When the user clicks the "Delete" button next to a task, the removeTodo action
is dispatched with the task's ID.
The todoReducer listens for the REMOVE_TODO action, removes the task from
the todos array in the state, and updates the UI.
UI Update:
View/Component: This is where the UI resides. When a user interacts with the
interface (e.g., clicks a button), an action is dispatched.
Action: An action is a plain JavaScript object that describes the event that
occurred (like adding an item to a to-do list). The action is dispatched to the
Redux store.
Reducer: Once the action is dispatched, the reducer function receives the
current state and the action. It calculates and returns a new state based on the
action type.
Store: The Redux store holds the current application state. After the reducer
processes the action, the store is updated with the new state.
sql
Copy code
^ | |
| v |
| +--------------+ |
| | Store | <----------+
| +--------------+
| |
+------------------------> v
+------------------+
| View/Component |
+------------------+
This diagram shows the flow of data:
The action goes to the reducer, which processes it and updates the store.
The updated store state is then passed down to the view/component, which re-
renders with the new state.
This unidirectional data flow is what makes Redux predictable and easier to
debug.