
// computeSalesTax.js
// P. Conrad for CISC103 sect 99, Fall 2006, lab10, 11/16/2006

// function computeSalesTax: computes sales tax
//   consumes: 
//         dollars: a dollar amount (number)
//         taxRate: a sales tax rate (a number, as a percent, e.g 5.5)
//   produces:
//         the sales tax amount, rounded to the nearest penny, 
//         expressed as a string
//
// Examples:
//   js> computeSalesTax(10,5);
//   0.50
//   js> computeSalesTax(10,5.5);
//   0.55
//   js> computeSalesTax(1,5.5);
//   0.06
//   js> typeof computeSalesTax(1,5.5)
//   string
//   js>

function computeSalesTax(dollars,taxRate)
{ 
   // convert from percent to decimal, e.g. from 5% to 0.05

   var taxRateAsDecimal = taxRate / 100; 
  
   // multiply to get the dollar amount

   var dollarAmount = dollars * taxRateAsDecimal;
 
   // round to two decimals, and return as a string with two decimal places

   return roundToTwoPlaces(dollarAmount).toFixed(2);
}


// function roundToTwoPlaces
//   rounds a number to two places
// consumes: num ( a number)
// produces: the same number rounded to two decimal places
// 
// Examples: 
//  js> roundToTwoPlaces(5.505)
//  5.51
//  js> roundToTwoPlaces(5.499)
//  5.0
//  js>
//

function roundToTwoPlaces(num) 
{
  return Math.round(num * 100) / 100;
}

function testComputeSalesTax()
{

  if (computeSalesTax(10,5) =="0.50")
    print("test 1: passed");
  else
    print("test 1: failed");


  if (computeSalesTax(10,5.5) =="0.55")
    print("test 2: passed");
  else
    print("test 2: failed");

  if (computeSalesTax(1,5.5) =="0.06")
    print("test 3: passed");
  else
    print("test 3: failed");

  if (typeof(computeSalesTax(1,5.5)) =="string")
    print("test 3: passed");
  else
    print("test 3: failed");

}
