Advertisement
Todorov_Stanimir

09. Coffee Machine Exercise: Syntax, Functions and Statement

Sep 24th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function coffeeMachine(input) {
  2.     let type = {
  3.         'coffee caffeine': 0.80,
  4.         'coffee decaf': 0.90,
  5.         'tea': 0.80,
  6.         'milk': (price) => Math.ceil(10 * price * 0.10) / 10,
  7.         'sugar': 0.10
  8.     }
  9.     let incomes = 0;
  10.     for (let line of input) {
  11.         let price = 0;
  12.         let tokens = line.split(', ');
  13.         const coins = Number(tokens.shift());
  14.         let typeDrink = tokens.shift();
  15.         if (typeDrink === 'coffee') {
  16.             const typeCoffe = tokens.shift();
  17.             price = type[`${typeDrink} ${typeCoffe}`];
  18.         } else {
  19.             price = type[typeDrink];
  20.         }
  21.         if (tokens[0] === 'milk') {
  22.             tokens.shift();
  23.             price += type['milk'](price);
  24.         }
  25.         if (Number(tokens[0]) > 0 && Number(tokens[0]) <= 5) {
  26.             price += type['sugar'];
  27.         }
  28.         if (coins >= price) {
  29.             console.log(`You ordered ${typeDrink}. Price: $${price.toFixed(2)} Change: $${(coins - price).toFixed(2)}`)
  30.             incomes += price;
  31.         } else {
  32.             console.log(`Not enough money for ${typeDrink}. Need $${(price - coins).toFixed(2)} more.`)
  33.         }
  34.     }
  35.     console.log(`Income Report: $${incomes.toFixed(2)}`);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement