Skip to content

Instantly share code, notes, and snippets.

@QuadFlask
Last active April 25, 2016 15:21
Show Gist options
  • Save QuadFlask/3a8bfc9908e9a84367f47ff505a89d07 to your computer and use it in GitHub Desktop.
Save QuadFlask/3a8bfc9908e9a84367f47ff505a89d07 to your computer and use it in GitHub Desktop.
[CodeWars] Ingredients

Pete, the baker (part 2)

재료를 모두 사용할 수 있는 갯수에 대한 부족분을 계산(? 말로 적으니까 이상한데?)

var recipe = {flour: 200, eggs: 1, sugar: 100};

getMissingIngredients(recipe, {flour: 50, eggs: 1}); // must return {flour: 150, sugar: 100}
getMissingIngredients(recipe, {}); // must return {flour: 200, eggs: 1, sugar: 100}
getMissingIngredients(recipe, {flour: 500, sugar: 200}); // must return {flour: 100, eggs: 3, sugar: 100}

역시 이름 짓기가 어려워 대충 지었긴한데, 로직은 다들 비슷한듯

function getMissingIngredients(recipe, added) {
	var repipeIng = Object.keys(recipe);
	var addedIng = Object.keys(added);

	if (addedIng.length == 0) return recipe;

	var count = addedIng
		.map(k=> Math.ceil(added[k] / recipe[k]))
		.reduce((a,b)=> Math.max(a,b));

	var needs = {};
	repipeIng.forEach(k=> {
		var amount = recipe[k] * count - ~~added[k];
		if (amount > 0) needs[k] = amount;
	});

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