Advertisement
vdjalov

Untitled

Jan 17th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. function demo(arr){
  2. let sum = 0;
  3. for(let i = 0; i < arr.length; i++){
  4. let split = arr[i].split(", ");
  5. calculateDrinkCost(split);
  6. }
  7. function calculateDrinkCost(split) {
  8. let cost = 0;
  9. let insertedAmount = +split[0];
  10. let drink = split[1];
  11. let milk = split.includes("milk");
  12. let sugar = split[split.length - 1];
  13.  
  14. if(drink == "coffee"){
  15. if(split.includes("caffeine")){
  16. cost = 0.80;
  17. }else{
  18. cost = 0.90;
  19. }
  20. }else{
  21. cost = 0.80;
  22. }
  23.  
  24. if(milk) {
  25. cost = cost + +(cost * 0.1).toFixed(1);
  26. }
  27.  
  28. if(sugar > 0) {
  29. cost+= 0.1;
  30. }
  31.  
  32. let change = Math.abs(cost - insertedAmount);
  33. if(insertedAmount >= cost){
  34. sum+=cost;
  35. console.log(`You ordered ${drink}. Price: ${cost.toFixed(2)}$ Change: ${change.toFixed(2)}$`);
  36. }else {
  37. console.log(`Not enough money for ${drink}. Need ${change.toFixed(2)}$ more.`)
  38. }
  39. }
  40.  
  41. console.log(`Income Report: ${sum.toFixed(2)}$`);
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement