Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.80 KB | None | 0 0
  1. // JavaScript coding challenge 1
  2.  
  3. /* BMI: mass / height^2 = mass / (height * height)
  4. 1.store mark and john's mass and height in variables.
  5. 2. calculate both their bmis.
  6. 3. create a boolean variable containing information about whether Mark has a higher BMI than John
  7. 4. Print a string to the console containing the variable from Step 3. (Something like "Is Mark's BMI higher than
  8. John's? true").
  9. */
  10.  
  11. var markMass; var markHeight; var johnMass; var johnHeight; var markBMI; var johnBMI;
  12. markMass = prompt('What is Mark\'s Mass?' );
  13. markHeight = prompt('What is Mark\'s Height?' );
  14. johnMass = prompt('What is John\'s Mass?' );
  15. johnHeight = prompt('What is John\'s Height?' );
  16.  
  17. markBMI = markMass / (markHeight * markHeight);
  18. johnBMI = johnMass / (johnHeight * johnHeight);
  19.  
  20. var markBiggerBMI = markBMI > johnBMI;
  21.  
  22. console.log(markBMI, johnBMI);
  23.  
  24. if(markBMI > johnBMI){
  25. console.log('Mark\'s BMI is bigger than John\'s');
  26. }
  27. else {
  28. console.log('John\'s BMI is bigger than Mark\'s');
  29. }
  30.  
  31. //CODING CHALLENGE #2
  32. /*
  33. John and Mike both play basketball in different teams. In the latest 3 games, John's team scored 89,
  34. 120 and 103 points, while Mike's team scored 116, 94, and 123 points.
  35. 1. Calculate the average score for each team.
  36. 2. Decide which teams wins in average (highest average score), and print the winner to the console.
  37. Also include the average score in the output.
  38. 3. Then change the scores to show different winners.
  39. Don't forget to take into acount there might be a draw (the same average score).
  40. 4. EXTRA: Mary also plays basketball, and her team scored 97, 134, and 105 points. Like before, log the
  41. average winner to the console. HINT: you will need the && operator to take the decision.
  42. 5. Like before, change the scores to generate different winners, keeping in mind there might be draws.
  43. */
  44. var teamJohnAvg = (89 + 102 + 103) / 3;
  45. var teamMikeAvg = (116 + 94 + 123) / 3;
  46. var teamMaryAvg = (97 + 134 + 105) / 3;
  47. console.log('John\'s team Average: ' + teamJohnAvg + ' \n' +
  48. 'Mike\'s team Average: ' + teamMikeAvg + ' \n' +
  49. 'Mary\'s team Average: ' + teamMaryAvg
  50. );
  51.  
  52. if (teamJohnAvg > teamMikeAvg && teamJohnAvg > teamMaryAvg) { console.log('John\'s team is winning'); }
  53. else if (teamMikeAvg > teamJohnAvg && teamMikeAvg > teamMaryAvg) { console.log('Mike\'s team is winning'); }
  54. else if ( teamMaryAvg > teamJohnAvg && teamMaryAvg > teamMikeAvg) { console.log('Mary\'s team is winning'); }
  55. else if (teamJohnAvg === teamMikeAvg && teamJohnAvg > teamMaryAvg) { console.log('John\'s team and Mike\'s team are tied for winning'); }
  56. else if (teamJohnAvg === teamMaryAvg && teamJohnAvg > teamMikeAvg ) { console.log('John\'s team and Mary\'s team are tied for winning'); }
  57. else if (teamMikeAvg === teamMaryAvg && teamMaryAvg > teamJohnAvg ) { console.log('Mike\'s team and Mary\'s team are tied for winning'); }
  58. else { console.log('They are all tied for winning \n\n'); }
  59.  
  60. console.log('John\'s Team Score: ' + teamJohnAvg + '\n' +
  61. 'Mike\'s Team Score: ' + teamMikeAvg + '\n' +
  62. 'Mary\'s Team Score: ' + teamMaryAvg
  63. );
  64.  
  65. /*
  66. CODING CHALLENGE 3
  67. John and his family went on a holiday and went to 3 different restaurants. The bills were $124, $48, and $268.
  68. To tip the waiter a fair amount, John created a simple tip calculator (as a function). He likes to tip 20% of
  69. the bill when the bill is less than $50, 15% when the bill is between 50-200 dollars, and 10% if more than 200 dollars.
  70. In the end, John would like to have 2 arrays:
  71. 1. Containing all three tips (one for each bill)
  72. 2. Containing all three final paid amounts (bill + tip).
  73.  
  74. (NOTE: To calculate 20% of a value, simply multiply if with 20/100 = 0.2)
  75. */
  76. var billsBeforeTip = [124, 48, 268]; var tipAmounts = []; var finalAmounts = [];
  77.  
  78. function tipCalculator(billAmt)
  79. {
  80. var percentage;
  81. if ( billAmt <= 50) {
  82. percentage = .2; }
  83. else if (50 < billAmt <= 200) {
  84. percentage = .15;
  85. }
  86. else {
  87. percentage = .1;
  88. }
  89. return percentage * billAmt
  90. }
  91.  
  92. for (i = 0; i< billsBeforeTip.length; i++)
  93. {
  94. tipAmounts[i] = tipCalculator(billsBeforeTip[i]);
  95. finalAmounts[i] = billsBeforeTip[i] + tipAmounts[i];
  96. console.log([i] + '. total tip: ' + tipAmounts[i]);
  97. }
  98.  
  99. console.log('\n\nBills before tips: ' + billsBeforeTip.toString());
  100. console.log('Final Amounts: ' + finalAmounts.toString());
  101.  
  102.  
  103. /*
  104. Coding Challenge #4
  105. remember Mark and John BMI Challenge. Implement the same functionality with objects and methods.
  106. 1. For each of them, create an object with properties for their full name, mass, and Height
  107. 2. Then, add a method to each object to calculate the BMI. Save the BMI to the object and also return
  108. it from the method.
  109. 3. In the end, log to the console who has the highest BMI, together with the full name and the respective BMI.
  110. Don't forget they might have the same BMI.
  111. Remember: BMI = mass / heightˆ2 = mass / (height * height). (mass in kg and height in meter).
  112. */
  113.  
  114. var mark = {
  115. name: 'Mark Cage', // key: value
  116. mass: '78',
  117. height: '1.75',
  118. calcBMI: function(){
  119. this.bmi = this.mass / Math.pow(this.height,2);
  120. }
  121. }
  122.  
  123. mark.calcBMI();
  124. console.log(mark);
  125.  
  126. var john = {
  127. name: 'John Smith', // key: value
  128. mass: '82',
  129. height: '1.65',
  130. calcBMI: function(){
  131. this.bmi = this.mass / Math.pow(this.height,2);
  132. }
  133. }
  134.  
  135. john.calcBMI();
  136. console.log(john);
  137.  
  138. if(mark.bmi > john.bmi){
  139. console.log(mark.name + ' has a higher BMI');
  140. }
  141. else if (mark.bmi < john.bmi){
  142. console.log(john.name + ' has a higher BMI');
  143. } else {
  144. console.log(mark.name + ' and ' + john.name + ' has the same BMI');
  145. }
  146.  
  147. //coding Challenge #5
  148. /*
  149. tip tipCalculator V2.0
  150. John and his family went to 5 different restaurants.
  151. The bills were $124, $48, $268, $180, and $42.
  152. John likes to tip 20% of the bill when the bill is less than $50, 15% when the bill is between $50, and $200, and 10% if the bill is more than $200.
  153.  
  154. Implement a tip calculator using objects and loops:
  155. 1. Create an object with an array for the bill values
  156. 2. add a method to calculate the tips.
  157. 3. this method should include a loop to iterate over all the paid bills and do the tip calculations
  158. 4. As an output, create (1) a new array containing all tips, and (2) an array containing final paid amounts (bill + tip). HINT: start with 2 empty arrays as PROPERTIESand then fill them up in the loop.
  159. */
  160. var john = {
  161. fullName: 'John Smith',
  162. bills: [128, 48, 268, 180, 42],
  163. calcTips: function(){
  164. this.tipAmounts = []; this.finalAmounts = [];
  165. for (var i = 0; i < this.bills.length; i++) {
  166. var percentage;
  167. var bill = this.bills[i];
  168. if (bill < 50) {
  169. percentage = .2;
  170. }
  171. else if (50 <= bill <= 200 ) {
  172. percentage = .15;
  173. }
  174. else {
  175. percentage = .1;
  176. }
  177. this.tipAmounts[i] = bill * percentage;
  178. this.finalAmounts[i] = bill + (bill * percentage);
  179. }
  180. }
  181. }
  182. john.calcTips();
  183. console.log(john);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement