Last active
April 9, 2019 12:56
-
-
Save Christonja/3e7e41911657d1e2d744d2381275b629 to your computer and use it in GitHub Desktop.
Cash Register created by Christonja - https://repl.it/@Christonja/Cash-Register
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* JavaScript Algorithms and Data Structures Projects: Cash Register: Challenge designed by FreeCodeCamp, | |
solution derived completely from my learning obtained thanks to FreeCodeCamp and other sources on the internet. | |
Function calls provided by FreeCodeCamp as ways to test the algorithm */ | |
function checkCashRegister(price, cash, cid) { | |
var changeDue = cash - price; //change required from cash given minus cost of purchase | |
var totalChange = { status: "OPEN", change: [] }; //new object that is initialised with an open status and empty array by default | |
var changeCount = 0; //counter | |
var tempChange; //temporary variable to assist with decrementing the change due, however it could be implemented without this | |
var tempCash; //gets the money out of the cash drawer for the currency unit we're working with | |
var cashDrawer = totalCash(cid); //cashDrawer is initialised to the return of a function that calculates the total cash in the drawer | |
//if cash-in-drawer is less than the change due, return insufficent funds, not needed however saves the code from running through further to the end of the else statement if this is provided up top. | |
if (cashDrawer < changeDue) { | |
console.log({ status: "INSUFFICIENT_FUNDS", change: [] }); | |
return { status: "INSUFFICIENT_FUNDS", change: [] }; | |
//if cash-in-drawer is equal to the change due, return the value of the cash in drawer and close it as it's been emptied by the customer | |
} else if (cashDrawer == changeDue) { | |
console.log({status: "CLOSED", change: cid }); | |
return {status: "CLOSED", change: cid }; | |
} else { | |
//else run through each currency unit pushing the amount of cash available at each currency unit into the totalChange object array until the changeDue in zero | |
tempCash = cid[8][1]; //get the currency unit from the cash drawer | |
while (changeDue >= 100 && tempCash > 0) { //while changeDue is greater than currency unit and while there's cash available at that currency unit | |
tempChange = changeDue - 100; //decrement changeDue | |
changeCount++; //count the notes/coins | |
changeDue = tempChange; //changeDue assigned as the decrement | |
tempCash-=100; //decrement tempCash as there's now less money in the cash drawer | |
} | |
//if the currency unit was used then push the times it was used onto the totalchange array and multiply by the currency unit value to be equal to the note/coin value | |
if (changeCount > 0) { | |
totalChange.change.push(["ONE HUNDRED", changeCount * 100]); | |
} | |
//repeat this process for each denomination | |
tempCash = cid[7][1]; | |
changeCount = 0; | |
while (changeDue >= 20 && tempCash > 0) { | |
tempChange = changeDue - 20; | |
changeCount++; | |
changeDue = tempChange; | |
tempCash-=20; | |
} | |
if (changeCount > 0) { | |
totalChange.change.push(["TWENTY", changeCount * 20]); | |
} | |
tempCash = cid[6][1]; | |
changeCount = 0; | |
while (changeDue >= 10 && tempCash > 0) { | |
tempChange = changeDue - 10; | |
changeCount++; | |
changeDue = tempChange; | |
tempCash-=10; | |
} | |
if (changeCount > 0) { | |
totalChange.change.push(["TEN", changeCount * 10]); | |
} | |
tempCash = cid[5][1]; | |
changeCount = 0; | |
while (changeDue >= 5 && tempCash > 0) { | |
tempChange = changeDue - 5; | |
changeCount++; | |
changeDue = tempChange; | |
tempCash-=5; | |
} | |
if (changeCount > 0) { | |
totalChange.change.push(["FIVE", changeCount * 5]); | |
} | |
tempCash = cid[4][1]; | |
changeCount = 0; | |
while (changeDue >= 1 && tempCash > 0) { | |
tempChange = changeDue - 1; | |
changeCount++; | |
changeDue = tempChange; | |
tempCash-=1; | |
} | |
if (changeCount > 0) { | |
totalChange.change.push(["ONE", changeCount * 1]); | |
} | |
tempCash = cid[3][1]; | |
changeCount = 0; | |
while (changeDue >= 0.25 && tempCash > 0) { | |
tempChange = changeDue - 0.25; | |
changeCount++; | |
changeDue = tempChange; | |
tempCash-=0.25; | |
} | |
if (changeCount > 0) { | |
totalChange.change.push(["QUARTER", changeCount * 0.25]); | |
} | |
tempCash = cid[2][1]; | |
changeCount = 0; | |
while (changeDue >= 0.1 && tempCash > 0) { | |
tempChange = changeDue - 0.1; | |
changeCount++; | |
changeDue = tempChange; | |
tempCash-=0.1; | |
} | |
if (changeCount > 0) { | |
totalChange.change.push(["DIME", changeCount * 0.1]); | |
} | |
tempCash = cid[1][1]; | |
changeCount = 0; | |
while (changeDue >= 0.05 && tempCash > 0) { | |
tempChange = changeDue - 0.05; | |
changeCount++; | |
changeDue = tempChange; | |
tempCash-=0.05; | |
} | |
if (changeCount > 0) { | |
totalChange.change.push(["NICKEL", changeCount * 0.05]); | |
} | |
tempCash = cid[0][1]; | |
changeCount = 0; | |
//if change due is more than what's left in the cash drawer at this point then return insufficient, the customer gets nothing even though they almost got most of their money back | |
if (changeDue > tempCash) { | |
return { status: "INSUFFICIENT_FUNDS", change: [] }; | |
// else if change is still due, we know there's money in the cash drawer enough to cover it, and we also know we've exhausted the other currency units, therefore, supply the customer with the remainder of the change in penny's | |
} else if (changeDue > 0){ | |
var remainder = 0; | |
remainder = Number(changeDue.toFixed(2)); //fixed the remainder to two decimal places to remove imprecision | |
totalChange.change.push(["PENNY", remainder]); //push penny's onto the array | |
} | |
console.log(totalChange); | |
return totalChange; | |
} | |
//function to calculate total cash in cash drawer | |
function totalCash(total) { | |
var temp = 0; | |
for (var x = 0; x < total.length; x++) { | |
temp+=total[x][1]; | |
} return temp.toFixed(2); //returns the total fixed to 2 decimal places to prevent imprecision | |
} | |
} | |
// Example cash-in-drawer array: | |
// [["PENNY", 1.01], | |
// ["NICKEL", 2.05], | |
// ["DIME", 3.1], | |
// ["QUARTER", 4.25], | |
// ["ONE", 90], | |
// ["FIVE", 55], | |
// ["TEN", 20], | |
// ["TWENTY", 60], | |
// ["ONE HUNDRED", 100]] | |
checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]) //should return an object. | |
checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]) //should return {status: "OPEN", change: [["QUARTER", 0.5]]}. | |
checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]) //should return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}. | |
checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) //should return {status: "INSUFFICIENT_FUNDS", change: []}. | |
checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) //should return {status: "INSUFFICIENT_FUNDS", change: []}. | |
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) //should return {status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment