From f8a95543890a4e3c4e58f9299717a0b1bbbd1b45 Mon Sep 17 00:00:00 2001 From: jess2896 Date: Thu, 26 Sep 2019 13:20:16 -0400 Subject: [PATCH 1/2] Calculates the tips of two objects --- index.html | 3 ++- script.js | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 script.js diff --git a/index.html b/index.html index 975ef26..955e354 100644 --- a/index.html +++ b/index.html @@ -8,4 +8,5 @@

Section 2: JavaScript Language Basics

- \ No newline at end of file + + diff --git a/script.js b/script.js new file mode 100644 index 0000000..f4b1c92 --- /dev/null +++ b/script.js @@ -0,0 +1,67 @@ +let john = { + bills : [124,48,268,180,42], + allTips : [], + finalAmounts : [], + calcTips : function(){ + for(let i=0;imark.tipsAvrg?"John's family has paid the highest amount of tips":"Mark's family has paid the highest amount of tips"; +} +console.log(highestTips); From b54fca5d752ab84125950c1b68f17fb801433101 Mon Sep 17 00:00:00 2001 From: jess2896 Date: Mon, 30 Sep 2019 11:49:42 -0400 Subject: [PATCH 2/2] Fixed the code's format --- index.html | 2 +- script.js | 126 ++++++++++++++++++++++++++++++++--------------------- 2 files changed, 77 insertions(+), 51 deletions(-) diff --git a/index.html b/index.html index 955e354..3a077e6 100644 --- a/index.html +++ b/index.html @@ -7,6 +7,6 @@

Section 2: JavaScript Language Basics

+ - diff --git a/script.js b/script.js index f4b1c92..40ec4aa 100644 --- a/script.js +++ b/script.js @@ -1,67 +1,93 @@ -let john = { - bills : [124,48,268,180,42], - allTips : [], - finalAmounts : [], - calcTips : function(){ - for(let i=0;i { + let tip; + + if (bill < 50){ + tip = bill * 0.2; + this.allTips.push(tip); + this.finalAmounts.push(tip + bill); } - else{ - this.allTips.push(this.bills[i]*0.1); - this.finalAmounts.push(this.allTips[i]+this.bills[i]); + else if (bill < 200) { + tip = bill * 0.15; + this.allTips.push(tip); + this.finalAmounts.push(tip + bill); } - } + else { + tip = bill * 0.1; + this.allTips.push(tip); + this.finalAmounts.push(tip + bill); + } + }); } -} -john.calcTips(); -console.log(john.allTips); -console.log(john.finalAmounts); +}; + +johnsFamily.calcTips(); + +console.log(johnsFamily.allTips); +console.log(johnsFamily.finalAmounts); //Extra -let mark = { - bills : [77,375,110,45], - allTips : [], - finalAmounts : [], - calcTips : function(){ - for(let i=0;i { + let tip; + if (bill < 100) { + tip = bill * 0.2; + this.allTips.push(tip); + this.finalAmounts.push(tip + bill); } - else if(this.bills[i]<300){ - this.allTips.push(this.bills[i]*0.1); - this.finalAmounts.push(this.allTips[i]+this.bills[i]); + else if (bill < 300) { + tip = bill * 0.1; + this.allTips.push(tip); + this.finalAmounts.push(tip + bill); } - else{ - this.allTips.push(this.bills[i]*0.25); - this.finalAmounts.push(this.allTips[i]+this.bills[i]); + else { + tip = bill * 0.25; + this.allTips.push(tip); + this.finalAmounts.push(tip + bill); } - } + }); } -} +}; -function tipsAvrg(tips){ - let sum=0; - for(let i=0;i { + sum += tip; + }); + + return sum / tips.length; } -mark.calcTips(); -john['avrgTips'] = tipsAvrg(john.allTips); -mark['avrgTips'] = tipsAvrg(mark.allTips); +marksFamily.calcTips(); -let highestTips='John and Mark have paid the same amount of average tips'; +johnsFamily['avrgTips'] = tipsAvrg(johnsFamily.allTips); +marksFamily['avrgTips'] = tipsAvrg(marksFamily.allTips); -if(john.avrgTips !== mark.avrgTips){ - highestTips = john.avrgTips>mark.tipsAvrg?"John's family has paid the highest amount of tips":"Mark's family has paid the highest amount of tips"; +if (johnsFamily.avrgTips > marksFamily.avrgTips) { + highestTipsFamily = johnsFamily.name; +} +else if (marksFamily.avrgTips > johnsFamily.avrgTips) { + highestTipsFamily = marksFamily.name; } + +const highestTips = johnsFamily.avrgTips !== marksFamily.avrgTips ? `${highestTipsFamily}\'s family has paid the highest amount of tips` : 'Both have paid the same amount of average tips'; + console.log(highestTips);