Guest User

Untitled

a guest
Oct 12th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>CtoF</title>
  7. <script src='CtoF.js'></script>
  8. </head>
  9. <body>
  10. <script id="jsbin-javascript">
  11. function testSuite(fn, testCycle) {
  12. const testCases = [
  13. {msg:'Boiling point', data:100, expected:212},
  14. {msg:'Mid point', data:50, expected:122},
  15. {msg:'Freezing', data:0, expected:32},
  16. {msg:'Zero F', data:-17.777, expected:0}
  17. ];
  18.  
  19. console.log(`Test Cycle: ${testCycle}`);
  20. testCases.map((test, ind) =>{
  21. console.log(`\tTest Case ${ind + 1} - ${test.msg}`);
  22. const result = fn(test.data);
  23. console.log(`\t\tInput:${test.data}, Expected:${test.expected}, received:${result}, result:${
  24. test.expected === result ? 'Pass' : 'Fail'
  25. }`);
  26. })
  27. }
  28.  
  29. {
  30. function tempCtoF(degC) {
  31. return Math.round(((degC/5)*9)+32);}
  32. //testSuite(tempCtoF, 'Single line ES5 function');
  33. }
  34.  
  35. {
  36. const tempCtoF = (degC) => Math.round(((degC/5)*9)+32);
  37. //testSuite(tempCtoF, 'Single line Arrow function');
  38. }
  39.  
  40. {
  41. const mult9 = (x) => x*9;
  42. const div5 = (x) => x/5;
  43. const add32 = (x) => x+32;
  44. const rnd = (x) => Math.round(x);
  45.  
  46. function tempCtoF(degC) {
  47. return rnd(add32(div5(mult9(degC))));
  48. }
  49.  
  50. //testSuite(tempCtoF, 'Sub-functions');
  51. }
  52.  
  53. const gen = (opt, opd) => {
  54. switch(opt) {
  55. case '+': return (val) => val+opd; break;
  56. case '-': return (val) => val-opd; break;
  57. case '*': return (val) => val*opd; break;
  58. case '/': return (val) => val/opd; break;
  59. case '.': return (val) => Math.round(val); break;
  60. }};
  61.  
  62. {
  63. const mult9 = gen('*',9);
  64. const div5 = gen('/',5);
  65. const add32 = gen('+',32);
  66. const rnd = gen('.');
  67.  
  68. function tempCtoF(degC) {
  69. return rnd(add32(div5(mult9(degC))));
  70. }
  71.  
  72. //testSuite(tempCtoF, 'Generated Sub-functions');
  73. }
  74.  
  75. {
  76. const mult9 = gen('*',9);
  77. const div5 = gen('/',5);
  78. const add32 = gen('+',32);
  79. const rnd = gen('.');
  80.  
  81. function tempCtoF(degC) {
  82. const ops = [mult9, div5, add32, rnd];
  83. return ops.reduce((degF,fn) => fn(degF),degC);
  84. }
  85.  
  86. //testSuite(tempCtoF, 'Reduced Sub-functions 1');
  87. }
  88.  
  89. {
  90. function tempCtoF(degC) {
  91. const ops = [
  92. gen('*',9),
  93. gen('/',5),
  94. gen('+',32),
  95. gen('.')
  96. ];
  97. return ops.reduce((degF,fn) => fn(degF), degC);
  98. }
  99.  
  100. //testSuite(tempCtoF, 'Reduced Sub-functions 2');
  101. }
  102.  
  103. {
  104. function tempCtoF(degC) {
  105. const pipe = (ops) => (val) =>
  106. ops.reduce((acc,fn) => fn(acc), val);
  107.  
  108. return pipe([
  109. gen('*',9),
  110. gen('/',5),
  111. gen('+',32),
  112. gen('.')
  113. ])(degC);
  114. }
  115.  
  116. testSuite(tempCtoF, 'Pipe Sub-functions 1');
  117. }
  118. </script>
  119.  
  120.  
  121.  
  122. <script id="jsbin-source-javascript" type="text/javascript">function testSuite(fn, testCycle) {
  123. const testCases = [
  124. {msg:'Boiling point', data:100, expected:212},
  125. {msg:'Mid point', data:50, expected:122},
  126. {msg:'Freezing', data:0, expected:32},
  127. {msg:'Zero F', data:-17.777, expected:0}
  128. ];
  129.  
  130. console.log(`Test Cycle: ${testCycle}`);
  131. testCases.map((test, ind) =>{
  132. console.log(`\tTest Case ${ind + 1} - ${test.msg}`);
  133. const result = fn(test.data);
  134. console.log(`\t\tInput:${test.data}, Expected:${test.expected}, received:${result}, result:${
  135. test.expected === result ? 'Pass' : 'Fail'
  136. }`);
  137. })
  138. }
  139.  
  140. {
  141. function tempCtoF(degC) {
  142. return Math.round(((degC/5)*9)+32);}
  143. //testSuite(tempCtoF, 'Single line ES5 function');
  144. }
  145.  
  146. {
  147. const tempCtoF = (degC) => Math.round(((degC/5)*9)+32);
  148. //testSuite(tempCtoF, 'Single line Arrow function');
  149. }
  150.  
  151. {
  152. const mult9 = (x) => x*9;
  153. const div5 = (x) => x/5;
  154. const add32 = (x) => x+32;
  155. const rnd = (x) => Math.round(x);
  156.  
  157. function tempCtoF(degC) {
  158. return rnd(add32(div5(mult9(degC))));
  159. }
  160.  
  161. //testSuite(tempCtoF, 'Sub-functions');
  162. }
  163.  
  164. const gen = (opt, opd) => {
  165. switch(opt) {
  166. case '+': return (val) => val+opd; break;
  167. case '-': return (val) => val-opd; break;
  168. case '*': return (val) => val*opd; break;
  169. case '/': return (val) => val/opd; break;
  170. case '.': return (val) => Math.round(val); break;
  171. }};
  172.  
  173. {
  174. const mult9 = gen('*',9);
  175. const div5 = gen('/',5);
  176. const add32 = gen('+',32);
  177. const rnd = gen('.');
  178.  
  179. function tempCtoF(degC) {
  180. return rnd(add32(div5(mult9(degC))));
  181. }
  182.  
  183. //testSuite(tempCtoF, 'Generated Sub-functions');
  184. }
  185.  
  186. {
  187. const mult9 = gen('*',9);
  188. const div5 = gen('/',5);
  189. const add32 = gen('+',32);
  190. const rnd = gen('.');
  191.  
  192. function tempCtoF(degC) {
  193. const ops = [mult9, div5, add32, rnd];
  194. return ops.reduce((degF,fn) => fn(degF),degC);
  195. }
  196.  
  197. //testSuite(tempCtoF, 'Reduced Sub-functions 1');
  198. }
  199.  
  200. {
  201. function tempCtoF(degC) {
  202. const ops = [
  203. gen('*',9),
  204. gen('/',5),
  205. gen('+',32),
  206. gen('.')
  207. ];
  208. return ops.reduce((degF,fn) => fn(degF), degC);
  209. }
  210.  
  211. //testSuite(tempCtoF, 'Reduced Sub-functions 2');
  212. }
  213.  
  214. {
  215. function tempCtoF(degC) {
  216. const pipe = (ops) => (val) =>
  217. ops.reduce((acc,fn) => fn(acc), val);
  218.  
  219. return pipe([
  220. gen('*',9),
  221. gen('/',5),
  222. gen('+',32),
  223. gen('.')
  224. ])(degC);
  225. }
  226.  
  227. testSuite(tempCtoF, 'Pipe Sub-functions 1');
  228. }
  229.  
  230. </script></body>
  231. </html>
Add Comment
Please, Sign In to add comment