Skip to content

Instantly share code, notes, and snippets.

@stacietaylorcima
Created February 17, 2019 20:54
Show Gist options
  • Save stacietaylorcima/8b8fae9d98f65eeb4865c198fd95aa73 to your computer and use it in GitHub Desktop.
Save stacietaylorcima/8b8fae9d98f65eeb4865c198fd95aa73 to your computer and use it in GitHub Desktop.
var john = {
    fullName: "John Smith", 
    bill: [124, 48, 268, 180, 42], 
    calcTips: function(){
        this.tips = [];
        this.finalValues = [];

        for (var i = 0; i < this.bill.length; i++){
            var percent; 
            var bill = this.bill[i];

            if (bill > 0 && bill < 50){
                percent = .2;
            } else if (bill >= 50 && bill <200){
                percent = .15;
            } else {
                percent = .1;
            }

            this.tips[i] = bill * percent;
            this.finalValues[i] = bill + bill * percent;
        }
    }
}

var mark = {
    fullName: "Mark Miller", 
    bill: [77, 475, 110, 45], 
    calcTips: function(){
        this.tips = [];
        this.finalValues = [];

        for (var i = 0; i < this.bill.length; i++){
            var percent; 
            var bill = this.bill[i];

            if (bill > 100){
                percent = .2;
            } else if (bill >= 100 && bill < 300){
                percent = .1;
            } else {
                percent = .25;
            }

            this.tips[i] = bill * percent;
            this.finalValues[i] = bill + bill * percent;
        }
    }
}

function calcAverage(tips){
    var sum = 0;
    for (i = 0; i < tips.length; i++){
        sum = sum + tips[i];
    }
    return sum / tips.length;
}

john.calcTips();
mark.calcTips();
console.log(john, mark); 

john.average = calcAverage(john.tips); 
mark.average = calcAverage(mark.tips); 
console.log(john, mark); 

if (john.average > mark.average){
    console.log(john.fullName + "'s family pays higher tips, with an average of $" + john.average);
    }
    else if (mark.average > john.average) {
        console.log(mark.fullName + "'s family pays higher tips, with an average of $" + mark.average);
    }
    else {
        console.log("Both " + mark.fullName + " and " + john.fullName + " pay the same tips!"); 
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment