JS Code 2024
JS Code 2024
Once obtained, the program outputs HTML text displayed in the browser
indicating which number is greater than the other. If the two numbers are
equal, it displays the message “The two numbers are equal”. The text
must be displayed within <h1> tags.
=-=-
<!DOCTYPE html>
<html>
<head>
<title>Number Comparison</title>
<script>
function compareNumbers() {
var num1 = parseInt(prompt("Enter the first number:"));
var num2 = parseInt(prompt("Enter the second number:"));
var result;
if (num1 > num2) {
result = "<h1>The first number is greater than the second number.</h1>";
} else if (num1 < num2) {
result = "<h1>The second number is greater than the first number.</h1>";
} else {
result = "<h1>The two numbers are equal.</h1>";
}
document.getElementById("comparisonResult").innerHTML = result;
}
</script>
</head>
<body>
<h1>Number Comparison</h1>
<button onclick="compareNumbers()">Compare Numbers</button>
<div id="comparisonResult"></div>
</body>
</html>
var result;
alert(result);
}
</script>
</head>
<body>
<h1>Number Comparison</h1>
<button onclick="compareNumbers()">Compare Numbers</button>
</body>
</html>
=-=-=-=-=-=
3-Write a JavaScript program that uses functions to find the square of
a given number entered as form input. You should use two functions
one to calculate the square, while the other writes the result to the
form.
<!DOCTYPE html>
<html>
<head>
<title>Square Calculator</title>
<script>
function calculateSquare() {
var inputNumber = document.getElementById("numberInput").value;
var square = squareNumber(inputNumber);
displayResult(square);
}
function squareNumber(number) {
return number * number;
}
function displayResult(square) {
document.getElementById("result").innerHTML = "The square is: " + square;
}
</script>
</head>
<body>
<h1>Square Calculator</h1>
<form>
<label for="numberInput">Enter a number:</label>
<input type="number" id="numberInput" />
<button type="button" onclick="calculateSquare()">Calculate Square</button>
</form>
<div id="result"></div>
</body>
</html>
4-Create a webpage using form text boxes that:
contains the function Celsius, that returns the Celsius equivalent of a Fahrenheit
temperature, using the calculation:
C = 5.0/9.0 * (F – 32)
Contains the function Fahrenheit that returns the Fahrenheit equivalent of a
Celsius temperature, using the calculation:
F = 9.0/5.0 * C + 32
Use these functions to write a script that enables the user to enter either a
Fahrenheit or a Celsius temperature and displays the Celsius or Fahrenheit
equivalent. Your XHTML document should contain two buttons; one
to initiate the conversion from Fahrenheit to Celsius and one to initiate the
conversion from Celsius to Fahrenheit.
=-=-=-=
<!DOCTYPE html>
<html>
<head>
<title>Temperature Converter</title>
<script>
function convertToFahrenheit() {
var celsiusInput = document.getElementById("celsiusInput"). value;
var fahrenheit = celsiusToFahrenheit(celsiusInput);
document.getElementById("conversionRes ult").innerHTML = "Fahrenheit: " + fahrenheit + "°F";
}
function convertToCelsius() {
var fahrenheitInput = document.getElementById("fahrenheitInput"). value;
var celsius = fahrenheitToCelsius (fahrenheit Input);
document.getElementById("conversionRes ult").innerHTML = "Celsius: " + celsius + "°C";
}
function celsiusToFahrenheit(celsius) {
return (9.0 / 5.0) * celsius + 32;
}
function fahrenheitToCelsius(fahrenheit) {
return 5.0 / 9.0 * (fahrenheit - 32);
}
</script>
</head>
<body>
<h1>Temperature Converter</h1>
<form>
<label for="celsiusInput">Enter Celsius temperature:</label>
<input type="number" id="celsiusInput" />
<button type="button" onclick="convertToFahrenheit ()">Convert to Fahrenheit</button>
<br />
<!DOCTYPE html>
<html>
<head>
<title>Sales Summary</title>
<style>
table {
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 5px;
}
</style>
</head>
<body>
<h1>Sales Summary</h1>
<div id="salesTable"></div>
<script>
// Define the sales data for last month
var sales = [
[1, 1, 100],
[1, 2, 200],
[2, 1, 150],
[2, 3, 300],
[2, 4, 250],
[3, 2, 175],
[3, 3, 225],
[4, 1, 125],
[4, 2, 180],
[4, 4, 275]
];
salesSummary[salesperson][product] += amount;
}
tableHtml += "<tr><td>Total</td>";
var productTotal = 0;
tableHtml += "</tr></table>";
document.getElementById("salesTable").innerHTML = tableHtml;
</script>
</body>
</html>