vdjalov

Untitled

Jan 17th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 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. let availableAmount = +split[0];
  6. let sugar = split[split.length - 1];
  7. let cost;
  8. if(split.includes("coffee") && split.includes("caffeine")) {
  9. cost = 0.80;
  10. if(split.includes("milk")){
  11. cost = +cost + +(cost * 0.1).toFixed(1);
  12. }
  13. if(sugar > 0){
  14. cost = +cost + 0.1;
  15. }
  16. let change = Math.abs(cost - availableAmount);
  17. if(cost <= availableAmount) {
  18. sum = +sum + +cost;
  19. console.log(`You ordered coffee. Price: ${cost.toFixed(2)}$ Change: ${change.toFixed(2)}$`);
  20. }else {
  21. console.log(`Not enough money for coffee. Need ${cost.toFixed(2)}$ more.`);
  22. }
  23. } else if(split.includes("coffee") && split.includes("decaf")) {
  24. cost = 0.90;
  25. if(split.includes("milk")){
  26. cost = +cost + +(cost * 0.1).toFixed(1);
  27. }
  28. if(sugar > 0){
  29. cost = +cost + 0.1;
  30. }
  31. let change1 = Math.abs(cost - availableAmount);
  32. if(cost <= availableAmount) {
  33. sum = +sum + +cost;
  34. console.log(`You ordered coffee. Price: ${cost.toFixed(2)}$ Change: ${change1.toFixed(2)}$`);
  35. }else {
  36. console.log(`Not enough money for coffee. Need ${cost.toFixed(2)}$ more.`);
  37. }
  38. } else if (split.includes("tea")){
  39. cost = 0.80;
  40. if(split.includes("milk")){
  41. cost = +cost + +(cost * 0.1).toFixed(1);
  42. }
  43. if(sugar > 0){
  44. cost = +cost + 0.1;
  45. }
  46. let changeTea = Math.abs(cost - availableAmount);
  47. if(cost <= availableAmount) {
  48. sum = sum + +cost;
  49. console.log(`You ordered tea. Price: ${cost.toFixed(2)}$ Change: ${changeTea.toFixed(2)}$`);
  50. }else {
  51. console.log(`Not enough money for tea. Need ${changeTea.toFixed(2)}$ more.`);
  52. }
  53. }
  54. }
  55. console.log(`Income Report: ${sum.toFixed(2)}$`);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment