A Pen by philip

Thumbnail
This awesome code was written by efferon, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright efferon ©
  • HTML
  • CSS
  • JavaScript
    

/*Downloaded from https://www.codeseek.co/efferon/a-pen-by-philip-EZjGbN */
    


/*Downloaded from https://www.codeseek.co/efferon/a-pen-by-philip-EZjGbN */
    function roundToTwo(num) {    
    return +(Math.round(num + "e+2")  + "e-2");
}

// startingCash is an array of "denominationName", "totalForDenomination"
//   eg.  [["TEN", 20,]. [TWETNTY, 60]]
function CashDrawer(startingCash) {  
  for (var i=0; i<startingCash.length; i++) {
    this[startingCash[i][0]] = startingCash[i][1];
  } 
} 

CashDrawer.prototype.totalCash = function () {
    var cash = 0;
    var that = this;  
  
    var denominations = Object.keys(that);
  
    //console.log(denominations);
    for (var i=0; i< denominations.length; i++) {
      cash =  cash + that[denominations[i]];
    }
    return roundToTwo(cash);
  };

CashDrawer.prototype.pay = function (amount) {
  var change = {};
  
  if (amount > this.totalCash()) {
    return "Insufficient Funds";
  }  
  
  if (amount === this.totalCash()) {
    return "Closed";
  }  
  
  while (amount > 0) {
    if (amount >= 100 && this["ONE HUNDRED"] ) {
//       console.log(`Payout 100`);
      this["ONE HUNDERED"] -= 100;
      if (!change["ONE HUNDRED"]) { change["ONE HUNDRED"] = 0; }  
      change["ONE HUNDRED"] += 100;
      amount -= 100;
    } else if (amount >= 20 && this.TWENTY ) {
//       console.log(`Payout 20`);
      this.TWENTY -= 20;
      if (!change.TWENTY) { change.TWENTY = 0;}
      change.TWENTY += 20;
      amount -= 20;
    } else if (amount >= 10 && this.TEN ) {
//       console.log(`Payout 10`);
      this.TEN -= 10;
      if (!change.TEN) { change.TEN = 0;}   
      change.TEN += 10;
      amount -= 10;
    } else if (amount >= 5 && this.FIVE ) {
//       console.log(`Payout 5`);
      this.FIVE -= 5;
      if (!change.FIVE) { change.FIVE = 0;}
      change.FIVE += 5;
      amount -= 5;
    } else if (amount >= 1 && this.ONE ) {
      console.log(`Payout 1`);
      this.ONE -= 1;
      if (!change.ONE) { change.ONE = 0;}
      change.ONE += 1;
      amount -= 1;
    } else if (amount >= 0.25 && this.QUARTER ) {
//       console.log(`Payout .25`);
      this.QUARTER -= 0.25;
      if (!change.QUARTER) { change.QUARTER = 0; }
      change.QUARTER += 0.25;
      amount -= 0.25;
    } else if (amount >= 0.1 && this.DIME ) {
//       console.log(`Payout .1`);
      this.DIME -= 0.10;
      if (!change.DIME) { change.DIME = 0;}
      change.DIME += 0.10;
      amount -= 0.10;
    } else if (amount >= 0.05 && this.NICKEL ) {
//       console.log(`Payout .05`);
      this.NICKEL -= 0.05;	
      if (!change.NICKEL) { change.NICKEL = 0;}    
      change.NICKEL += 0.05;
      amount -= 0.05;
    } else if (amount >= 0.01 && this.PENNY ) {
//       console.log(`Payout .01`);
      this.PENNY -= 0.01;
      if (!change.PENNY) { change.PENNY = 0;}
      change.PENNY += 0.01;
      amount -= 0.01;
    }
  }
  return change;
};


function formatChangeToArray(change) { 
  if (change === "Insufficient Funds" || change === "Closed") {
    return change; 
  }
   
  if (change["ONE HUNDRED"]) {
    console.log(`100: ${change["ONE HUNDRED"]}`);
  }
  
  if (change.TWENTY) {
    console.log(`20: ${change.TWENTY}`);
  }
  
  if (change.TEN) {
    console.log(`10: ${change.TEN}`);
  }
  
  if (change.PENNY) {
    console.log(`.01: ${change.PENNY}`);
  }

  
}

function checkCashRegister(price, cash, cid) {
  var cashDrawer = new CashDrawer(cid);  
  var cashInDrawer = cashDrawer.totalCash();
  var changeNeeded = cashDrawer.pay(cash - price);
  return formatChangeToArray(changeNeeded); 
}

// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.10],
// ["QUARTER", 4.25],
// ["ONE", 90.00],
// ["FIVE", 55.00],
// ["TEN", 20.00],
// ["TWENTY", 60.00],
// ["ONE HUNDRED", 100.00]]

// checkCashRegister(19.50, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);
checkCashRegister(19.51, 20.00, [["PENNY", 0.50], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);

Comments