DEV Community

Discussion on: Daily Challenge #56 - Coffee Shop

Collapse
 
cgty_ky profile image
Cagatay Kaya

A simple Javascript solution using nested ternary operators.

const coffeeShop = (order = "Water", cash = 0) => {
  const drinks = [
    { name: "Americano", cost: 2.2 },
    { name: "Latte", cost: 2.3 },
    { name: "Flat White", cost: 2.4 },
    { name: "Filter", cost: 3.5 }
  ];

  const request = drinks.filter(drink => drink.name == order);
  request[0] == null
    ? console.log(`We do not serve ${order} here. Try some Filter tomorrow.`)
    : request[0].cost == cash
    ? console.log(`Here is your ${request[0].name}, have a nice day!`)
    : console.log("Sorry, exact change only, try again tomorrow!");
};

Works fine

coffeeShop(); //We do not serve Water here. Try some Filter tomorrow!
coffeeShop("Americano", 2.2); //Here is your Americano, have a nice day!
coffeeShop("Americano", 2.5); //Sorry, exact change only, try again tomorrow!
coffeeShop("Filter", 3.5); //Here is your Filter, have a nice day!
coffeeShop("Mocha", 3.5); //We do not serve Mocha here. Try some Filter tomorrow!!