Fsd Lab Manual
Fsd Lab Manual
L T P C
0 1 2 2
II Year II Semester
FULL STACK DEVELOPMENT – 1
(SKILL ENHANCEMENT COURSE)
Course Objectives:
The main objectives of the course are to
• Make use of HTML elements and their attributes for designing static web pages
• Build a web page by applying appropriate CSS styles to HTML elements
• Experiment with JavaScript to develop dynamic web pages and validate forms
Experiments covering the Topics:
• Lists, Links and Images
• HTML Tables, Forms and Frames
• HTML 5 and Cascading Style Sheets, Types of CSS
• Selector forms
• CSS with Color, Background, Font, Text and CSS Box Model
• Applying JavaScript - internal and external, I/O, Type Conversion
• JavaScript Conditional Statements and Loops, Pre-defined and User-defined Objects
JavaScript Functions and Events
• [Link]
Sample Experiments:
1. Lists, Links and Images
a) Write a HTML program, to explain the working of lists. Note: It should have an
ordered list, unordered list, nested lists and ordered list in an unordered list and
definition lists.
b) Write a HTML program, to explain the working of hyperlinks using tag and href,
target Attributes.
c) Create a HTML document that has your image and your friend’s image with a specific
height and width. Also when clicked on the images it should navigate to their
respective profiles.
d) Write a HTML program, in such a way that, rather than placing large images on a
page, the preferred technique is to use thumbnails by setting the height and width
parameters to something like to 100*100 pixels. Each thumbnail image is also a link
to a full sized version of the image. Create an image gallery using this technique
2. HTML Tables, Forms and Frames
a) Write a HTML program, to explain the working of tables. (use tags:
<table>,<tr>,<th>,<td> and attributes: border, rowspan, colspan )
b) Write a HTML program, to explain the working of tables by preparing a timetable.
(Note: Use tag to set the caption to the table & also use cell spacing, cell padding,
border, rowspan, colspan etc.).
c) Write a HTML program, to explain the working of forms by designing Registration
form. (Note: Include text field, password field, number field, date of birth field,
checkboxes, radio buttons, list boxes using <select>&<option> tags, <text area> and
two buttons ie: submit and reset. Use tables to provide a better view).
d) Write a HTML program, to explain the working of frames, such that page is to be
divided into 3 parts on either direction. (Note: first frame image,
second frame paragraph, third frame hyperlink. And also make sure of using “no
frame” attribute such that frames to be fixed).
Text Books:
1. Programming the World Wide Web, 7th Edition, Robet W Sebesta, Pearson, 2013.
2. Web Programming with HTML5, CSS and JavaScript, John Dean, Jones & Bartlett
Learning, 2019 (Chapters 1-11).
3. Pro MERN Stack: Full Stack Web App Development with Mongo, Express, React, and
Node, Vasan Subramanian, 2nd edition, APress, O’Reilly.
Week 1
1. Lists, Links and Images
a. Write a HTML program, to explain the working of lists. Note: It should have an ordered
list, unordered list, nested lists and ordered list in an unordered list and definition lists.
b. Write a HTML program, to explain the working of hyperlinks using tag and href, target
Attributes.
c. Create a HTML document that has your image and your friend’s image with a specific
height and width. Also when clicked on the images it should navigate to their respective
profiles.
d. Write a HTML program, in such a way that, rather than placing large images on a page, the
preferred technique is to use thumbnails by setting the height and width parameters to
something like to 100*100 pixels. Each thumbnail image is also a link to a full sized version
of the image. Create an image gallery using this technique
1. Lists, Links, and Images
a. Write a HTML program, to explain the working of lists. Note: It should have an
ordered list, unordered list, nested lists and ordered list in an unordered list and
definition lists.
Theory:
HTML provides different types of lists to structure and organize content. The primary types
of lists are:
1. Ordered List (<ol>):
o Displays items in a numbered sequence.
o Each item is wrapped in an <li> (list item) tag.
2. Unordered List (<ul>):
o Displays items with bullets or other symbols.
o Each item is also wrapped in an <li> tag.
3. Nested Lists:
o A list (ordered or unordered) inside another list.
4. Mixed Lists:
o Combining ordered and unordered lists.
5. Definition List (<dl>):
o Consists of terms and their definitions.
o <dt>: Represents the term.
o <dd>: Represents the definition.
Program:
<!DOCTYPE html>
<html>
<head>
<title>Lists Example</title>
</head>
<body>
<h1>Lists in HTML</h1>
<h2>Ordered List</h2>
<ol>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ol>
<h2>Unordered List</h2>
<ul>
<li>Item A</li>
<li>Item B</li>
<li>Item C</li>
</ul>
<h2>Nested Lists</h2>
<ul>
<li>Parent Item
<ol>
<li>Child Item 1</li>
<li>Child Item 2</li>
</ol>
</li>
<li>Parent Item 2</li>
</ul>
<h2>Definition List</h2>
<dl>
<dt>HTML</dt>
<dd>HyperText Markup Language</dd>
<dt>CSS</dt>
<dd>Cascading Style Sheets</dd>
</dl>
</body>
</html>
Output:
b. Write a HTML program, to explain the working of hyperlinks using tag and href,
target Attributes.
Theory:
Hyperlinks are fundamental elements in HTML used to navigate between different web pages
or sections within the same page. They are created using the <a> (anchor) tag. The following
key attributes are commonly used with hyperlinks:
1. href Attribute: Specifies the URL of the page the link goes to. It can be an absolute
URL (e.g., [Link] or a relative URL (e.g., [Link]).
2. target Attribute: Determines where to open the linked document. Common values for
the target attribute include:
o _self (default): Opens the link in the same tab/window.
o _blank: Opens the link in a new tab/window.
o _parent: Opens the link in the parent frame (if the page is inside a frame).
o _top: Opens the link in the full body of the window (if the page is inside a
frame).
Program:
<!DOCTYPE html>
<html>
<head>
<title>Hyperlinks Example</title>
</head>
<body>
<h1>Hyperlinks in HTML</h1>
<a href="[Link] target="_blank">Visit Google</a><br>
<a href="[Link] target="_self">Visit Example</a>
</body>
</html>
Output:
c. Create a HTML document that has your image and your friend’s image with a
specific height and width. Also when clicked on the images it should navigate to their
respective profiles.
Theory:
In HTML, images can act as hyperlinks, allowing users to navigate to different pages or
websites when the image is clicked. This is achieved by embedding an <img> tag inside an
<a> (anchor) tag.
Key attributes used:
1. <img> Tag:
o src: Specifies the source of the image.
o alt: Provides alternative text for the image.
o height and width: Specify the dimensions of the image.
2. <a> Tag:
o href: Specifies the URL to navigate to when the image is clicked.
Program
<!DOCTYPE html>
<html>
<head>
<title>Image Navigation</title>
</head>
<body>
<h1>Images with Navigation</h1>
<a href="[Link]
<img src="[Link]" alt="Friend's Image" width="200" height="200">
</a>
<a href="[Link]
<img src="[Link]" alt="My Image" width="200" height="200">
</a>
</body>
</html>
Output:
d. Write a HTML program, in such a way that, rather than placing large images on a
page, the preferred technique is to use thumbnails by setting the height and width
parameters to something like to 100*100 pixels. Each thumbnail image is also a link to a
full sized version of the image. Create an image gallery using this technique
Theory:
Thumbnails are smaller versions of images, often used to save space and provide a preview.
When clicked, these thumbnails typically link to the full-sized image. In HTML, this is
accomplished using the <a> (anchor) and <img> (image) tags together. The following steps
describe how this works:
1. Thumbnail Display:
o Use the <img> tag to display a smaller version of the image.
o Set the width and height attributes of the <img> tag to control the size of the
thumbnail.
2. Link to Full-Sized Image:
o Wrap the <img> tag inside an <a> tag.
o The href attribute of the <a> tag should point to the full-sized image.
This technique improves performance by initially loading smaller images while allowing
users to view the full-sized version on demand.
Program:
<!DOCTYPE html>
<html>
<head>
<title>Image Gallery</title>
</head>
<body>
<h1>Thumbnail Gallery</h1>
<a href="[Link]"><img src="[Link]" alt="Image 1" width="100"
height="100"></a>
<a href="[Link]"><img src="[Link]" alt="Image 2" width="100"
height="100"></a>
<a href="[Link]"><img src="[Link]" alt="Image 3" width="100"
height="100"></a>
</body>
</html>
Output:
WEEK 2
a. Write a HTML program, to explain the working of tables. (use tags: <table>, <tr>, <th>,
<td> and attributes: border, rowspan, colspan)
b. Write a HTML program, to explain the working of tables by preparing a
timetable. (Note: Use <caption> tag to set the caption to the table & also use cell spacing,
cell padding, border, rowspan, colspan etc.).
c. Write a HTML program, to explain the working of forms by designing Registration form.
(Note: Include text field, password field, number field, date of birth field, checkboxes,
radio buttons, list boxes using <select>&<option> tags, <text area> and two buttons ie:
submit and reset. Use tables to provide a better view).
d. Write a HTML program, to explain the working of frames, such that page is to be divided
into 3 parts on either direction. (Note: first frame image, second frame paragraph,
third frame hyperlink. And also make sure of using “no frame” attribute such that
frames to be fixed).
2. HTML Tables, Forms, and Frames
a. Write a HTML program, to explain the working of tables. (use tags: <table>, <tr>,
<th>,
<td> and attributes: border, rowspan, colspan)
Theory:
1. <table>: Used to define an HTML table. It organizes data into rows and columns.
2. <tr>: Represents a table row. Contains one or more <th> (table header) or <td> (table
data) elements.
3. <th>: Defines a header cell within a row, typically displayed as bold and centered.
4. <td>: Represents a standard data cell within a row.
Attributes:
1. border: Specifies the width of the border around the table and its cells.
2. rowspan: Merges cells vertically by specifying the number of rows a cell should
span.
3. colspan: Merges cells horizontally by specifying the number of columns a cell should
span.
Program:
<!DOCTYPE html>
<html>
<head>
<title>HTML Tables</title>
</head>
<body>
<table border="1">
<tr>
<th>Name</th>
<th>Age</th>
<th>City</th>
</tr>
<tr>
<td>John</td>
<td>25</td>
<td>New York</td>
</tr>
<tr>
<td>Jane</td>
<td>30</td>
<td>Los Angeles</td>
</tr>
</table>
</body>
</html>
Output:
b. Write a HTML program, to explain the working of tables by preparing a
timetable. (Note: Use <caption> tag to set the caption to the table & also use cell spacing,
cell padding, border, rowspan, colspan etc.).
Theory:
Tags:
<table>: Creates a table for displaying data in rows and columns.
<caption>: Provides a descriptive title for the table, usually displayed above it.
<th>: Defines header cells in a table row, typically bold and centered by default.
<td>: Defines standard data cells in a table row.
<tr>: Groups a row of table cells (either <th> or <td>).
Attributes:
border: Specifies the thickness of the table's borders.
cellspacing: Sets the space between individual table cells.
cellpadding: Defines the padding inside each table cell to control spacing between the
content and cell edges.
rowspan: Merges a cell vertically across multiple rows.
colspan: Merges a cell horizontally across multiple columns.
Program:
<!DOCTYPE html>
<html>
<head>
<title>Timetable</title>
</head>
<body>
<table border="1" cellpadding="5" cellspacing="2">
<caption>Weekly Timetable</caption>
<tr>
<th>Day</th>
<th>9-10 AM</th>
<th>10-11 AM</th>
<th>11-12 PM</th>
</tr>
<tr>
<td>Monday</td>
<td>Math</td>
<td>Physics</td>
<td>Chemistry</td>
</tr>
<tr>
<td>Tuesday</td>
<td>Biology</td>
<td>Math</td>
<td>English</td>
</tr>
</table>
</body>
</html>
Output:
c. Write a HTML program, to explain the working of forms by designing Registration
form.
(Note: Include text field, password field, number field, date of birth field, checkboxes,
radio buttons, list boxes using <select>&<option> tags, <text area> and two buttons ie:
submit and reset. Use tables to provide a better view).
Theory:
HTML Forms allow users to input data, which can be processed by a web server.
• The <form> tag defines a form, and the action attribute specifies where the data is
sent.
• Text fields (<input type="text">) allow users to enter single-line input.
• Password fields (<input type="password">) hide input characters for security.
• Number fields (<input type="number">) accept numeric input.
• Date fields (<input type="date">) allow users to select a date.
• Checkboxes (<input type="checkbox">) enable multiple selections, while radio
buttons (<input type="radio">) allow single-choice selection.
• Dropdown lists use <select> and <option> tags to provide multiple options.
• Text areas (<textarea>) allow multi-line text input.
• Submit and Reset buttons (<input type="submit">, <input type="reset">) send and
clear the form data, respectively.
Program:
<!DOCTYPE html>
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<form>
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>Date of Birth:</td>
<td><input type="date" name="dob"></td>
</tr>
<tr>
<td>Gender:</td>
<td>
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="female">Female
</td>
</tr>
<tr>
<td>Languages Known:</td>
<td>
<input type="checkbox" name="lang" value="english">English
<input type="checkbox" name="lang" value="hindi">Hindi
</td>
</tr>
<tr>
<td>Country:</td>
<td>
<select name="country">
<option>India</option>
<option>USA</option>
<option>UK</option>
</select>
</td>
</tr>
<tr>
<td>Comments:</td>
<td><textarea name="comments"></textarea></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
</body>
</html>
Output:
d. Write a HTML program, to explain the working of frames, such that page is to be
divided
into 3 parts on either direction. (Note: first frame image, second frame paragraph,
third frame hyperlink. And also make sure of using “no frame” attribute such that
frames to be fixed).
Theory:
HTML Frames are used to divide a webpage into multiple sections, each displaying a
different document.
Program:
<!DOCTYPE html>
<html>
<head>
<title>HTML Frames</title>
</head>
<frameset rows="50%,25%,25%">
<frame src="[Link]" name="image_frame">
<frame src="[Link]" name="text_frame">
<frame src="[Link]" name="link_frame">
<noframes>
Your browser does not support frames.
</noframes>
</frameset>
</html>
Output:
WEEK 3
a. Write a HTML program, that makes use of <article>, <aside>, <figure>, <figcaption>,
<footer>, <header>, <main>, <nav>, <section>, <div>, <span> tags.
b. Write a HTML program, to embed audio and video into HTML web page.
c. Write a program to apply different types (or levels of styles or style specification formats) -
inline, internal, external styles to HTML elements. (identify selector, property and value).
3. HTML5 and Cascading Style Sheets (CSS)
a. Write a HTML program, that makes use of <article>, <aside>, <figure>,
<figcaption>,<footer>, <header>, <main>, <nav>, <section>, <div>, <span> tags.
Theory:
Attributes:
o controls: Adds play, pause, and volume controls.
o autoplay: Starts playback automatically.
o loop: Replays the video when it ends.
o poster: Displays an image before the video starts playing.
o width/height: Sets the dimensions of the video.
o src: Specifies the video file's path.
o type: Specifies the MIME type of the video file.
Program:
<!DOCTYPE html>
<html>
<head>
<title>Audio and Video Embedding</title>
</head>
<body>
<h1>Embedding Audio and Video</h1>
<audio controls>
<source src="audio.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<br>
<video controls width="400">
<source src="video.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
</body>
</html>
Output:
c. Write a program to apply different types (or levels of styles or style specification
formats) - inline, internal, external styles to HTML elements. (identify selector, property
and value).
Theory:
CSS styles can be applied in three ways:
12. Inline Styles:
1. Applied directly to an HTML element using the style attribute.
2. Selector: The specific HTML tag where the style is used.
3. Property: A CSS attribute that defines the style (e.g., color, font-size).
4. Value: The specific value assigned to the property (e.g., red, 16px).
13. Internal Styles:
1. Defined within a <style> tag inside the <head> section of the HTML
document.
◦ Simple Selectors:
▪ Select elements based on their name, ID, class, or grouping.
▪ Element Selector: Targets specific HTML tags (e.g., h1).
▪ ID Selector: Targets an element with a specific id (e.g., #header).
▪ Class Selector: Targets elements with a specific class (e.g., .highlight).
▪ Group Selector: Applies styles to multiple selectors, separated by
commas (e.g., h1, h2, p).
/* Group Selector */
h1, h2, h3 { font-family: Arial; }
/* Combinator Selectors */
div > p { background-color: yellow; } /* Child */
div p { color: green; } /* Descendant */
p + p { font-weight: bold; } /* Adjacent Sibling */
p ~ p { font-style: italic; } /* General Sibling */
/* Pseudo-classes */
a:hover { color: orange; }
/* Pseudo-elements */
p::first-line { text-transform: uppercase; }
/* Attribute Selector */
input[type="text"] { border: 1px solid black; }
</style>
</head>
<body>
<h1>Selectors Example</h1>
<div>
<p>This is a paragraph inside div.</p>
<p>This is another paragraph inside div.</p>
</div>
<p id="unique">This is a paragraph with ID.</p>
<p class="class-name">This is a paragraph with a class.</p>
<a href="#">Hover over this link</a>
<p>This paragraph will have italic siblings.</p>
<input type="text" placeholder="Type here">
</body>
</html>
Output:
WEEK 5
a. Write a program to demonstrate the various ways you can reference a color in CSS.
b. Write a CSS rule that places a background image halfway down the page, tilting it
horizontally. The image should remain in place when the user scrolls up or down.
c. Write a program using the following terms related to CSS font and text:
i. font-size
ii. font-weight
iv. text-decoration v. text-transformation
iii. font-style
vi. text-alignment
d. Write a program, to explain the importance of CSS Box model using
i. Content
ii. Border
iii. Margin
iv. padding
5. CSS with Colors, Fonts, Text, and Box Model
a. Write a program to demonstrate the various ways you can reference a color in CSS.
Theory:
Colors in CSS
In CSS, colors can be specified in various ways to style elements, each providing
flexibility for design:
1. Named Colors: Use predefined color names (e.g., red, blue, yellow).
2. Hexadecimal Colors: Specify colors using the #RRGGBB format (e.g., #ff0000
for red).
3. RGB Colors: Define colors using the rgb(red, green, blue) format with values
ranging from 0 to 255 (e.g., rgb(255, 0, 0) for red).
4. RGBA Colors: Similar to RGB but includes an alpha value for transparency (e.g.,
rgba(255, 0, 0, 0.5) for semi-transparent red).
5. HSL Colors: Specify colors using the hsl(hue, saturation, lightness) format (e.g.,
hsl(0, 100%, 50%) for red).
6. HSLA Colors: Similar to HSL but includes an alpha value for transparency (e.g.,
hsla(0, 100%, 50%, 0.5)).
7. Transparent: Use transparent to make an element fully see-through.
8. CurrentColor: Inherits the color from the parent element's color property.
Program:
<!DOCTYPE html>
<html>
<head>
<title>Colors in CSS</title>
<style>
body { color: rgb(255, 0, 0); }
h1 { color: #00FF00; }
p { color: hsl(240, 100%, 50%); }
</style>
</head>
<body>
<h1>RGB Color</h1>
<p>Hexadecimal and HSL Color</p>
</body>
</html>
Output:
b. Write a CSS rule that places a background image halfway down the page, tilting it
horizontally. The image should remain in place when the user scrolls up or down.
Theory:
• background-image: Specifies the image you want to display as the background.
• background-position: 50% 50%: This centers the background image
horizontally and vertically on the page, positioning it halfway down.
• background-attachment: fixed: Ensures that the background image doesn't
scroll with the page, but remains fixed while the content scrolls.
• transform: rotate(10deg): Rotates the background image by 10 degrees to tilt it
horizontally (you can adjust the degree to achieve the desired tilt effect).
• background-size: cover: Ensures the background image scales to cover the entire
viewport, ensuring it fits well no matter the screen size.
Program:
<!DOCTYPE html>
<html>
<head>
<title>Background Image</title>
<style>
body {
background-image: url('[Link]');
background-attachment: fixed;
background-repeat: no-repeat;
background-position: center bottom;
}
</style>
</head>
<body>
<h1>Background Example</h1>
</body>
</html>
Output:
c. Write a program using the following terms related to CSS font and text:
i. font-size
ii. font-weight
iv. text-decoration v. text-transformation
iii. font-style
vi. text-alignment
Theory:
▪ font-size: Defines the size of the text. It can be specified in various units like px, em,
%, rem, etc.
▪ font-weight: Specifies the thickness or boldness of the text. Common values include
normal, bold, lighter, and numerical values (e.g., 100, 400, 700).
▪ font-style: Sets the style of the font. Common values are normal, italic, and oblique.
▪ text-decoration: Applies decorations to text, such as underline, line-through,
overline, and none.
▪ text-transform: Controls the case of the text, such as uppercase, lowercase,
capitalize, or none.
▪ text-align: Specifies the horizontal alignment of the text within its container.
Common values are left, right, center, and justify.
Program:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Font and Text Properties</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center; /* text-alignment */
}
.styled-text {
font-size: 24px; /* font-size */
font-weight: bold; /* font-weight */
font-style: italic; /* font-style */
text-decoration: underline; /* text-decoration */
text-transform: uppercase; /* text-transformation */
margin: 20px;
}
</style>
</head>
<body>
<h1>CSS Font and Text Properties</h1>
<p class="styled-text">This is styled text</p>
</body>
</html>
Output:
d. Write a program, to explain the importance of CSS Box model using
i. Content
ii. Border
iii. Margin
iv. padding
Theory:
The CSS Box Model defines the rectangular boxes generated for elements on a webpage
and is essential for understanding how elements are sized and spaced in relation to each
other. The model consists of four main parts:
▪ Content: This is the actual content of the box, where text, images, or other media
appear. The content area defines the width and height of the element, and it is the
innermost part of the box.
▪ Padding: The space between the content and the border. Padding creates space inside
the box, between the content and the border. It can be set uniformly for all sides or
individually for top, right, bottom, and left.
▪ Border: The border surrounds the padding (if any) and the content. It is placed
between the padding and the margin. You can style borders with width, color, and
style (solid, dashed, etc.).
▪ Margin: The space outside the border. Margins create space between the element
and other elements on the page. Like padding, margins can be set for all sides or
individually for top, right, bottom, and left.
Program:
<!DOCTYPE html>
<html>
<head>
<title>Box Model</title>
<style>
.box {
width: 200px;
height: 100px;
padding: 20px;
border: 10px solid black;
margin: 15px;
}
</style>
</head>
<body>
<div class="box">Box Model Example</div>
</body>
</html>
Output:
WEEK 6
a. Write a program to embed internal and external JavaScript in a web page.
b. Write a program to explain the different ways for displaying output.
c. Write a program to explain the different ways for taking input.
d. Create a webpage which uses prompt dialogue box to ask a voter for his name and age.
Display the information in table format along with either the voter can vote or not
6. Applying JavaScript - Internal and External, I/O, Type Conversion
a. Write a program to embed internal and external JavaScript in a web page.
Theory:
• Internal and External JavaScript in a Web Page
▪ Internal JavaScript:
• Internal JavaScript is written directly within the HTML document,
enclosed in a <script> tag.
• This method is convenient for small scripts that are specific to a single
HTML page.
▪ External JavaScript:
• External JavaScript is placed in a separate .js file, which is then linked to
the HTML document using the <script> tag with the src attribute.
• It helps in organizing and reusing JavaScript code across multiple HTML
pages, making the code more maintainable.
• The external JavaScript file is linked just before the closing </body> tag to
ensure the HTML content is loaded first.
Program:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Example</title>
<script>
function internalFunction() {
alert("This is internal JavaScript!");
}
</script>
<script src="[Link]"></script>
</head>
<body>
<button onclick="internalFunction()">Internal Script</button>
<button onclick="externalFunction()">External Script</button>
</body>
</html>
External JavaScript file ([Link]):
function externalFunction() {
alert("This is external JavaScript!");
}
Output:
b. Write a program to explain the different ways for displaying output.
Theory:
Displaying Output in JavaScript
In JavaScript, there are several ways to display output to the user. These methods are used
depending on the context and the desired interaction with the user:
9. alert():
a) Displays a simple popup dialog with a message and an "OK" button.
b) It's often used for debugging or showing quick notifications but is not
ideal for modern web applications due to its intrusive nature.
10. [Link]():
a) Writes a string of text directly into the HTML document at the point where
the script is called.
11. [Link]():
a) Outputs information to the browser's console, which is especially useful for
debugging.
b) It doesn’t affect the web page's display, and it's used for developers to
view values during development.
12. innerHTML:
a) Allows you to change the content of an HTML element dynamically.
b) It’s commonly used to display results on the webpage itself, by modifying
Program:
<!DOCTYPE html>
<html>
<head>
<title>Display Output</title>
</head>
<body>
<script>
alert("This is an alert box!");
[Link]("This is written on the webpage.<br>");
[Link]("This is a console message.");
</script>
</body>
</html>
Output:
c. Write a program to explain the different ways for taking input.
Theory:
Taking Input in JavaScript
There are several ways to take input from the user in JavaScript. These methods allow
developers to interact with users and collect data for processing or displaying results.
▪ prompt():
• The prompt() method displays a dialog box with an input field, allowing the user
to type in data.
• It returns the value entered by the user as a string. If the user clicks "Cancel," it
returns null.
• This method is simple but outdated for modern web development due to its
intrusive nature and lack of customization.
▪ HTML Forms and <input> Elements:
• You can create HTML forms with <input> elements, such as text fields,
checkboxes, radio buttons, etc., to gather various types of user input.
• JavaScript can be used to access the values entered in the form fields using
methods like .value.
• Forms provide a more flexible and user-friendly way of taking input compared to
prompt().
• prompt(): Displays a dialog box asking the user for input. In this case, it will
collect the voter's name and age.
• Conditional Logic: After collecting the input, JavaScript can use conditional
statements to check the age of the user. If the age is 18 or older, the user is
eligible to vote; otherwise, they are not.
a) Goal: This program asks the user to input three integers. It compares the
numbers and displays either the largest number or a message saying "EǪUAL
NUMBERS" if all numbers are the same.
◦ Approach:
i. Use the prompt() method to collect the user input.
[Link] if and else conditions to compare the numbers and display the
result using alert().
If the numbers are equal, output the message "EǪUAL NUMBERS"; otherwise,
display the largest number followed by "LARGER NUMBER".
Program:
<!DOCTYPE html>
<html>
<head>
<title>Larger Number</title>
</head>
<body>
<script>
let a = parseInt(prompt("Enter first number:"));
let b = parseInt(prompt("Enter second number:"));
let c = parseInt(prompt("Enter third number:"));
Output:
b. Write a program to display week days using switch case.
Theory:
Program to Display Weekdays Using Switch-Case
a. Goal: This program displays the name of the weekday based on the user's input (a
number from 1 to 7).
◦ Approach:
i.
Use a switch statement where each case corresponds to a day of the
week.
The number entered by the user determines which day to display.
Program:
<!DOCTYPE html>
<html>
<head>
<title>Weekdays</title>
</head>
<body>
<script>
let day = parseInt(prompt("Enter a number (1-7):"));
switch (day) {
case 1: [Link]("Monday"); break;
case 2: [Link]("Tuesday"); break;
case 3: [Link]("Wednesday"); break;
case 4: [Link]("Thursday"); break;
case 5: [Link]("Friday"); break;
case 6: [Link]("Saturday"); break;
case 7: [Link]("Sunday"); break;
default: [Link]("Invalid Day");
}
</script>
</body>
</html>
Output:
c. Write a program to print 1 to 10 numbers using for, while and do-while loops.
Theory:
Program to Print Numbers from 1 to 10 Using Different Loops
Goal: This program demonstrates how to print numbers 1 through 10 using three
different types of loops: for, while, and do-while.
◦ Approach:
i. Use for loop to iterate through numbers.
ii. Use while loop for the same task, with a condition.
Use do-while loop to ensure the code runs at least once before checking the condition.
Program:
<!DOCTYPE html>
<html>
<head>
<title>Loops</title>
</head>
<body>
<script>
[Link]("<strong>Using for loop:</strong><br>");
for (let i = 1; i <= 10; i++) {
[Link](i + " ");
}
Goal: This program demonstrates different JavaScript loop types (for-in, forEach,
and for-of) to iterate over an object.
◦ Approach:
i. for-in loop iterates over the object's properties.
[Link]() iterates over the array values (if the object contains
arrays).
for-of loop works with iterable objects, like arrays or strings.
Program:
<!DOCTYPE html>
<html>
<head>
<title>Loops in Objects</title>
</head>
<body>
<script>
let student = { name: "John", age: 20, grade: "A" };
[Link]("<strong>Using for-in:</strong><br>");
for (let key in student) {
[Link](key + ": " + student[key] + "<br>");
}
◦ Approach:
i. Extract each digit of the number.
ii.
Compute the sum of the digits raised to the power of the number of
digits.
Compare the sum with the original number to determine if it’s an Armstrong number.
Program:
<!DOCTYPE html>
<html>
<head>
<title>Armstrong Number</title>
</head>
<body>
<script>
let num = parseInt(prompt("Enter a number:"));
let sum = 0, temp = num;
Goal: This program takes an amount (e.g., Rs. 163) and calculates how many
banknotes of different denominations (100’s, 50’s, etc.) are needed to match the
amount.
o Approach:
▪
Use division and modulo operations to calculate how many notes of
each denomination fit into the amount.
Start with the largest denomination and work down to the smallest (1’s).
Program:
<!DOCTYPE html>
<html>
<head>
<title>Denominations</title>
</head>
<body>
<script>
let amount = parseInt(prompt("Enter the amount:"));
let denominations = [100, 50, 20, 10, 5, 2, 1];
[Link]("<strong>Denominations:</strong><br>");
[Link](den => {
let count = [Link](amount / den);
if (count > 0) {
[Link](`${count} x ${den}<br>`);
amount %= den;
}
});
</script>
</body>
</html>
Output:
WEEK 9
a. Design a appropriate function should be called to display
i. Factorial of that number
ii. Fibonacci series up to that number
iii. Prime numbers up to that number
iv. Is it palindrome or not
b. Design a HTML having a text box and four buttons named Factorial, Fibonacci, Prime,
and Palindrome. When a button is pressed an appropriate function should be called to
display
i. Factorial of that number
ii. Fibonacci series up to that number
iii. Prime numbers up to that number
iv. Is it palindrome or not
c. Write a program to validate the following fields in a registration page
9. JavaScript Functions and Events
a. Design a appropriate function should be called to display
i. Factorial of that number
ii. Fibonacci series up to that number
iii. Prime numbers up to that number
iv. Is it palindrome or not
Theory:
◦ Factorial of a Number:
▪ Goal: To calculate the factorial of a given number.
▪ Explanation: The factorial of a number n is the product of all positive integers
less than or equal to n. It is calculated as n! = n * (n-1) * (n-2) * ...
• * 1. For example, 5! = 5 * 4 * 3 * 2 * 1 = 120.
◦ Palindrome Check:
▪ Goal: To determine if a given number is a palindrome.
▪ Explanation: A palindrome is a number that reads the same forwards and
backwards. For example, 121 is a palindrome, but 123 is not.
▪ Function Design: Convert the number to a string and check if it is equal to its
reverse.
Program:
<!DOCTYPE html>
<html>
<head>
<title>Functions</title>
</head>
<body>
<script>
function factorial(n) {
return n === 0 ? 1 : n * factorial(n - 1);
}
function fibonacci(n) {
let series = [0, 1];
for (let i = 2; i < n; i++) {
[Link](series[i - 1] + series[i - 2]);
}
return [Link](0, n);
}
function isPrime(num) {
if (num < 2) return false;
for (let i = 2; i <= [Link](num); i++) {
if (num % i === 0) return false;
}
return true;
}
function palindromeCheck(str) {
return str === [Link]("").reverse().join("");
}
Output:
b. Design a HTML having a text box and four buttons named Factorial, Fibonacci,
Prime,
and Palindrome. When a button is pressed an appropriate function should be called to
display
i. Factorial of that number
ii. Fibonacci series up to that number
iii. Prime numbers up to that number
iv. Is it palindrome or not
Theory:
Design HTML with Buttons for Various Operations
• Goal: Create an HTML page with a text box to accept the number and four
buttons to trigger the respective functions for factorial, Fibonacci, prime number
check, and palindrome check.
• Explanation:
• Use HTML to design a simple form with a text input to enter a number.
• Each button, when clicked, will call the appropriate JavaScript function based on
the number entered.
Program:
<!DOCTYPE html>
<html>
<head>
<title>Function Buttons</title>
<script>
function factorial(n) {
return n === 0 ? 1 : n * factorial(n - 1);
}
function fibonacci(n) {
let series = [0, 1];
for (let i = 2; i < n; i++) {
[Link](series[i - 1] + series[i - 2]);
}
return [Link](0, n);
}
function isPrime(num) {
if (num < 2) return false;
for (let i = 2; i <= [Link](num); i++) {
if (num % i === 0) return false;
}
return true;
}
function palindromeCheck(str) {
return str === [Link]("").reverse().join("");
}
function handleClick(action) {
let num = parseInt([Link]("number").value);
if (isNaN(num)) {
alert("Please enter a valid number");
return;
}
let result;
switch (action) {
case "factorial":
result = "Factorial: " + factorial(num);
break;
case "fibonacci":
result = "Fibonacci: " + fibonacci(num);
break;
case "prime":
result = "Is Prime: " + isPrime(num);
break;
case "palindrome":
result = "Palindrome: " + palindromeCheck(String(num));
break;
}
[Link]("output").innerHTML = result;
}
</script>
</head>
<body>
<input type="text" id="number" placeholder="Enter a number">
<button onclick="handleClick('factorial')">Factorial</button>
<button onclick="handleClick('fibonacci')">Fibonacci</button>
<button onclick="handleClick('prime')">Prime</button>
<button onclick="handleClick('palindrome')">Palindrome</button>
<p id="output"></p>
</body>
</html>
Output:
c. Write a program to validate the following fields in a registration page
Theory:
Program to Validate Registration Fields
Goal: To validate the fields in a user registration form such as name, email,
password, and confirm password.
o Explanation:
▪ Use JavaScript to ensure that the form fields are filled correctly. For
instance: