Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. var john = {
  2. fullName: 'John Smith',
  3. bills: [124, 48, 268, 180, 42],
  4. tipsCalc: function(){
  5.  
  6. this.tips = [];
  7. this.finalValues = [];
  8.  
  9. for (let i = 0; i < this.bills.length; i++) {
  10.  
  11.  
  12. // Determinate percentage based on tipping rules
  13. var percentage;
  14.  
  15.  
  16. if (this.bills[i] < 50) {
  17. percentage = .2;
  18.  
  19. }else if (this.bills[i] >= 50 && this.bills[i] <200) {
  20. percentage = .15;
  21.  
  22. }else{
  23. percentage = .1;
  24.  
  25. }
  26.  
  27. // Add results to the corresponing arrays
  28.  
  29. this.tips[i] = this.bills[i] * percentage;
  30. this.finalValues[i] = this.bills[i] + this.bills[i] * percentage;
  31. }
  32. }
  33. }
  34.  
  35.  
  36. john.tipsCalc();
  37. console.log('John's Family TIPS ' + john.tips);
  38. console.log('John's Family BILLS ' + john.bills);
  39. console.log('John's Family Final a pagar ' + john.finalValues);
  40.  
  41. console.log('---------------------------------------------------');
  42.  
  43. var mark = {
  44. fullName: 'Mark Miller',
  45. bills: [77, 475, 110, 45],
  46. tipsCalc: function(){
  47.  
  48. this.tips = [];
  49. this.finalValues = [];
  50.  
  51. for (let i = 0; i < this.bills.length; i++) {
  52.  
  53.  
  54. // Determinate percentage based on tipping rules
  55. var percentage;
  56.  
  57.  
  58. if (this.bills[i] < 100) {
  59. percentage = .2;
  60.  
  61. }else if (this.bills[i] >= 100 && this.bills[i] < 300) {
  62. percentage = .1;
  63.  
  64. }else{
  65. percentage = .25;
  66.  
  67. }
  68.  
  69. // Add results to the corresponing arrays
  70.  
  71. this.tips[i] = this.bills[i] * percentage;
  72. this.finalValues[i] = this.bills[i] + this.bills[i] * percentage;
  73. }
  74. }
  75. }
  76.  
  77.  
  78. mark.tipsCalc();
  79. console.log('Mark's Family TIPS ' + mark.tips);
  80. console.log('Mark's Family BILLS ' + mark.bills);
  81. console.log('Mark's Family Final a pagar ' + mark.finalValues);
  82.  
  83. var sum = 0;
  84. var averageTips;
  85.  
  86. function average(family) {
  87.  
  88. for (let i = 0; i < family.tips.length; i++) {
  89.  
  90. sum += family.tips[i];
  91. }
  92.  
  93. return averageTips = sum / (family.tips.length);
  94. }
  95.  
  96. console.log('---------------------------------------------------');
  97.  
  98. average(john);
  99. console.log('John's Family Suma TIPS = ' + sum);
  100. console.log('John's Family Average TIPS = ' + averageTips);
  101.  
  102. console.log('---------------------------------------------------');
  103.  
  104. average(mark);
  105. console.log('Mark's Family Suma TIPS = ' + sum);
  106. console.log('Mark's Family Average TIPS = ' + averageTips);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement