Javascript Programs
Javascript Programs
Create an HTML page that includes an input field for the user to enter the number of
copies of a newspaper sold on a Sunday, a button labeled "Calculate Profit," and a
paragraph element to display the calculated profit. Write a JavaScript function named
calculateProfit() that takes the number of copies sold as input, calculates the total
revenue, total cost, and profit based on given values, and returns the calculated profit.
Integrate this function with the HTML page so that when the "Calculate Profit" button is
clicked, the number of copies sold is read from the input field, the calculateProfit()
function is called with the number of copies as input, and the calculated profit is
displayed in the paragraph element.
Total Revenue:
o Total Revenue = Number of copies sold (w) * Selling
price per copy
Total Cost:
o Total Cost = (Number of copies sold (w) * Cost price
per copy) + Fixed cost
Profit:
o Profit = Total Revenue - Total Cost
Given Values:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
function calculateProfit() {
const costPrice = 5;
const w = parseInt(document.getElementById("copiesSold").value);
</script>
</body>
</html>
2. Create an interactive web page for calculating the factorial of a non-negative integer. The page
should include an input field for the user to enter an integer, a "Calculate Factorial" button, and
a paragraph element to display the result. Implement a JavaScript function named
factorial() that calculates the factorial recursively. Integrate this function with the HTML
elements to calculate the factorial when the button is clicked and display the result in the
paragraph element. Finally, enhance the user experience by applying basic CSS styling to center
the content, add appropriate spacing, and style the input field, button, and result paragraph
with suitable colors, fonts, and borders.
</html>
<!DOCTYPE html>
<html>
<head>
<title>Factorial Calculator</title>
<style>
body {
font-family: sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f0f0f0;
.container {
background-color: #fff;
padding: 20px;
border-radius: 5px;
input[type="number"] {
padding: 10px;
border-radius: 3px;
margin-right: 10px;
button {
background-color: #4CAF50;
color: white;
border: none;
border-radius: 3px;
cursor: pointer;
p{
margin-top: 10px;
font-weight: bold;
</style>
</head>
<body>
<div class="container">
<h2>Factorial Calculator</h2>
<button onclick="calculateFactorial()">Calculate</button>
<p id="result"></p>
</div>
<script>
function factorial(n) {
if (n === 0 || n === 1) {
return 1;
} else {
}
}
function calculateFactorial() {
</script>
</body>
</html>
3. Create an interactive web page for calculating the fibonacci of a non-negative integer. The page
should include an input field for the user to enter an integer, a "Calculate Fibonacci" button, and
a paragraph element to display the result. Implement a JavaScript function named
fibonacci() that calculates the factorial recursively. Integrate this function with the HTML
elements to calculate the factorial when the button is clicked and display the result in the
paragraph element. Finally, enhance the user experience by applying basic CSS styling to center
the content, add appropriate spacing, and style the input field, button, and result paragraph
with suitable colors, fonts, and borders
<!DOCTYPE html>
<html>
<head>
<title>Fibonacci Calculator</title>
<style>
body {
font-family: sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f0f0f0;
.container {
background-color: #fff;
padding: 20px;
border-radius: 5px;
input[type="number"] {
padding: 10px;
border-radius: 3px;
margin-right: 10px;
button {
background-color: #4CAF50;
color: white;
border: none;
border-radius: 3px;
cursor: pointer;
}
p{
margin-top: 10px;
font-weight: bold;
</style>
</head>
<body>
<div class="container">
<h2>Fibonacci Calculator</h2>
<button onclick="calculateFibonacci()">Calculate</button>
<p id="result"></p>
</div>
<script>
function fibonacci(n) {
if (n <= 1) {
return n;
} else {
function calculateFibonacci() {
const number = parseInt(document.getElementById("numberInput").value);
</script>
</body>
</html>
4. Create an interactive web page that calculates the sum of two numbers. The page should
include two input fields for the user to enter the numbers, a "Calculate Sum" button, and a
paragraph element to display the result. You are provided with a JavaScript function
addTwoNumbers(num1, num2) that takes two numbers as input and returns their sum. Write
JavaScript code to: 1) Retrieve the values entered by the user from the input fields, 2) Call the
addTwoNumbers() function with the entered values, and 3) Display the result returned by the
function in the paragraph element
<!DOCTYPE html>
<html>
<head>
<title>Sum Calculator</title>
</head>
<body>
<h2>Sum Calculator</h2>
<p id="result"></p>
<script>
function addTwoNumbers(num1, num2) {
function calculateSum() {
document.getElementById("result").innerHTML = "The sum of " + num1 + " and " + num2 + " is: " +
sum;
</script>
</body>
</html>
5. DOM Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<ul id="myList">
<li>Item 1</li>
<li>Item 2</li>
</ul>
<button id="addItemButton">Add Item</button>
<script>
document.getElementById('addItemButton').addEventListener('click', function() {
document.getElementById('myList').appendChild(newItem);
});
</script>
</body>
</html>
Code:
html
Copy code
<!DOCTYPE html>
<html>
<head>
<title>Greeting Page</title>
<style>
body {
text-align: center;
font-family: Arial, sans-serif;
}
h1 {
color: #4CAF50;
}
</style>
</head>
<body>
<h1>Welcome!</h1>
<p>Enter your name below:</p>
<input type="text" id="userName" placeholder="Your name">
<button onclick="greetUser()">Greet Me</button>
<script>
function greetUser() {
const name = document.getElementById("userName").value;
alert(`Hello, ${name}! Welcome to the webpage.`);
}
</script>
</body>
</html>
A webpage that changes the background color when you click a button.
Code:
html
Copy code
<!DOCTYPE html>
<html>
<head>
<title>Color Changer</title>
<style>
body {
text-align: center;
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Background Color Changer</h1>
<p>Click the button to change the background color!</p>
<button onclick="changeColor()">Change Color</button>
<script>
function changeColor() {
const colors = ['#FF5733', '#33FF57', '#3357FF', '#F0FF33',
'#FF33A8'];
const randomColor = colors[Math.floor(Math.random() * colors.length)];
document.body.style.backgroundColor = randomColor;
}
</script>
</body>
</html>
A simple counter that increases every time you press the button.
Code:
html
Copy code
<!DOCTYPE html>
<html>
<head>
<title>Counter</title>
<style>
body {
text-align: center;
font-family: Arial, sans-serif;
}
h1 {
color: #FF5733;
}
</style>
</head>
<body>
<h1>Click Counter</h1>
<p>Button has been clicked <span id="counter">0</span> times.</p>
<button onclick="incrementCounter()">Click Me</button>
<script>
let count = 0;
function incrementCounter() {
count++;
document.getElementById('counter').textContent = count;
}
</script>
</body>
</html>