Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var john = {
- fullName: 'John Smith',
- bills: [124, 48, 268, 180, 42],
- tipsCalc: function(){
- this.tips = [];
- this.finalValues = [];
- for (let i = 0; i < this.bills.length; i++) {
- // Determinate percentage based on tipping rules
- var percentage;
- if (this.bills[i] < 50) {
- percentage = .2;
- }else if (this.bills[i] >= 50 && this.bills[i] <200) {
- percentage = .15;
- }else{
- percentage = .1;
- }
- // Add results to the corresponing arrays
- this.tips[i] = this.bills[i] * percentage;
- this.finalValues[i] = this.bills[i] + this.bills[i] * percentage;
- }
- }
- }
- john.tipsCalc();
- console.log('John's Family TIPS ' + john.tips);
- console.log('John's Family BILLS ' + john.bills);
- console.log('John's Family Final a pagar ' + john.finalValues);
- console.log('---------------------------------------------------');
- var mark = {
- fullName: 'Mark Miller',
- bills: [77, 475, 110, 45],
- tipsCalc: function(){
- this.tips = [];
- this.finalValues = [];
- for (let i = 0; i < this.bills.length; i++) {
- // Determinate percentage based on tipping rules
- var percentage;
- if (this.bills[i] < 100) {
- percentage = .2;
- }else if (this.bills[i] >= 100 && this.bills[i] < 300) {
- percentage = .1;
- }else{
- percentage = .25;
- }
- // Add results to the corresponing arrays
- this.tips[i] = this.bills[i] * percentage;
- this.finalValues[i] = this.bills[i] + this.bills[i] * percentage;
- }
- }
- }
- mark.tipsCalc();
- console.log('Mark's Family TIPS ' + mark.tips);
- console.log('Mark's Family BILLS ' + mark.bills);
- console.log('Mark's Family Final a pagar ' + mark.finalValues);
- var sum = 0;
- var averageTips;
- function average(family) {
- for (let i = 0; i < family.tips.length; i++) {
- sum += family.tips[i];
- }
- return averageTips = sum / (family.tips.length);
- }
- console.log('---------------------------------------------------');
- average(john);
- console.log('John's Family Suma TIPS = ' + sum);
- console.log('John's Family Average TIPS = ' + averageTips);
- console.log('---------------------------------------------------');
- average(mark);
- console.log('Mark's Family Suma TIPS = ' + sum);
- console.log('Mark's Family Average TIPS = ' + averageTips);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement