Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>CtoF</title>
- <script src='CtoF.js'></script>
- </head>
- <body>
- <script id="jsbin-javascript">
- function testSuite(fn, testCycle) {
- const testCases = [
- {msg:'Boiling point', data:100, expected:212},
- {msg:'Mid point', data:50, expected:122},
- {msg:'Freezing', data:0, expected:32},
- {msg:'Zero F', data:-17.777, expected:0}
- ];
- console.log(`Test Cycle: ${testCycle}`);
- testCases.map((test, ind) =>{
- console.log(`\tTest Case ${ind + 1} - ${test.msg}`);
- const result = fn(test.data);
- console.log(`\t\tInput:${test.data}, Expected:${test.expected}, received:${result}, result:${
- test.expected === result ? 'Pass' : 'Fail'
- }`);
- })
- }
- {
- function tempCtoF(degC) {
- return Math.round(((degC/5)*9)+32);}
- //testSuite(tempCtoF, 'Single line ES5 function');
- }
- {
- const tempCtoF = (degC) => Math.round(((degC/5)*9)+32);
- //testSuite(tempCtoF, 'Single line Arrow function');
- }
- {
- const mult9 = (x) => x*9;
- const div5 = (x) => x/5;
- const add32 = (x) => x+32;
- const rnd = (x) => Math.round(x);
- function tempCtoF(degC) {
- return rnd(add32(div5(mult9(degC))));
- }
- //testSuite(tempCtoF, 'Sub-functions');
- }
- const gen = (opt, opd) => {
- switch(opt) {
- case '+': return (val) => val+opd; break;
- case '-': return (val) => val-opd; break;
- case '*': return (val) => val*opd; break;
- case '/': return (val) => val/opd; break;
- case '.': return (val) => Math.round(val); break;
- }};
- {
- const mult9 = gen('*',9);
- const div5 = gen('/',5);
- const add32 = gen('+',32);
- const rnd = gen('.');
- function tempCtoF(degC) {
- return rnd(add32(div5(mult9(degC))));
- }
- //testSuite(tempCtoF, 'Generated Sub-functions');
- }
- {
- const mult9 = gen('*',9);
- const div5 = gen('/',5);
- const add32 = gen('+',32);
- const rnd = gen('.');
- function tempCtoF(degC) {
- const ops = [mult9, div5, add32, rnd];
- return ops.reduce((degF,fn) => fn(degF),degC);
- }
- //testSuite(tempCtoF, 'Reduced Sub-functions 1');
- }
- {
- function tempCtoF(degC) {
- const ops = [
- gen('*',9),
- gen('/',5),
- gen('+',32),
- gen('.')
- ];
- return ops.reduce((degF,fn) => fn(degF), degC);
- }
- //testSuite(tempCtoF, 'Reduced Sub-functions 2');
- }
- {
- function tempCtoF(degC) {
- const pipe = (ops) => (val) =>
- ops.reduce((acc,fn) => fn(acc), val);
- return pipe([
- gen('*',9),
- gen('/',5),
- gen('+',32),
- gen('.')
- ])(degC);
- }
- testSuite(tempCtoF, 'Pipe Sub-functions 1');
- }
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">function testSuite(fn, testCycle) {
- const testCases = [
- {msg:'Boiling point', data:100, expected:212},
- {msg:'Mid point', data:50, expected:122},
- {msg:'Freezing', data:0, expected:32},
- {msg:'Zero F', data:-17.777, expected:0}
- ];
- console.log(`Test Cycle: ${testCycle}`);
- testCases.map((test, ind) =>{
- console.log(`\tTest Case ${ind + 1} - ${test.msg}`);
- const result = fn(test.data);
- console.log(`\t\tInput:${test.data}, Expected:${test.expected}, received:${result}, result:${
- test.expected === result ? 'Pass' : 'Fail'
- }`);
- })
- }
- {
- function tempCtoF(degC) {
- return Math.round(((degC/5)*9)+32);}
- //testSuite(tempCtoF, 'Single line ES5 function');
- }
- {
- const tempCtoF = (degC) => Math.round(((degC/5)*9)+32);
- //testSuite(tempCtoF, 'Single line Arrow function');
- }
- {
- const mult9 = (x) => x*9;
- const div5 = (x) => x/5;
- const add32 = (x) => x+32;
- const rnd = (x) => Math.round(x);
- function tempCtoF(degC) {
- return rnd(add32(div5(mult9(degC))));
- }
- //testSuite(tempCtoF, 'Sub-functions');
- }
- const gen = (opt, opd) => {
- switch(opt) {
- case '+': return (val) => val+opd; break;
- case '-': return (val) => val-opd; break;
- case '*': return (val) => val*opd; break;
- case '/': return (val) => val/opd; break;
- case '.': return (val) => Math.round(val); break;
- }};
- {
- const mult9 = gen('*',9);
- const div5 = gen('/',5);
- const add32 = gen('+',32);
- const rnd = gen('.');
- function tempCtoF(degC) {
- return rnd(add32(div5(mult9(degC))));
- }
- //testSuite(tempCtoF, 'Generated Sub-functions');
- }
- {
- const mult9 = gen('*',9);
- const div5 = gen('/',5);
- const add32 = gen('+',32);
- const rnd = gen('.');
- function tempCtoF(degC) {
- const ops = [mult9, div5, add32, rnd];
- return ops.reduce((degF,fn) => fn(degF),degC);
- }
- //testSuite(tempCtoF, 'Reduced Sub-functions 1');
- }
- {
- function tempCtoF(degC) {
- const ops = [
- gen('*',9),
- gen('/',5),
- gen('+',32),
- gen('.')
- ];
- return ops.reduce((degF,fn) => fn(degF), degC);
- }
- //testSuite(tempCtoF, 'Reduced Sub-functions 2');
- }
- {
- function tempCtoF(degC) {
- const pipe = (ops) => (val) =>
- ops.reduce((acc,fn) => fn(acc), val);
- return pipe([
- gen('*',9),
- gen('/',5),
- gen('+',32),
- gen('.')
- ])(degC);
- }
- testSuite(tempCtoF, 'Pipe Sub-functions 1');
- }
- </script></body>
- </html>
Add Comment
Please, Sign In to add comment