Skip to content

Instantly share code, notes, and snippets.

@JaymesKat
Created December 27, 2019 02:33
Show Gist options
  • Save JaymesKat/c56ea4a222cca04bdd65fd2cadd91680 to your computer and use it in GitHub Desktop.
Save JaymesKat/c56ea4a222cca04bdd65fd2cadd91680 to your computer and use it in GitHub Desktop.
Design a cash register drawer function checkCashRegister() that accepts purchase price as the first argument (price), payment as the second argument (cash), and cash-in-drawer (cid) as the third argument. cid is a 2D array listing available currency. The checkCashRegister() function should always return an object with a status key and a change k…
const CLOSED = "CLOSED";
const INSUFFICIENT_FUNDS = "INSUFFICIENT_FUNDS";
const OPEN = "OPEN";
const PENNY = 0;
const NICKEL = 1;
const DIME = 2;
const QUARTER = 3;
const ONE = 4;
const FIVE = 5;
const TEN = 6;
const TWENTY = 7;
const ONE_HUNDRED = 8;
const currUnits = [
["PENNY", 0.01],
["NICKEL", 0.05],
["DIME", 0.1],
["QUARTER", 0.25],
["ONE", 1],
["FIVE", 5],
["TEN", 10],
["TWENTY", 20],
["ONE HUNDRED", 100]
]
function checkCashRegister(price, cash, cid) {
var change;
let changeDue = cash - price;
const drawerStatus = getDrawerStatus(changeDue, cid);
switch(drawerStatus){
case INSUFFICIENT_FUNDS:
change = { status: INSUFFICIENT_FUNDS, change: [] }
break;
case CLOSED:
change = { status: CLOSED, change: [...cid] }
break;
case OPEN:
const changeCurrency = [...collectChange(changeDue, cid)];
change = changeCurrency.length > 0 ? { status: OPEN, change: changeCurrency } : { status: INSUFFICIENT_FUNDS, change: [] }
}
// Here is your change, ma'am.
return change;
}
function getDrawerStatus(changeDue, cashInDrawer){
let totalCash = 0;
let status;
for(let i = 0; i < cashInDrawer.length; i++){
totalCash += cashInDrawer[i][1];
}
if(totalCash > changeDue){
status = "OPEN";
}
if(totalCash == changeDue){
status = "CLOSED";
}
if(totalCash < changeDue){
status = "INSUFFICIENT_FUNDS";
}
return status;
}
function collectChange(changeDue, cashInDrawer) {
const changeBreakdown = splitChange(changeDue);
const { dollars, cents } = changeBreakdown;
const billChange = getBillsFromDrawer(dollars, cashInDrawer).change;
let coinChange = getCoinsFromDrawer(cents, cashInDrawer).change;
return billChange.concat(coinChange);
}
function splitChange(change){
const breakdown = change.toString().split(".");
return { dollars: breakdown[0], cents: breakdown[1] }
}
function splitBillAmounts(amount){
let digits = amount.toString().split('').reverse();
for(let i = 0; i < digits.length; i++){
digits[i] = i >= 1 ? parseInt(digits[i]+'0'.repeat(i)) : parseInt(digits[i]);
}
return digits.reverse();
}
function selectBillOrCoinChange(amount, cid, unit, change){
let currency = cid[unit];
if(unit < PENNY){
return { change: [], cid }
}
else if(amount == 0){
return { change, cid };
}
else if(amount <= currency[1]){
if(amount%currUnits[unit][1] != 0){
const amtToRemove = Math.floor(amount/currUnits[unit][1])*currUnits[unit][1];
cid[unit] = [currency[0], currency[1] - amtToRemove]
change.push([currency[0], amtToRemove])
const remAmount = unit > QUARTER ? amount - amtToRemove : parseFloat((amount - amtToRemove).toFixed(2))
return selectBillOrCoinChange(remAmount, cid, unit - 1, change)
} else {
cid[unit] = [currency[0], currency[1] - amount]
change.push([currency[0], amount])
}
return { change, cid }
}
else if(amount > currency[1]){
const remAmount = amount - currency[1];
if(currency[1] != 0){
change.push(currency);
}
cid[unit] = [currency[0], 0]
return selectBillOrCoinChange(remAmount, cid, unit - 1, change);
}
}
function getBillsFromDrawer(amount, cid){
const billAmounts = splitBillAmounts(amount);
let result = [];
let i = 0, change = [];
while(i < billAmounts.length){
if(billAmounts[i] >= 100) {
result.push(selectBillOrCoinChange(billAmounts[i], cid, ONE_HUNDRED, change));
i++
continue;
}
if(billAmounts[i] >= 20){
result.push(selectBillOrCoinChange(billAmounts[i], cid, TWENTY, change));
i++
continue;
}
if(billAmounts[i] >= 10){
result.push(selectBillOrCoinChange(billAmounts[i], cid, TEN, change));
i++
continue;
}
if(billAmounts[i] >= 5){
result.push(selectBillOrCoinChange(billAmounts[i], cid, FIVE, change));
i++
continue;
}
result.push(selectBillOrCoinChange(billAmounts[i], cid, ONE, change));
i++;
}
result = result[result.length-1];
result.change = arrangeChange(result.change);
return result
}
function getCoinsFromDrawer(amount, cid){
const coinAmounts = splitCoinAmounts(amount);
let result = [];
let i = 0, change = [];
while(i < coinAmounts.length){
if(coinAmounts[i] >= 0.25) {
result.push(selectBillOrCoinChange(coinAmounts[i], cid, QUARTER, change));
i++
continue;
}
if(coinAmounts[i] >= 0.1) {
result.push(selectBillOrCoinChange(coinAmounts[i], cid, DIME, change));
i++
continue;
}
if(coinAmounts[i] >= 0.5) {
result.push(selectBillOrCoinChange(coinAmounts[i], cid, NICKEL, change));
i++
continue;
}
result.push(selectBillOrCoinChange(coinAmounts[i], cid, PENNY, change));
i++
}
result = result[result.length-1];
result.change = arrangeChange(result.change);
return result
}
function splitCoinAmounts(amount) {
let digits = amount.toString().split('');
for(let i = 0; i < digits.length; i++){
digits[i] = i >= 1 ? parseFloat('0.'+'0'.repeat(i)+digits[i]) : parseFloat('0.'+digits[i]);
}
return digits;
}
// Utility Functions
function arrangeChange(change){
let arrangedChange = [];
let copyChange = change;
for(let i = 0; i < copyChange.length; i++){
let currency = copyChange[i];
if(existsInArr(arrangedChange, currency[0])) {
continue;
}
for(let j = 0; j < change.length; j++){
if(i != j) {
if(currency[0] == change[j][0]){
currency[1] += change[j][1];
}
}
}
arrangedChange.push(currency)
}
return arrangedChange;
}
function existsInArr(haystack, needle) {
for (var i = 0; i < haystack.length; ++i) {
if (haystack[i][0] == needle) {
return true;
}
}
return false;
}
checkCashRegister(19.5, 19, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
@kashyapmanu
Copy link

Thank you for this, I was searching for a solution and this is very neatly written, I could understand properly.

@webhasan
Copy link

webhasan commented Jan 18, 2022

This is my solution. I have tried to make thing little cleaner & easy to understand.

function checkCashRegister(price, cash, cid) {
  const INSUFFICIENT_FUNDS = {status: "INSUFFICIENT_FUNDS", change: []}
  const CLOSED = {status: "CLOSED", change: cid}
  const OPEN = {status: "OPEN", change: []}
  let totalCid = cidTotal();
  let dueBlance = parseFloat((cash - price).toFixed(2));

  if(totalCid < dueBlance) {
    return INSUFFICIENT_FUNDS;
  }

  if(totalCid === dueBlance) {
    return CLOSED;
  }

  let cashTypes = [
    ["ONE HUNDRED", 100],
    ["TWENTY", 20],
    ["TEN", 10],
    ["FIVE", 5],
    ["ONE", 1],
    ["QUARTER", 0.25],
    ["DIME", 0.1],
    ["NICKEL", 0.05],
    ["PENNY", 0.01],
  ];


  for(let i = 0; i < cashTypes.length; i++) {
    let cashType = cashTypes[i][0];
    let cashValue = cashTypes[i][1];
    let totalCash = cid.find(item => item[0] === cashType)[1];

    if(dueBlance > cashValue && dueBlance > totalCash ) { // if due balance is bigger than available blance
      dueBlance -= totalCash;
      OPEN.change.push([cashType, totalCash]);
    }else if(dueBlance > cashValue && totalCash > dueBlance) { // if there are more balance than due balance
      let pay = Math.floor(dueBlance / cashValue) * cashValue;
      dueBlance -= pay;
      dueBlance = parseFloat(dueBlance.toFixed(2));
      OPEN.change.push([cashType, pay]);
    }
  }

  if(dueBlance > 0) {
    return INSUFFICIENT_FUNDS;
  }
  
  return OPEN;

  function cidTotal() {
    return parseFloat(cid.reduce((a,b) => a + b[1], 0).toFixed(2));
  }
}

@JaymesKat
Copy link
Author

JaymesKat commented Jan 18, 2022 via email

@zain-akram
Copy link

zain-akram commented Apr 30, 2022

Here is my solution:

function checkCashRegister(price, cash, cid) {
  let change = [];
  let changeDue = cash - price;
  const currency = {
    PENNY: 0.01,
    NICKEL: 0.05,
    DIME: 0.1,
    QUARTER: 0.25,
    ONE: 1,
    FIVE: 5,
    TEN: 10,
    TWENTY: 20,
    "ONE HUNDRED": 100
  };

  let incomingCid = [...cid].reverse().reduce((obj, curr) => {
    obj[curr[0]] = curr[1]
    return obj
  }, {});

  const status = {
    1: 'OPEN',
    0: 'CLOSED',
    '-1': 'INSUFFICIENT_FUNDS'
  };

  for (let key in incomingCid) {

    if(parseInt(changeDue / currency[key]) > 0){
      let particularValue = changeDue - incomingCid[key] <= 0? (parseInt((changeDue/currency[key]).toFixed(2))*currency[key]) : incomingCid[key]
      changeDue -= particularValue
      incomingCid[key] -= particularValue;
      change.push([key, particularValue])
      
    }
  }

  let amountPresentInCid = Object.values(incomingCid).every(val => val === 0)
  changeDue = changeDue.toFixed(2)
  if (changeDue == 0 && !amountPresentInCid){
    return { status: status[1], change }
  } 
  else if(changeDue > 0){
    return { status: status[-1], change: []}
  }
  else{
    return { status: status[0], change: cid}
  }
}

checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);

@andresListorti
Copy link

thanks!

@leandreAlly
Copy link

Thanks!

@AmigosKazz
Copy link

thanks

@maryam0108
Copy link

Thank you

@Hien2432
Copy link

Hien2432 commented May 10, 2024

Cash App offers a variety of ways to earn free money. From referral bonuses to cashback rewards, users can accumulate funds effortlessly. By inviting friends to join Cash App, both parties can receive a cash bonus upon successful sign-up and qualifying transactions. Additionally, Cash Boosts provide instant discounts at select merchants, allowing users to save money with every purchase. Explore free money on Cash App features today and start earning free money.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment