Advertisement
EntropyStarRover

(Demo) Technology Fundamentals Mid Exam - 02 March 2019

Jun 25th, 2019
177
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 1.Cooking masterclass
  2.  
  3. function cooking(input){
  4. input=input.map(Number);
  5.  
  6. let budget=input.shift();
  7. let students=input.shift();
  8. let flourPrice=input.shift();
  9. let singleEggPrice=input.shift();
  10. let apronPrice=input.shift();
  11. let moneyNeeded=0;
  12. let aprons=0;
  13.  
  14.  
  15. for (let i=1; i<=students; i++){
  16. moneyNeeded+=singleEggPrice*10;
  17. aprons++;
  18. if (i%5!==0){
  19. moneyNeeded+=flourPrice;
  20. }
  21. }
  22. aprons=Math.ceil(aprons*1.2);
  23.  
  24. moneyNeeded+=apronPrice*aprons;
  25. if (budget>=moneyNeeded){
  26. console.log(`Items purchased for ${moneyNeeded.toFixed(2)}$.`);
  27. } else {
  28. console.log(`${(moneyNeeded-budget).toFixed(2)}$ more needed.`)
  29. }
  30.  
  31. }
  32.  
  33.  
  34. 02. Bread Factory
  35.  
  36. function baker(input){
  37. let energy=100;
  38. let coins=100;
  39.  
  40. let arr=input[0].split("|");
  41.  
  42. let bonusEnergy=0;
  43.  
  44.  
  45.  
  46. for (let i=0; i<arr.length; i++){
  47. element=arr[i];
  48. let elAr=element.split("-");
  49. let action = elAr[0];
  50. let value=Number(elAr[1]);
  51.  
  52.  
  53.  
  54. if (action.includes("rest")){
  55. if (energy+value>100){
  56. bonusEnergy=100-energy;
  57. }
  58.  
  59. else {
  60. bonusEnergy=value;
  61. }
  62.  
  63. energy+=bonusEnergy;
  64. console.log(`You gained ${bonusEnergy} energy.`);
  65. console.log(`Current energy: ${energy}.`);
  66.  
  67. } else if (action.includes("order")) {
  68. if (energy-30>=0){
  69. energy-=30;
  70. coins+=value;
  71. console.log(`You earned ${value} coins.`);
  72. } else {
  73. console.log("You had to rest!");
  74. energy+=50;
  75. }
  76.  
  77. } else {
  78. if (coins-value>0){
  79. coins-=value;
  80. console.log(`You bought ${action}.`);
  81. } else {
  82. console.log(`Closed! Cannot afford ${action}.`);
  83. breakFlag=true;
  84. return;
  85. }
  86.  
  87. }
  88.  
  89.  
  90.  
  91. }
  92.  
  93. console.log("Day completed!");
  94. console.log(`Coins: ${coins}`);
  95. console.log(`Energy: ${energy}`);
  96.  
  97. }
  98.  
  99.  
  100. 3.Cooking factory
  101.  
  102.  
  103. function bakeFactory(input) {
  104. input = input.slice(0, input.length - 1);
  105. let batchesObjArr = []
  106.  
  107.  
  108. for (let i = 0; i < input.length; i++) {
  109. let batch = input[i];
  110. let batchArr = batch.split("#");
  111. batchArr = batchArr.map(Number);
  112. let sum = 0;
  113. batchArr.forEach(element => {
  114. sum += element;
  115. });
  116.  
  117.  
  118. batchesObjArr.push({
  119. batch: batchArr,
  120. totalQ: sum,
  121. average: sum / batchArr.length
  122. });
  123.  
  124. }
  125.  
  126. batchesObjArr = batchesObjArr.sort(function (a, b) {
  127. return b.totalQ - a.totalQ;
  128. })
  129. let bestBatch = batchesObjArr[0].totalQ;
  130.  
  131.  
  132. batchesObjArr = batchesObjArr.filter((element => element.totalQ == bestBatch));
  133. batchesObjArr = batchesObjArr.sort(function (a, b) {
  134. return b.average - a.average;
  135. })
  136.  
  137. batchesObjArr=batchesObjArr.sort(function(a,b){
  138. return a.batch.length-b.batch.length;
  139. })
  140.  
  141.  
  142. console.log(`Best Batch quality: ${bestBatch}`);
  143. console.log(batchesObjArr[0].batch.join(" "));
  144. }
Advertisement
RAW Paste Data Copied
Advertisement