CS 170 Assignment 8


SUBMITTED BY: Guest

DATE: April 5, 2020, 7 p.m.

FORMAT: Text only

SIZE: 9.5 kB

HITS: 396

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset = "UTF-8" />
  5. <title> Best Eatery </title>
  6. <style>
  7. body {background-color: BlanchedAlmond; font-family: optima;
  8. color: MidnightBlue; text-align: center}
  9. p { font-size: 30px}
  10. button {font-size: 20px; background-color: coral}
  11. input { font-size: 20px}
  12. </style>
  13. </head>
  14. <body>
  15. <script>
  16. /*
  17. This function sets each element in its array parameter, p, to a random
  18. number between 5.00 and 25.00 inclusive which will
  19. be the price of the food item. It returns nothing.
  20. */
  21. function setItemPrices(p)
  22. {
  23. //add code here
  24. for(var i=0;i<p.length;i++)
  25. {
  26. p[i]=Math.floor(Math.random()*20)+5;
  27. }
  28. }
  29. /*
  30. This function sets each element in its array parameter, t, to to the product of
  31. the corresponding array elements in the arrays q and p. It
  32. returns nothing. For example: t[1] = q[1] * p[1]
  33. */
  34. function getItemTotals(q, p,t)
  35. {
  36. //add code here
  37. for(var i=0;i<q.length;i++)
  38. {
  39. t[i]=Number(q[i])*Number(p[i]);
  40. }
  41. }
  42. /*
  43. This function returns the sum of all of the elements in its array parameter,a.
  44. You will be passing the array that holds your totals to
  45. the parameter a.
  46. */
  47. function getTotalBill(a)
  48. {
  49. //add code here
  50. var sum=0;
  51. for(var i=0;i<a.length;i++)
  52. {
  53. sum=sum+Number(a[i]);
  54. }
  55. return sum.toFixed(2);
  56. }
  57. /*
  58. This function will find and return the POSITION of
  59. the largest value in its parameter variable, array a.
  60. */
  61. function findLargestPos(a)
  62. {
  63. //add code here
  64. var largePos=0;
  65. var largeValue=0;
  66. for(var i=0;i<a.length;i++)
  67. {
  68. if(Number(a[i])>largeValue)
  69. {
  70. largePos=i;
  71. largeValue=a[i];
  72. }
  73. }
  74. return largePos;
  75. }
  76. /* This function clears all total textboxes */
  77. function reset()
  78. {
  79. total0.value = '';
  80. total1.value = "";
  81. total2.value = "";
  82. total3.value = "";
  83. total4.value = "";
  84. profit.value = "";
  85. sumTotal.value = "";
  86. }
  87. // The array names holds the names of the food items.
  88. var names = ["toast","butter","cake","fruits","Jam"] // assign an initializer list that holds 5 food items. Keep the names short.
  89. //Complete the instruction below to declare a new array that will hold 5 elements
  90. var quantities = new Array(5);
  91. // Write a loop that will set all values of the quantities array to 0.
  92. for(var i=0;i<quantities.length;i++)
  93. quantities[i]=0;
  94. //Complete the instruction below to declare new arrays that will hold 5 elements each.
  95. var prices = new Array(0,0,0,0,0);
  96. var totals = new Array(0,0,0,0,0);
  97. </script>
  98. <!-- Much of the following is completed for you
  99. You need to complete JS code for the button total-->
  100. <h1> Best Eatery Order Summary </h1>
  101. <table id = "myTable" border="1"; align= "center">
  102. <tr>
  103. <!-- This button fills boxes with menu items from array names.-->
  104. <th> <button id = "name" onclick = 'name0.value = names[0];
  105. name1.value = names[1];
  106. name2.value = names[2];
  107. name3.value = names[3];
  108. name4.value = names[4];'
  109. >Food Item </button> </th>
  110. <th> Enter Number Ordered </th>
  111. <!-- This buttom fills the boxes with items from the array prices (random $ amounts)-->
  112. <th><button id = "PriceEach" onclick = '
  113. setItemPrices(prices);
  114. price0.value = "$" + prices[0].toFixed(2);
  115. price1.value = "$" + prices[1].toFixed(2);
  116. price2.value = "$" + prices[2].toFixed(2);
  117. price3.value = "$" + prices[3].toFixed(2);
  118. price4.value = "$" + prices[4].toFixed(2);
  119. reset();'>Price for each</button></th>
  120. <!-- This button will fill the total amount owed for each food item. For example,
  121. if names[0] = apple, quantity[0] = 2, prices[0] = 1.00, then
  122. total[0] = 2.00 so the total spent on apples is $2.00. -->
  123. <th><button id = "total" onclick = '
  124. getItemTotals(quantities, prices, totals);
  125. total0.value = "$" + totals[0].toFixed(2);
  126. total1.value = "$" + totals[1].toFixed(2);
  127. total2.value = "$" + totals[2].toFixed(2);
  128. total3.value = "$" + totals[3].toFixed(2);
  129. total4.value = "$" + totals[4].toFixed(2);
  130. // Add code below.
  131. sumTotal.value = "$" + getTotalBill(totals);//Call the approprite function to find the total bill amount.
  132. //Display fixed to 2 decimal places
  133. var position = findLargestPos(totals)// Call the apprpopriate function to find the position of the
  134. // largest value in the totals array
  135. profit.value = names[position] //assign the food item with that value to profit.value
  136. ' >Item Total</button> </th>
  137. </tr>
  138. <!-- This section deals with the textboxes n the table. There are four values for each
  139. food item to account for: name, quantity, price, total. You do NOT have to
  140. modify any code below.-->
  141. <tr>
  142. <td><input type="text" id="name0" size=10 style="background-color:cadetblue" value = '' onchange = ''></td>
  143. <td><input type="text" id="quantity0" size=10 style = "text-align: center" value = '' onchange =
  144. 'quantities[0] = Number(quantity0.value)'></td>
  145. <td><input type="text" id="price0" size=10 style="background-color:cadetblue"readonly></td>
  146. <td><input type="text" id="total0" size=10 style="background-color:cadetblue" readonly></td>
  147. </tr>
  148. <tr>
  149. <td><input type="text" id="name1" size=10 style="background-color:cadetblue" value = '' onchange = ''></td>
  150. <td><input type="text" id="quantity1" size=10 style = "text-align: center" value = '' onchange ='quantities[1]=Number(quantity1.value)'></td>
  151. <td><input type="text" id="price1" size=10 style="background-color:cadetblue"readonly></td>
  152. <td><input type="text" id="total1" size=10 style="background-color:cadetblue" readonly></td>
  153. </tr>
  154. <tr>
  155. <td><input type="text" id="name2" size=10 style="background-color:cadetblue" value = '' onchange = ''></td>
  156. <td><input type="text" id="quantity2" size=10 style = "text-align: center" value = '' onchange ='quantities[2]=Number(quantity2.value)'></td>
  157. <td><input type="text" id="price2" size=10 style="background-color:cadetblue" readonly></td>
  158. <td><input type="text" id="total2" size=10 style="background-color:cadetblue" readonly></td>
  159. </tr>
  160. <tr>
  161. <td><input type="text" id="name3" size=10 style="background-color:cadetblue" value = '' onchange = ''></td>
  162. <td><input type="text" id="quantity3" size=10 style = "text-align: center" value = '' onchange ='quantities[3]=Number(quantity3.value)'></td>
  163. <td><input type="text" id="price3" size=10 style="background-color:cadetblue" readonly></td>
  164. <td><input type="text" id="total3" size=10 style="background-color:cadetblue" readonly></td>
  165. </tr>
  166. <tr>
  167. <td><input type="text" id="name4" size=10 style="background-color:cadetblue" onchange=''></td>
  168. <td><input type="text" id="quantity4" size=10 style = "text-align: center" value = '' onchange =
  169. 'quantities[4]=Number(quantity4.value)'></td>
  170. <td><input type="text" id="price4" size=10 style="background-color:cadetblue" readonly></td>
  171. <td><input type="text" id="total4" size=10 style="background-color:cadetblue" readonly></td>
  172. </tr>
  173. </table>
  174. <p>
  175. Total Amount Collected:
  176. <input type = "text" id = "sumTotal" size = 10 = value = "" readonly />
  177. </p>
  178. <p>
  179. Most Profitable Item:
  180. <input type = "text" id = "profit" size = 10 value = "" readonly>
  181. </p>
  182. </body>
  183. </html>

comments powered by Disqus