Skip to content

Instantly share code, notes, and snippets.

@yvan-sraka
Created October 26, 2016 13:54
Show Gist options
  • Save yvan-sraka/a883d82437819d7a12e44ce464d96237 to your computer and use it in GitHub Desktop.
Save yvan-sraka/a883d82437819d7a12e44ce464d96237 to your computer and use it in GitHub Desktop.
/* ###### CONSTANT ###### */
var BILLS = [200, 100, 50, 20, 10, 5, 2, 1] // ARRAY of bills with really cheap bills like coins
/* ###### SMART ALGORITHM ###### */
var cash_machine = function (amount) { // FUNCTION
var i = 0; // iterator
while (amount > 0) { // check if their is still money to give for the customer
if (BILLS[i] <= amount) { // try with the larger bill
console.log("Cash Machine give me a bill of " + BILLS[i] + "€"); // GIVE ME MY MONEY!!!
amount = amount - BILLS[i]; // reduce the amount of money we still have to gave for the customer
} else { // try with a smaller bill
i = i + 1; // increment i variable of 1
}
}
}
/* ###### TESTS ###### */
cash_machine(42);
//cash_machine(25);
//cash_machine(128);
//cash_machine(201);
//cash_machine(1024);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment