complete-javascript-course icon indicating copy to clipboard operation
complete-javascript-course copied to clipboard

291 Updating Recipe Servings at 07:34 - an incorrect formula

Open hazartilirot opened this issue 4 years ago • 1 comments

Well, if we look at the provided template there are two buttons -/+, meaning the one would increase a value by 1 and the other is the other way around i.e. decrease the value by 1.

We pass in a value of "1" to updateServings(newServing). The very first action should be made is increase the total of servings. We should take a value of servings (let's say 4) and add our 1. const totalServings = newServing + state.recipe.servings; then, we should calculate the ratio. const ratio = totalServings / state.recipe.servings and only then can we change our ingredients. However, before that, we have to check if total servings is greater than 1 if so return immediately for we could decrease servings as low as below "0".

BTW fractional library is buggy. It reaches 27 (or 28) and throws an error.

291 Updating Recipe Servings mp4_snapshot_07 34 418

hazartilirot avatar Dec 13 '20 10:12 hazartilirot

Oh, apologies, I've jumped to conclusions. The feature would work in the end. However, what's the purpose in calculating in The View I thought we need to deal with data in a Model. Why do we change principles? Well, the actions alike confuse me. We should be consistent...

export const updateServings = function(servingsAmount) {
  let totalServings = servingsAmount + state.recipe.servings;
  if (totalServings < 1) return;
  const ratio = totalServings / state.recipe.servings;
  state.recipe.ingredients.forEach(e => {
    e.quantity = e.quantity * ratio;
  });
  state.recipe.servings = totalServings;
};

hazartilirot avatar Dec 13 '20 11:12 hazartilirot