Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function coffeeMachine(input){
- const milk="milk";
- const caffeine="caffeine";
- const decaf="decaf";
- const sugarPrice=0.1;
- const milkCost=0.1;
- let totalIncome=0;
- input.forEach(element => {
- let e=element.split(',').map((item)=>item.trim());
- totalIncome+=getDrinkInfo(e);
- });
- let finalReport=`Income Report: ${totalIncome.toFixed(2)}$`;
- console.log(finalReport);
- function getDrinkInfo(element){
- let coins=Number(element[0]);
- let drinkType=element[1];
- let milkAdded=element.includes(milk);
- let defacCoffee=element.includes(decaf);
- let caffeineCoffee=element.includes(caffeine);
- let sugar=parseInt(element.pop());
- let sugarForCurrentDrink=sugar!=0?sugarPrice:0;
- let price;
- if(defacCoffee){
- price=0.90;
- }
- else if(caffeineCoffee){
- price=0.80;
- }
- else{
- price=0.80;
- }
- if(milkAdded){
- let newPrice=price+(price*milkCost);
- price=Number(newPrice.toFixed(1))+sugarForCurrentDrink;
- }else{
- price=price+sugarForCurrentDrink;
- }
- let difference=coins-price;
- if(difference>=0){
- let result=`You ordered ${drinkType}. Price: ${price.toFixed(2)}$ Change: ${difference.toFixed(2)}$`;
- console.log(result);
- return price;
- }
- else{
- let result=`Not enough money for ${drinkType}. Need ${Math.abs(difference).toFixed(2)}$ more.`;
- console.log(result);
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment