How to Calculate a Baseball Ticket in JavaScript
- 1). Begin the script, and create a baseball calculation function, that takes as its arguments the number of baseball tickets and the price:
<script type="text/javascript">
function calcBaseballTickets(num, cost){
}
</script> - 2). Calculate the total cost of the tickets:
function calcBaseballTickets(num, cost){
var total = num * cost;
} - 3). Add the applicable sales tax to the price. This example assumes a sales tax of 3 percent:
function calcBaseballTickets(num, cost){
var total = num * cost;
total += total * (.03);
} - 4). Add any service charges and print the result. This example assumes a service charge of $4:
function calcBaseballTickets(num, cost){
var total = num * cost;
total += (total * (.03));
total += 4;
document.write("Total Cost is: " + total);
} - 5). Calculate the price for one ticket using the function. This will print "34.09" to the browser:
<script type="text/javascript">
function calcBaseballTickets(num, cost){
var total = num * cost;
total += (total * (.03));
total += 4;
document.write("Total Cost is: " + total);
}
var tickets = 1;
var cost = 30.00;
calcBaseball(tickets, cost);
</script>
Source...