Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative solution for coins ! #91

Open
BackPackerDz opened this issue Jun 24, 2017 · 2 comments
Open

Alternative solution for coins ! #91

BackPackerDz opened this issue Jun 24, 2017 · 2 comments

Comments

@BackPackerDz
Copy link

BackPackerDz commented Jun 24, 2017

Hi ! thank you very much for you amazing tutorials !

Here is my solution for the coins

function getChange (totalPayable, cashPaid) {

		var result = [];

		var coins = [200, 100, 50, 20, 10, 5, 2, 1];

		var diffrence = cashPaid - totalPayable;

		var i = 0;

		do {
			if (coins[i] <= diffrence)
			{
				result.push(coins[i]);
				diffrence = diffrence - coins[i];
			}
			else 
				i++;
		}
		while (i < coins.length);

		return result;
	}
@BackPackerDz BackPackerDz changed the title Alerternative solution for coins ! Alternative solution for coins ! Jun 24, 2017
@xianshenglu
Copy link

My solution:

      function getChange (totalPayable, cashPaid) {
        let coins = [200, 100, 50, 20, 10, 5, 2, 1]
        let difference = cashPaid - totalPayable
        let result = []
        while (difference > 0) {
          let maxCoin = coins.find(v => v <= difference)
          result.push(maxCoin)
          difference -= maxCoin
        }
        return result
      }

@nelsonic
Copy link
Member

@xianshenglu great use of a while loop. ✅

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

No branches or pull requests

3 participants