
// computeMarginOfVictory.js
// P. Conrad for CISC103 sect 99, Fall 2006, lab10, 11/16/2006

// function computeMarginOfVictory: computes margin of victory between two candidates
//   consumes: 
//         votesForWinner: a number
//         votesForLoser: a number
//   produces:
//         the margin of victory, expressesd as a percentage, not rounded (as a number)
//        
//
// Examples:
//   js> computeMarginOfVictory(550,450);
//   10
//   js> computeMarginOfVictory(3642,3542);
//   1.39198218
//   js> computeMarginOfVictory(49,51);
//   2
//   js> typeof computeMarginOfVictory(49,51)
//   number
//   js>

function computeMarginOfVictory(votesForWinner,votesForLoser)
{ 
  // @@@ fill in body here!
  return 0; // @@@ this is just a place holder---remove this line!
}


// function approxEquals
//   computes whether two numbers are within some tolerance of each other
// consumes: a (a number)
//           b (a number)
//           tolerance (a number)
// produces:  boolean
// 
// Examples: 
//      js> approxEquals(1.01,1.02,0.0001)
//      false
//      js> approxEquals(100.01,100.99,0.1)
//      true
//      js>


function approxEquals(a,b,tolerance) 
{
 // compute absolute value of difference
 // determine whether it is less than the tolerance, and 
 // return true or false
 //    Math.abs() is a method (function) that computes absolute value
  return (Math.abs(a-b)<=tolerance);
}



function testComputeMarginOfVictory()
{

// Examples:
//   js> computeMarginOfVictory(550,450);
//   10
//   js> computeMarginOfVictory(3642,3542);
//   1.39198218
//   js> computeMarginOfVictory(51,49);
//   2
//   js> typeof computeMarginOfVictory(51,49)
//   number
//   js>
  var expectedAnswer, actualAnswer;
  
  expectedAnswer = 10;
  actualAnswer = computeMarginOfVictory(550,450);
  if (approxEquals(expectedAnswer,actualAnswer,0.0001))
    print("test 1: passed");
  else
    print("test 1: failed");

  expectedAnswer = 1.39198218;
  actualAnswer = computeMarginOfVictory(3642,3542);
  if (approxEquals(expectedAnswer,actualAnswer,0.0001))
    print("test 2: passed");
  else
    print("test 2: failed");

  expectedAnswer = 2;
  actualAnswer = computeMarginOfVictory(51,49);
  if (approxEquals(expectedAnswer,actualAnswer,0.0001))
    print("test 3: passed");
  else
    print("test 3: failed");

  if (typeof(actualAnswer)=="number")
    print("test 4: passed");
  else
    print("test 4: failed");


}
