lundi 13 avril 2020

can't get this simple FOR LOOP code run correctly

I'm trying to solve a simple coffee-shop puzzle, the goal is to check if the item is in the menu and if I have enough beans after serving each drink.

I tried to console.log each row- and still I do not understand why it doesn't run as intended- somehow it goes into one if condition- and then goes also into the else.. what am i doing wrong?

thanks for your time

  `const coffeeShop = {
  beans: 40,
  drinkRequirements: {
    latte: 10,
    americano: 5,
    doubleShot: 15,
    frenchPress: 12
  },
  makeDrink: function (drinkType) {
    const drinks = Object.keys(coffeeShop.drinkRequirements);
    let drinkCost = coffeeShop.drinkRequirements[drinkType];
    let binz = this.beans;
    for (const key of drinks) {
      if (key === drinkType){
        if (drinkCost <= binz){
          coffeeShop.beans = [binz - drinkCost]
          console.log(`Good news! we have ${drinkType} and we have enough beans')
        }
        else {
          console.log("OUT of beans!")
        }
      } else {
        console.log(`we dont serve ${drinkType}`)
      }
    }
      }
    }
    // tests that wont run correctly:
    coffeeShop.makeDrink("latte"); 
    coffeeShop.makeDrink("americano");
    coffeeShop.makeDrink("filtered"); 
    coffeeShop.makeDrink("doubleShot");
    coffeeShop.makeDrink("frenchPress"); `

Aucun commentaire:

Enregistrer un commentaire