How to Calculate a Baseball Ticket in JavaScript

JavaScript offers an easy scripting solution for quick calculations needed inside a Web browser. Particularly, mathematical operations done to get a total cost or calculate tax on an item such as a baseball ticket require only that the programmer somehow provide a ticket price, and relevant tax and fee information. After that, it is simply a matter of doing the math, and wrapping the calculations in a function for ease of use.

Things You'll Need

  • Text Editor
  • Web Browser
Show More

Instructions

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

Related Searches:

References

Comments

Related Ads

Featured