0% found this document useful (0 votes)
11 views

JS Code 2024

Uploaded by

World Shorts
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

JS Code 2024

Uploaded by

World Shorts
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1-Write a JavaScript program that prompts the user to enter two integers.

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>

2-Modify the JavaScript program done in Part A by changing the way


output is displayed. In this part, you are expected to output the same
text in an alert window.
<!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 = "The first number is greater than the second number.";
} else if (num1 < num2) {
result = "The second number is greater than the first number.";
} else {
result = "The two numbers are equal.";
}

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 />

<label for="fahrenheitInput">Enter Fahrenheit temperature:</label>


<input type="number" id="fahrenheitInput" />
<button type="button" onclick="convertToCelsius()"> Convert to Celsius</button>
</form>
<div id="conversionResult"></div>
</body>
</html>
=-=-=-
5-Use a two-dimensional array to solve the following problem: A company has
four salespeople (1 to 4) who sell five different products (1 to 5). Once a day,
each salesperson passes in a slip for each different type of product actually sold.
Each slip contains:
a) the salesperson number,
b) the product number, and
c) the total dollar value of the product sold that day.
Thus, each salesperson passes in between zero and five sales slips per
day. Assume that the information from all of the slips for last month is
available. Write a script that will read all this information for last month’s
sales and summarize the total sales by salesperson by product. All totals
should be stored in the two-dimensional array sales. After processing all
the information for last month, display the results in an HTML5 table
format, with each of the columns representing a different salesperson and
each of the rows representing a different product. Cross-total each row to
get the total sales of each product for last month; cross-total each column
to get the total sales by salesperson for last month. Your tabular printout
should include these cross-totals to the right of the totaled rows and to the
bottom of the totaled columns.

<!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]
];

// Initialize the sales summary array


var salesSummary = [];

// Calculate the total sales by salesperson by product


for (var i = 1; i <= 4; i++) {
salesSummary[i] = [];
for (var j = 1; j <= 5; j++) {
salesSummary[i][j] = 0;
}
}

for (var k = 0; k < sales.length; k++) {


var salesperson = sales[k][0];
var product = sales[k][1];
var amount = sales[k][2];

salesSummary[salesperson][product] += amount;
}

// Display the sales summary in an HTML table format


var tableHtml = "<table>";
tableHtml += "<tr><th>Salesperson</th><th>Product 1</th><th>Product 2</th><th>Product
3</th><th>Product 4</th><th>Product 5</th><th>Total</th></tr>";

for (var i = 1; i <= 4; i++) {


tableHtml += "<tr><td>Salesperson " + i + "</td>";
var salespersonTotal = 0;

for (var j = 1; j <= 5; j++) {


tableHtml += "<td>" + salesSummary[i][j] + "</td>";
salespersonTotal += salesSummary[i][j];
}

tableHtml += "<td>" + salespersonTotal + "</td></tr>";


}

tableHtml += "<tr><td>Total</td>";
var productTotal = 0;

for (var j = 1; j <= 5; j++) {


var productTotal = 0;

for (var i = 1; i <= 4; i++) {


productTotal += salesSummary[i][j];
}

tableHtml += "<td>" + productTotal + "</td>";


}

tableHtml += "</tr></table>";

document.getElementById("salesTable").innerHTML = tableHtml;
</script>
</body>
</html>

You might also like