Last active
August 29, 2015 14:18
-
-
Save grese/9a41aff876ccecf1bf15 to your computer and use it in GitHub Desktop.
A script that creates functions for operators and operands, which can be used to perform basic math operations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Creates functions for the numbers 0-9, and also functions for +, -, *, and /. | |
* Here is a list of the available functions: | |
* Operands: zero(), one(), two(), three(), four(), five(), six(), seven(), eight(), nine(); | |
* Operators: plus(), minus(), times(), divide(); | |
* | |
* Usage: | |
* The Operands ('zero' through 'nine') can be called with or without an operator as an argument. | |
* | |
* The Operators ('plus', 'minus', etc..) require that a number be passed as a param, and must be executed within an Operand. | |
* | |
* Examples: | |
* (0) => zero(); // 0 | |
* (9) => nine(); // 9 | |
* (3 + 7 = 10) => three(plus(seven())); // 10 | |
* (1 - 6 = -5) => one(minus(six())); // -5 | |
* (4 * 4 = 16) => four(times(four())); // 16 | |
* (8 / 2 = 4) => eight(divide(two())); // 4 | |
* | |
* plus(); //INVALID (missing operand) | |
* one(two()); // INVALID (no operator) | |
*/ | |
// Operand Class: | |
var Operand = function(value){ | |
return function(operator){ | |
if(operator instanceof Function){ | |
return operator(value); | |
}else{ | |
return value; | |
} | |
}; | |
}; | |
// Operator Class: | |
var Operator = function(symbol){ | |
return function(rightOperand){ | |
return function(leftOperand){ | |
if(!isNaN(leftOperand) && !isNaN(rightOperand)){ | |
switch(symbol){ | |
case '+': | |
return leftOperand + rightOperand; | |
case '-': | |
return leftOperand - rightOperand; | |
case '*': | |
return leftOperand * rightOperand; | |
case '/': | |
return leftOperand / rightOperand; | |
default: | |
break; | |
} | |
}else{ | |
return null; | |
} | |
}; | |
}; | |
}; | |
// Function to convert int to english... | |
var getNumberName = function(num){ | |
switch(num){ | |
case 0: | |
return 'zero'; | |
case 1: | |
return 'one'; | |
case 2: | |
return 'two'; | |
case 3: | |
return 'three'; | |
case 4: | |
return 'four'; | |
case 5: | |
return 'five'; | |
case 6: | |
return 'six'; | |
case 7: | |
return 'seven'; | |
case 8: | |
return 'eight'; | |
case 9: | |
return 'nine'; | |
case 10: | |
return 'ten'; | |
default: | |
break; | |
} | |
}; | |
// Function to define the methods on the window object... | |
var defineMathFunctions = function(){ | |
// Define the operators: (+, -, *, /); | |
window['plus'] = new Operator('+'); | |
window['minus'] = new Operator('-'); | |
window['times'] = new Operator('*'); | |
window['divide'] = new Operator('/'); | |
// Define the operands: (0 - 9); | |
for(var i=0; i<10; i++){ | |
window[getNumberName(i)] = new Operand(i); | |
} | |
}; | |
// Go ahead and define those functions! | |
defineMathFunctions(); | |
// Some tests to make sure it works as advertised... | |
var tests = [ | |
function(){ | |
var expected1 = 2; | |
var result1 = one(plus(one())); | |
console.log('1+1=2 : ', result1 === expected1); | |
var expected2 = 8; | |
var result2 = four(plus(four())); | |
console.log('4+4=8 : ', result2 === expected2); | |
}, | |
function(){ | |
var expected1 = 27; | |
var result1 = three(times(nine())); | |
console.log('3*9=27 : ', result1 === expected1); | |
var expected2 = 0; | |
var result2 = seven(times(zero())); | |
console.log('7*0=0 : ', result2 === expected2); | |
}, | |
function(){ | |
var expected1 = 7; | |
var result1 = nine(minus(two())); | |
console.log('9-2=7 : ', result1 === expected1); | |
var expected2 = -7; | |
var result2 = one(minus(eight())); | |
console.log('1-8=(-7) : ', result2 === expected2); | |
}, | |
function(){ | |
var expected1 = 4; | |
var result1 = eight(divide(two())); | |
console.log('8/2=4 : ', result1 === expected1); | |
var expected2 = 1.2; | |
var result2 = six(divide(five())); | |
console.log('6/5=1.2 : ', result2 === expected2); | |
} | |
]; | |
tests.forEach(function(test){ | |
test(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment