Created
May 17, 2013 15:02
-
-
Save m3talsmith/5599640 to your computer and use it in GitHub Desktop.
Beer Friday Challenges #1: Beer level 1 (you drink one beer before solving this)
The dynamic conditional. Solve in any language, but you must solve with code - no mental substitutions. Drink, fork, and solve, people!
This file contains hidden or 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
((15, 'less_than', 30) == true) |
And how do you get to that state mark?
Here's my solution in ruby:
def parse_operator(operator)
case operator.downcase.to_sym
when :less_than
:<
# and so on with other operator cases
end
end
def evaluate(key, operator, value)
key.send(parse_operator(operator), value)
end
evaluate(10, 'less_than', 30) == true
JS:
var evaluations = {
less_than: function (min, max) {
return min < max;
},
more_than: function (max, min) {
return max > min;
}
};
function evaluate(key, operator, value) {
return evaluations[operator](key, value);
}
console.log(evaluate(10, 'less_than', 30) == true);
console.log(evaluate(10, 'more_than', 30) == true);
Strictly, it should be === in JS although this works as intended.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
15 < 40 ? true : false