Created
October 26, 2016 13:54
-
-
Save yvan-sraka/6233409115b08d4b0813104d14a4abe8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* ###### 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